UIScrollerView循环滚动,加定时器和pageControl
2016-03-14 10:43
281 查看
.h文件
.m文件
原文地址:http://blog.sina.com.cn/s/blog_b8e97683010189he.html
解决动画未加载完切换到头尾页面而无法正常显示的问题:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int offsetX = (int)myScrollView.contentOffset.x;
if (offsetX - myScrollView.contentOffset.x == 0 && offsetX % (int)VIEWWIDTH == 0) {
[myScrollView setUserInteractionEnabled:YES];
}
else{
[myScrollView setUserInteractionEnabled:NO];
}
}
@interface MainViewController : UIViewController<UIScrollViewDelegate>{ BOOL isDeceleration; } @property (nonatomic, retain)UIScrollView * myScrollView; @property (nonatomic, retain)NSMutableArray * imageArray; @property (nonatomic, retain) NSTimer * time; @property (nonatomic, retain) UIPageControl * myPageControl; @end
.m文件
#import "Main<span style="font-family: Arial, Helvetica, sans-serif;">ViewController.h"</span> @interface MainViewController () @end @implementation ViewController @synthesize myScrollView,imageArray,time,myPageControl; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } #pragma mark - ScrollView Delegate -(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{ isDeceleration = YES; } -(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ [self scrollViewDidEndDecelerating:myScrollView]; } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { isDeceleration = NO; int currentPage = floor((self.myScrollView.contentOffset.x-self.myScrollView.frame.size.width/([imageArray count]+2))/self.myScrollView.frame.size.width)+1; if (currentPage==0) { [self.myScrollView scrollRectToVisible:CGRectMake(myScrollView.frame.size.width*[imageArray count], myScrollView.frame.origin.y, myScrollView.frame.size.width, myScrollView.frame.size.height) animated:NO]; }else if(currentPage==([imageArray count]+1)){//如果最后加一,也就是开始循环的第一个 [self.myScrollView scrollRectToVisible:CGRectMake(myScrollView.frame.size.width, myScrollView.frame.origin.y, myScrollView.frame.size.width, myScrollView.frame.size.height) animated:NO]; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.imageArray = [[[NSMutableArray alloc]initWithObjects:@"t_g_egg1_no@2x.png",@"t_g_egg1@2x.png",@"t_g_image_egg0.png",@"t_g_image_quan0@2x.png",@"y_g_tip_image_egg1.png",@"y_g_tip_image_quan1@2x.png",nil]autorelease]; self.myScrollView = [[[UIScrollView alloc]initWithFrame:CGRectMake(20, 20, 280, 360)]autorelease]; myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width * ([imageArray count] + 2), myScrollView.frame.size.height); myScrollView.delegate = self; myScrollView.bounces = YES; myScrollView.pagingEnabled = YES; myScrollView.userInteractionEnabled = YES; [self.view addSubview:myScrollView]; //把最后一张图片放到首页的位置 UIImageView * imgView =[[UIImageView alloc]initWithFrame:CGRectMake(myScrollView.frame.size.width *0, myScrollView.frame.origin.y, myScrollView.frame.size.width, myScrollView.frame.size.height)]; imgView.image = [UIImage imageNamed:[imageArray objectAtIndex:[imageArray count]-1]]; [myScrollView addSubview:imgView]; [imgView release]; //从第一张开始加 for (int i = 0; i < [imageArray count]; i++) { UIImageView * imgView1 =[[UIImageView alloc]initWithFrame:CGRectMake(myScrollView.frame.size.width *(i+1), myScrollView.frame.origin.y, myScrollView.frame.size.width, myScrollView.frame.size.height)]; imgView1.image = [UIImage imageNamed:[imageArray objectAtIndex:i]]; [myScrollView addSubview:imgView1]; [imgView1 release]; } //把第一张图片放到最后的位置 UIImageView * imgView0 =[[UIImageView alloc]initWithFrame:CGRectMake(myScrollView.frame.size.width * ([imageArray count]+1), myScrollView.frame.origin.y, myScrollView.frame.size.width, myScrollView.frame.size.height)]; imgView0.image = [UIImage imageNamed:[imageArray objectAtIndex:0]]; [myScrollView addSubview:imgView0]; [imgView0 release]; [myScrollView setContentOffset:CGPointMake(0, 0)]; [myScrollView scrollRectToVisible:CGRectMake(myScrollView.frame.size.width, myScrollView.frame.origin.y, myScrollView.frame.size.width, myScrollView.frame.size.height) animated:NO]; //与pagecontrol结合使用 self.myPageControl =[[UIPageControl alloc]initWithFrame:CGRectMake(30, 360, 260, 30)]; myPageControl.currentPage = 0; myPageControl.numberOfPages = [imageArray count]; [self.view addSubview:myPageControl]; [myPageControl release]; //加入定时器 self.time = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(pageChange) userInfo:nil repeats:YES]; } -(void)pageChange { if (isDeceleration==NO) { [myScrollView setContentOffset:CGPointMake(myScrollView.contentOffset.x+280, myScrollView.contentOffset.y) animated:YES]; CGFloat pageWidth = myScrollView.frame.size.width; int page = floor((myScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; myPageControl.currentPage = page; if (page==[imageArray count]) {//如果到最后一页,设置currentPage=0;重新开始 myPageControl.currentPage = 0; } } } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; [self setMyScrollView:nil]; [self setImageArray:nil]; [self setMyPageControl:nil]; } -(void)dealloc { [myScrollView release]; [imageArray release]; [time release]; [myPageControl release]; myScrollView.delegate = nil; [super dealloc]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
原文地址:http://blog.sina.com.cn/s/blog_b8e97683010189he.html
解决动画未加载完切换到头尾页面而无法正常显示的问题:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int offsetX = (int)myScrollView.contentOffset.x;
if (offsetX - myScrollView.contentOffset.x == 0 && offsetX % (int)VIEWWIDTH == 0) {
[myScrollView setUserInteractionEnabled:YES];
}
else{
[myScrollView setUserInteractionEnabled:NO];
}
}
相关文章推荐
- javascript实现状态栏文字首尾相接循环滚动的方法
- 多种JQuery循环滚动文字图片效果代码
- jQuery实现列表自动滚动循环滚动展示新闻
- jQuery实现自动与手动切换的滚动新闻特效代码分享
- 兼容IE和Firefox火狐的上下、左右循环无间断滚动JS代码
- iOS 循环滚动的UIScrollView
- UIPickerView实现循环滚动
- 图片循环滚动控件ScrollingImageView
- UIScrollView循环滚动
- [iOS]UICollectionView循环滚动功能的实现思路
- ViewPager和Gallery的循环滚动
- Flash游戏背景的循环滚动实现
- viewpager实现无限循环滚动幻灯片
- Jquery 动画-无限循环滚动
- 循环播放视图
- OC基础之可循环滚动并突出中间图片,并且可点击
- android开发游记:广告轮播器无限循环滚动 首尾无缝切换解决方案
- Android中RecyclerView设置边框并循环滚动
- C#中实现label文字循环滚动
- iOS - UIPickerView文字大小颜色修改,无限轮播,无限循环滚动