当前位置: 首页 > news >正文

ios开发图片轮播器以及定时器小问题

一:图片轮播器效果如图:能实现自动轮播,到最后一页时,轮播回来,可以实现拖拽滚动

 

二:代码:

  1 #import "ViewController.h"
  2 static CGFloat const imageCount = 5;
  3 @interface ViewController ()<UIScrollViewDelegate>
  4 /*scrollView*/
  5 @property (nonatomic,weak)UIScrollView *scroll;
  6 /*pageControl*/
  7 @property (nonatomic,weak)UIPageControl *pageControl;
  8 /*timer*/
  9 @property (nonatomic ,strong)NSTimer *timer;
 10 @property (nonatomic,assign)int  a;
 11 @end
 12 
 13 @implementation ViewController
 14 
 15 - (void)viewDidLoad {
 16     [super viewDidLoad];
 17     
 18     //0:创建底部视图
 19     UIView *bottomView = [[UIView alloc]init];
 20     bottomView.frame = CGRectMake(20, 100, self.view.frame.size.width - 40, 200);
 21     [self.view addSubview:bottomView];
 22     
 23     
 24     //1:创建滚动视图
 25     UIScrollView *scrollView = [[UIScrollView alloc]init];
 26     scrollView.frame  = bottomView.bounds;
 27     scrollView.bounces = NO;
 28     scrollView.showsVerticalScrollIndicator = NO;
 29     scrollView.showsHorizontalScrollIndicator = NO;
 30     scrollView.pagingEnabled = YES;
 31     scrollView.delegate = self;
 32     scrollView.backgroundColor = [UIColor redColor];
 33     self.scroll = scrollView;
 34     [bottomView addSubview:scrollView];
 35     
 36     //2:创建ImageView
 37     CGFloat imageWidth = scrollView.frame.size.width;
 38     CGFloat imageHeight = scrollView.frame.size.height;
 39     
 40     for (int i = 0; i < imageCount; i++) {
 41         
 42         UIImageView *imageView = [[UIImageView alloc]init];
 43         imageView.frame = CGRectMake(i *imageWidth, 0, imageWidth, imageHeight);
 44         imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_0%d.png",i+1]];
 45         [scrollView addSubview:imageView];
 46         
 47     }
 48     
 49     //3:设置scroll的contentSize
 50     scrollView.contentSize = CGSizeMake(imageCount *imageWidth, 0);
 51     
 52     
 53     //4:创建pageControl
 54     UIPageControl *pageControl = [[UIPageControl alloc]init];
 55     pageControl.numberOfPages = imageCount;
 56     pageControl.currentPage = 0;
 57     pageControl.pageIndicatorTintColor = [UIColor whiteColor];
 58     pageControl.currentPageIndicatorTintColor = [UIColor redColor];
 59     pageControl.center = CGPointMake(bottomView.frame.size.width *0.5, bottomView.frame.size.height *0.9);
 60     self.pageControl = pageControl;
 61     [bottomView addSubview:pageControl];
 62     
 63     //5:添加定时器
 64     NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
 65     NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
 66     /*
 67      forMode的参数:
 68      NSDefaultRunLoopMode
 69      NSRunLoopCommonModes
 70      */
 71     self.timer = timer;
 72     [runLoop addTimer:timer forMode: NSRunLoopCommonModes];
 73 //    [timer fire];
 74     
 75     
 76     //6:添加UITextView
 77     UITextView *textView = [[UITextView alloc]init];
 78     textView.frame = CGRectMake(0, 320, self.view.frame.size.width, 150);
 79     textView.text = @"凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo凡事看大佛我阿萨德fo一我啊搜ID念佛爱上你都放那艘IDif完加强覅进去我ijefo";
 80     textView.scrollEnabled = YES;
 81     textView.backgroundColor = [UIColor redColor];
 82     [self.view addSubview:textView];
 83 }
 84 
 85 - (void)changeImage {
 86     
 87     NSInteger page = self.pageControl.currentPage;
 88 
 89     
 90     
 91     if (page == imageCount -1) {
 92         self.a = imageCount -1;
 93     }
 94     
 95     
 96     if (page == 0) {
 97         self.a = 0;
 98     }
 99     
100 
101     if (self.a == imageCount -1) {
102         
103         page -- ;
104        
105     }else {
106         
107         page ++;
108     }
109     
110 
111 //    [self.scroll setContentOffset:CGPointMake(page *self.scroll.frame.size.width, 0) animated:YES];
112     [UIView animateWithDuration:1.0 animations:^{
113         self.scroll.contentOffset = CGPointMake(page *self.scroll.frame.size.width, 0);
114     }];
115     
116 }
117 
118 - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
119     
120         //当下一页滑到>=0.5宽度时,此时就要改变pageControl
121         CGFloat page = scrollView.contentOffset.x / scrollView.frame.size.width;
122         NSUInteger currentPage = page;
123         self.pageControl.currentPage = (page - currentPage) < 0.5 ? currentPage : currentPage +1;
124 }
125 
126 //拖拽开始时关闭定时器,定时器一旦关闭,就不能再重新开启
127 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
128     [self.timer invalidate];
129 
130 }
131 
132 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
133     
134     self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
135 }
136 
137 
138 @end

