博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己写的一个图片轮播器
阅读量:4568 次
发布时间:2019-06-08

本文共 3583 字,大约阅读时间需要 11 分钟。

1 #import "ViewController.h"  2   3 @interface ViewController () 
4 /** 5 * 图片轮播器 6 */ 7 @property (nonatomic ,weak ) UIScrollView *scrView; 8 /** 9 * 页面指示器 10 */ 11 @property (nonatomic, weak ) UIPageControl *pgControl; 12 /** 13 * 计时器 14 */ 15 @property (nonatomic, strong) NSTimer *timer; 16 @end 17 18 @implementation ViewController 19 - (void)viewDidLoad 20 { 21 [super viewDidLoad]; 22 //加载图片轮播器 23 [self setUpScrView]; 24 //加载页面指示器 25 [self setUpPageControl]; 26 //加载定时器 27 [self setUpTimer]; 28 } 29 - (void)setUpScrView 30 { 31 UIScrollView *scrView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 250)]; 32 scrView.alwaysBounceHorizontal = YES; 33 scrView.showsHorizontalScrollIndicator = NO; 34 scrView.pagingEnabled = YES; 35 scrView.contentSize = CGSizeMake(self.view.frame.size.width * 5, 250); 36 _scrView = scrView; 37 [self setUpImg]; 38 [self.view addSubview:_scrView]; 39 } 40 41 - (void)setUpPageControl 42 { 43 UIPageControl *pgControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)]; 44 pgControl.center = CGPointMake(CGRectGetMidX(self.view.bounds), 300); 45 pgControl.currentPage = 0; 46 pgControl.numberOfPages = 5; 47 pgControl.currentPageIndicatorTintColor = [UIColor redColor]; 48 pgControl.pageIndicatorTintColor = [UIColor greenColor]; 49 _pgControl = pgControl; 50 [self.view addSubview:_pgControl]; 51 } 52 53 - (void)scrollImage 54 { 55 NSInteger page = _pgControl.currentPage; 56 if (page == _pgControl.numberOfPages - 1) { 57 page = 0; 58 } else { 59 page ++; 60 } 61 CGFloat offsetX = page * _scrView.frame.size.width; 62 [_scrView setContentOffset:CGPointMake(offsetX, 0) animated:YES]; 63 64 _pgControl.currentPage = page; 65 } 66 67 - (void)setUpTimer 68 { 69 _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES]; 70 NSRunLoop *runloop = [NSRunLoop currentRunLoop]; 71 [runloop addTimer:_timer forMode:NSRunLoopCommonModes]; 72 } 73 74 - (void)setUpImg 75 { 76 CGFloat imgW = self.view.bounds.size.width; 77 CGFloat imgH = 250; 78 CGFloat imgY = 0; 79 for (int i = 0; i < 5; i ++) { 80 CGFloat imgX = imgW * i; 81 UIImageView *imgView = [[UIImageView alloc] init]; 82 imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%02d", i + 1]]; 83 imgView.frame = CGRectMake(imgX, imgY, imgW, imgH); 84 [_scrView addSubview:imgView]; 85 } 86 } 87 88 #pragma mark UIScrollView代理方法 89 - (void)scrollViewDidScroll:(UIScrollView *)scrollView 90 { 91 CGFloat offsetX = _scrView.contentOffset.x; 92 offsetX += _scrView.frame.size.width / 2; 93 NSInteger page = offsetX / _scrView.frame.size.width; 94 _pgControl.currentPage = page; 95 } 96 97 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 98 { 99 [_timer invalidate];100 _timer = nil;101 }102 103 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate104 {105 [self setUpTimer];106 }107 108 @end

 

转载于:https://www.cnblogs.com/xiayao/p/5262800.html

你可能感兴趣的文章
jquery制作图片瀑布流点击按钮加载更多内容
查看>>
artice与section的区别
查看>>
多租户SaaS的数据库设计模式
查看>>
和小哥哥一起刷洛谷(8) 图论之Floyd“算法”
查看>>
配置Spring
查看>>
bash 参数替换中的模式匹配
查看>>
DLog的使用
查看>>
使用第三方框架 Masonry 实现自动布局
查看>>
简明Linux命令行笔记:bzip2
查看>>
电子科大春季体验营 (都是思维题。。。。)
查看>>
Python - pandas 数据分析
查看>>
导航特效
查看>>
HTTP协议分析及攻防方法
查看>>
编程我们学到了什么?
查看>>
面向过程和面向对象的对比(转)
查看>>
206. 反转链表
查看>>
622. 设计循环队列
查看>>
MCMC 、抽样算法与软件实现
查看>>
Java开源工具 网站开发工具清单
查看>>
POJ 1442 Black Box
查看>>