自己封装的轮播工具
2015-12-25 10:43
411 查看
最近闲来无事,刚好自己封装了一个轮播的demo,可以实现设置时间间隔,是否轮播,是否显示indicator等,使用的时候直接设置参数就可以了,再也不用那么的麻烦了。
下面结合代码来阐述一下自己的思路吧,首先有两种模式,可以自动播放和不自动播放两种模式。
自动播放的时候,自己添加了一个定时器,来循环的播放,另外在自动播放的时候还可以滑动来实现切换页面,但是以前做的时候这个问题是没有注意到的,后来发现两者会有冲突,总感觉用手滑动的时候效果非常的阻塞,不是那么的流畅,仔细分析了一下,发现时定时器出发的滚动和用手拖拽实现的滚动同时触发了,造成了这种现象。我在这里优先用户用手拖拽的效果。因为用户拖拽的话,肯定是想快速的看到自己想看的东西。
在这里我在UIScrollview的代理方法里加了一个标签开关。isUserDragged =YES;
当run方法被触发时,默认用户没有拖拽。isUserDragged =NO;
这样的话,其实有两种模式,定时器轮播,用户手动切换页面。
下面来看看动画效果吧
demo下载地址如下:
demo
下面结合代码来阐述一下自己的思路吧,首先有两种模式,可以自动播放和不自动播放两种模式。
-(void)configureScrollPlayer{ [self backToOriginStatus]; if (automaticScroll) { [self configureWithAutomaticScroll]; }else{ [self configureWithNoAutomaticScroll]; } }
自动播放的时候,自己添加了一个定时器,来循环的播放,另外在自动播放的时候还可以滑动来实现切换页面,但是以前做的时候这个问题是没有注意到的,后来发现两者会有冲突,总感觉用手滑动的时候效果非常的阻塞,不是那么的流畅,仔细分析了一下,发现时定时器出发的滚动和用手拖拽实现的滚动同时触发了,造成了这种现象。我在这里优先用户用手拖拽的效果。因为用户拖拽的话,肯定是想快速的看到自己想看的东西。
在这里我在UIScrollview的代理方法里加了一个标签开关。isUserDragged =YES;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [timer invalidate]; isUserDragged =YES; } - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{ float playerWidth =mainScroll.frame.size.width; float lastTargetOffset =pageIndex *playerWidth; NSInteger page =(targetContentOffset->x - lastTargetOffset)/playerWidth; pageIndex+=page; // NSLog(@"pageIndex((((((((((((( %ld",(long)pageIndex); // NSLog(@"pageIndex((((((((((((( %ld",(long)pageIndex); UIButton *button =(UIButton *)[self viewWithTag:(101+pageIndex)]; [self pageIndicatorClicked:button]; timer =[NSTimer scheduledTimerWithTimeInterval:scrollTimeInterval target:self selector:@selector(run) userInfo:nil repeats:YES]; }
当run方法被触发时,默认用户没有拖拽。isUserDragged =NO;
-(void)run{ isUserDragged =NO; pageIndex++; UIButton *button =(UIButton *)[self viewWithTag:(100+pageIndex)]; [self pageIndicatorClicked:button]; if (pageIndex==[dataArr count]) { pageIndex=0; } NSLog(@"**pageIndex******%d*********",(int)pageIndex); }
这样的话,其实有两种模式,定时器轮播,用户手动切换页面。
下面来看看动画效果吧
demo下载地址如下:
demo
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- ScrollView滚动条颜色的设置方法
- Bootstrap轮播加上css3动画,炫酷到底!
- js实现图片点击左右轮播
- 基于JavaScript实现仿京东图片轮播效果
- jQuery旋转木马式幻灯片轮播特效
- jquery实现鼠标悬浮停止轮播特效
- jquery实现定时自动轮播特效
- JQuery实现的图文自动轮播效果插件
- Jquery代码实现图片轮播效果(一)
- 刷新Activity中的scrollview示例(局部ui刷新)
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略