三:知识点总结

1:创建定时器: 1:NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];此方法创建的定时器,必须加到NSRunLoop中,NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; [runLoop addTimer:timer forMode: NSRunLoopCommonModes];第二个参数有两种类型可供选择: forMode的参数: NSDefaultRunLoopMode  NSRunLoopCommonModes,第一个参数为默认参数,当下面有textView,textfield等控件时,拖拽控件,此时轮播器会停止轮播,因为NSRunLoop的原因,NSRunLoop为一个死循环,实时监测有无事件响应,textView或是textField的事件会影响图片轮播,使用第二个参数会解决这个问题 2: self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];此种创建定时器的方式,默认加到了runloop,且默认为第二个参数。3:定时器方法:[timer fire];此时会立刻执行定时器的方法,若是没有调用此方法,则会等待2秒执行定时器的方法。

2:轮播器的拖拽:在拖拽轮播器时,不影响轮播效果,实现滚动代理的开始拖拽方法,并关闭定时器[self.timer invalidate];定时器一旦被关闭,则不能再重新开启,需要重新创建定时器。在拖拽结束的代理中,再重新创建定时器

3:图片滚动超过宽度的0.5时的计算方法:四舍五入CGFloat page = scrollView.contentOffset.x / scrollView.frame.size.width; NSUInteger currentPage = page; self.pageControl.currentPage = (page - currentPage) < 0.5 ? currentPage : currentPage +1;先求出滚动的浮点数,在转化成整数,做差值,小于0.5的取当前的整数,大于0.5的取当前整数加1

4:设置scrollView的偏移量:1:UIView的动画  2:[self.scroll setContentOffset:CGPointMake(page *self.scroll.frame.size.width, 0) animated:YES];第二种方法,滚动的时间很短,时间小于1秒,第一种就是可以控制动画的时间,和完成动画的回调

5:设置轮播效果:

    NSInteger page = self.pageControl.currentPage;

    if (page == imageCount -1) {

        self.a = imageCount -1;

    }

    if (page == 0) {

        self.a = 0;

    }

 

    if (self.a == imageCount -1) {

        

        page -- ;

       

    }else {

        

        page ++;

    }

注意:不要在此方法中,设置pageControl的currentPage,因为如果设置了,会立刻偏移到相应的位置,但是此时还在实时监测scrollView的滚动,在此滚动代理中,小于宽度一半时,会取当前的整数页,大于时会取整数+1页,所以此时pageControl会立刻到某页,在回到-1页,再突然回到某页,存在bug。不要设置pageControl的currentPage,让其直接在滚动代理中去设置。

转载于:https://www.cnblogs.com/cqb-learner/p/5734995.html

相关文章:

  • Ubuntu里面软件的安装与卸载
  • ubuntu 设置DNS
  • jquery ajax 传数据到后台乱码的处理方法
  • CSS样式
  • NuGet 学习笔记(1)--Nuget安装使用
  • Part5核心初始化_lesson2---设置svc模式
  • 几个常用的CSS3样式代码以及不兼容的解决办法
  • 报个到
  • iOS: NSArray的方法arrayByAddingObjectsFromArray:
  • excel转化为Json
  • dispatch_after 导致controller没有及时释放
  • poj 2763: [JLOI2011]飞行路线(spfa分层图最短路)
  • uboot 第三天学习
  • 数学概念的理解
  • 深入浅出UML类图(一)
  • 10个确保微服务与容器安全的最佳实践
  • 4. 路由到控制器 - Laravel从零开始教程
  • angular学习第一篇-----环境搭建
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • Java Agent 学习笔记
  • orm2 中文文档 3.1 模型属性
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • windows下使用nginx调试简介
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 复习Javascript专题(四):js中的深浅拷贝
  • 高度不固定时垂直居中
  • 和 || 运算
  • 记一次用 NodeJs 实现模拟登录的思路
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 面试总结JavaScript篇
  • 手写一个CommonJS打包工具(一)
  • C# - 为值类型重定义相等性
  • ionic异常记录
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • ​七周四次课(5月9日)iptables filter表案例、iptables nat表应用
  • (14)Hive调优——合并小文件
  • (done) NLP “bag-of-words“ 方法 (带有二元分类和多元分类两个例子)词袋模型、BoW
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)springboot人体健康检测微信小程序 毕业设计 012142
  • (十一)手动添加用户和文件的特殊权限
  • (一)为什么要选择C++
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (转)c++ std::pair 与 std::make
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • (转)memcache、redis缓存
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • (转载)Linux网络编程入门
  • .describe() python_Python-Win32com-Excel
  • .NET 5种线程安全集合
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .net 后台导出excel ,word
  • .NET业务框架的构建