IOS:个人笔记|UI_UIScrollView图片循环轮播
先上结果图
不会制作gif,直接放个截图吧
代码如下
基本思路
1:先是实现基本的ScrollView滑动。主要弄清几个宽度之间的关系。
2:然后是pageControl控件的一些设置,currentpage的轮换需要配合scrollview的几个代理方法,监听scrollview的事件
注意pageControl和scrollview是同等级的关系。
3:自动播放,是去设置contentsize的偏移量来实现画面的更替。
4:page的计算,理解清楚就行.
1 #import "ViewController.h" 2 3 @interface ViewController ()<UIScrollViewDelegate> 4 @property(strong,nonatomic)UIButton *transBtn; 5 @property(strong,nonatomic)UIImageView *imageView; 6 @property(strong,nonatomic)UIScrollView *scrollView; 7 @property(strong,nonatomic)UIPageControl *pageControl; 8 @property(strong,nonatomic)NSTimer *timer; 9 @end 10 11 @implementation ViewController 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 // Do any additional setup after loading the view. 15 [self setUIScrollView]; 16 // self.view.backgroundColor=[UIColor redColor]; 17 } 18 -(void)setUIScrollView{ 19 UIImage *image=[UIImage imageNamed:@"l1.png"]; 20 CGFloat w=image.size.width; 21 CGFloat h=image.size.height; 22 _scrollView=[[UIScrollView alloc]init]; 23 _scrollView.frame=CGRectMake(30, 60, w, h); 24 for(int i= 0;i<5;i++) 25 { 26 NSString *pictName=[NSString stringWithFormat:@"l%d.png",i+1 ]; 27 UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(i*w, 0, w, h)]; 28 imageview.image=[UIImage imageNamed:pictName]; 29 [_scrollView addSubview:imageview]; 30 } 31 _scrollView.contentSize=CGSizeMake(5*w, h); 32 //开启分页功能 33 _scrollView.pagingEnabled=YES; 34 [self.view addSubview: _scrollView]; 35 36 _pageControl=[[UIPageControl alloc ]initWithFrame:CGRectMake(280, 200, 1, 1)]; 37 _pageControl.numberOfPages=5; 38 _pageControl.currentPageIndicatorTintColor=[UIColor blackColor]; 39 _pageControl.pageIndicatorTintColor=[UIColor redColor]; 40 [self.view addSubview:_pageControl]; 41 _scrollView.delegate=self; 42 [self startTimer]; 43 } 44 //开始手动拖拽的时候停止定时器 45 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 46 [self stopTimer]; 47 } 48 //滑动事件,只要滑动超过一半,分页标示就跳到下一页。 49 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 50 int page=(int)(scrollView.contentOffset.x/scrollView.frame.size.width+0.5); 51 _pageControl.currentPage=page; 52 } 53 //下面的两个监听方法是下个页面完整的显示出来了,分页标示才跳到下一个,一般的实际开发都用上面划一半就跳的,这里都写出来了。 54 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 55 if(decelerate) 56 {NSLog(@"已停止拖拽,由于惯性继续滚动");} 57 else 58 {NSLog(@"已停止拖拽,停止滚动"); 59 int page=scrollView.contentOffset.x/scrollView.frame.size.width; 60 _pageControl.currentPage=page; 61 } 62 } 63 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 64 { 65 NSLog(@"减速完毕停止滚动"); 66 int page=scrollView.contentOffset.x/scrollView.frame.size.width; 67 _pageControl.currentPage=page; 68 [self startTimer]; 69 } 70 -(void)startTimer{ 71 _timer= [ NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; 72 73 } 74 -(void)stopTimer{ 75 [_timer invalidate]; 76 _timer=nil; 77 } 78 -(void)nextPage{ 79 80 int page=(int)(_pageControl.currentPage+1); 81 if(page==5) 82 page=0; 83 [ _scrollView setContentOffset:CGPointMake(page*_scrollView.frame.size.width,0) animated:YES]; 84 } 85 @end