ios ScrollerView之图片轮播器
2015-08-03 15:20
281 查看
ios ScrollerView之图片轮播器
今天项目中用到了图片轮播器,写完之后贴到博客里来记录一下,也方便有兴趣的同学学习#import "JYHCarouselController.h" @interface JYHCarouselController ()<UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @property (strong, nonatomic) NSTimer *timer; @end @implementation JYHCarouselController - (void)viewDidLoad { [super viewDidLoad]; NSArray *array = @[@"img_01",@"img_02",@"img_03"]; [self imageCarouselWithimageArray:array]; } - (void) imageCarouselWithimageArray:(NSArray *)imageArray { //图片的宽度和X值 CGFloat imageW = self.scrollView.frame.size.width; CGFloat imageH = self.scrollView.frame.size.height; CGFloat imageY = 0; //将图片加入到scrollerView中 for(int i = 0; i < imageArray.count; i++) { UIImageView *imageView = [[UIImageView alloc]init]; //设置imageView的frame CGFloat imageX = i * imageW; imageView.frame = CGRectMake(imageX, imageY, imageW, imageH); //设置显示图片 NSString *imageName = imageArray[i]; imageView.image = [UIImage imageNamed:imageName]; //将图片加入到scrollView中 [self.scrollView addSubview:imageView]; } //设置内容尺寸 self.scrollView.contentSize = CGSizeMake(imageArray.count * imageW, 0); //隐藏水平滚动条且分页 self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.pagingEnabled = YES; self.scrollView.delegate = self; //页面指示器的总页数 self.pageControl.numberOfPages = imageArray.count; //添加定时器 [self addTimer]; } - (void)addTimer{ self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES]; //将定时器添加到当前的运行循环中(不添加会导致拖动其他控件时图片轮播器会停止) [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } -(void)nextImage { //增加页面指示器的页码 long page = 0; if(self.pageControl.currentPage == self.pageControl.numberOfPages - 1) { page = 0; } else { page = self.pageControl.currentPage + 1; } //让scrollView滚动到正确位置 CGPoint offset = CGPointMake(page * self.scrollView.frame.size.width, 0); [self.scrollView setContentOffset:offset animated:YES]; } //当scrollView滚动时调用 -(void)scrollViewDidScroll:(UIScrollView *)scrollView { // 根据scrollView的滚动位置决定pageControl显示第几页 CGFloat scrollW = scrollView.frame.size.width; int page = (scrollView.contentOffset.x + scrollW * 0.5) / scrollW; self.pageControl.currentPage = page; } //当scrollView开始拖动时调用 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [self removeTimer]; } //移除定时器 - (void)removeTimer { [self.timer invalidate];//定时器一旦停止就废了 self.timer = nil; } //当scrollView停止拖动时调用 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [self addTimer]; } @end
相关文章推荐
- iOS开发中tableview中cell分隔线与左右的距离
- IOS之GCD记录
- IOS8 ARM64下奇怪的崩溃问题
- 调试iOS 已经发布代码 Crash 文件分析出出错对应代码
- 定制iOS 7中的导航栏和状态栏
- 关于iOS的respondsToSelector等的相关使用小结
- iOS 字符属性NSAttributedString描述
- IOS9使用的SSL/TLS协议运行机制及概述
- iOS开发之在地图上绘制出你运行的轨迹
- iOS开发中tableview中cell图片大小自定义方法
- NSAttributedString的NSUnderlineStyleSingle在iOS8.1上无效的解决方法
- IOS 开发xcode 6.3以后 自制FrameWork教程
- iOS开发中tableview中cell图片自定义方法。
- IOS中几种数据持久化方案,我要永远记住你!
- ios自动布局
- 【IOS】仿QQ侧滑菜单
- iOS高级-QuartzCore框架-CoreAnimation
- IOS第六天(3:scrollView 图片轮播器)
- iOS开发——加载、滑动翻阅大量图片解决方案详解
- IOS第六天(2:10秒倒计时)