欢迎来到天天文库
浏览记录
ID:59254792
大小:500.13 KB
页数:5页
时间:2020-09-08
《iOS开发UI篇—UIScrollView控件实现图片轮播.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、一、实现效果实现图片的自动轮播 二、实现代码storyboard中布局代码:1#import"YYViewController.h"23@interfaceYYViewController()4@property(weak,nonatomic)IBOutletUIScrollView*scrollview;5/**6*页码7*/8@property(weak,nonatomic)IBOutletUIPageControl*pageControl;910@property(nonatomic,s
2、trong)NSTimer*timer;11@end1213@implementationYYViewController1415-(void)viewDidLoad16{17[superviewDidLoad];1819//图片的宽20CGFloatimageW=self.scrollview.frame.size.width;21//CGFloatimageW=300;22//图片高23CGFloatimageH=self.scrollview.frame.size.height;24//图片的Y25CGFloatimageY=0;26//图
3、片中数27NSIntegertotalCount=5;28//1.添加5张图片29for(inti=0;i4、0%d",i+1];37imageView.image=[UIImageimageNamed:name];38//隐藏指示条39self.scrollview.showsHorizontalScrollIndicator=NO;4041[self.scrollviewaddSubview:imageView];42}4344//2.设置scrollview的滚动范围45CGFloatcontentW=totalCount*imageW;46//不允许在垂直方向上进行滚动47self.scrollview.contentSize=CGSizeMak5、e(contentW,0);4849//3.设置分页50self.scrollview.pagingEnabled=YES;5152//4.监听scrollview的滚动53self.scrollview.delegate=self;5455[selfaddTimer];56}5758-(void)nextImage59{60intpage=(int)self.pageControl.currentPage;61if(page==4){62page=0;63}else64{65page++;66}6768//滚动scrollview69CGFlo6、atx=page*self.scrollview.frame.size.width;70self.scrollview.contentOffset=CGPointMake(x,0);71}7273//scrollview滚动的时候调用74-(void)scrollViewDidScroll:(UIScrollView*)scrollView75{76NSLog(@"滚动中");77//计算页码78//页码=(contentoffset.x+scrollView一半宽度)/scrollView宽度79CGFloatscrollviewW=scrol7、lView.frame.size.width;80CGFloatx=scrollView.contentOffset.x;81intpage=(x+scrollviewW/2)/scrollviewW;82self.pageControl.currentPage=page;83}8485//开始拖拽的时候调用86-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView87{88//关闭定时器(注意点;定时器一旦被关闭,无法再开启)89//[self.timerinvalidate];8、90[selfremoveTimer];91}9293-(void)scrollViewDidEndDragging:(UIScroll
4、0%d",i+1];37imageView.image=[UIImageimageNamed:name];38//隐藏指示条39self.scrollview.showsHorizontalScrollIndicator=NO;4041[self.scrollviewaddSubview:imageView];42}4344//2.设置scrollview的滚动范围45CGFloatcontentW=totalCount*imageW;46//不允许在垂直方向上进行滚动47self.scrollview.contentSize=CGSizeMak
5、e(contentW,0);4849//3.设置分页50self.scrollview.pagingEnabled=YES;5152//4.监听scrollview的滚动53self.scrollview.delegate=self;5455[selfaddTimer];56}5758-(void)nextImage59{60intpage=(int)self.pageControl.currentPage;61if(page==4){62page=0;63}else64{65page++;66}6768//滚动scrollview69CGFlo
6、atx=page*self.scrollview.frame.size.width;70self.scrollview.contentOffset=CGPointMake(x,0);71}7273//scrollview滚动的时候调用74-(void)scrollViewDidScroll:(UIScrollView*)scrollView75{76NSLog(@"滚动中");77//计算页码78//页码=(contentoffset.x+scrollView一半宽度)/scrollView宽度79CGFloatscrollviewW=scrol
7、lView.frame.size.width;80CGFloatx=scrollView.contentOffset.x;81intpage=(x+scrollviewW/2)/scrollviewW;82self.pageControl.currentPage=page;83}8485//开始拖拽的时候调用86-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView87{88//关闭定时器(注意点;定时器一旦被关闭,无法再开启)89//[self.timerinvalidate];
8、90[selfremoveTimer];91}9293-(void)scrollViewDidEndDragging:(UIScroll
此文档下载收益归作者所有