您的位置:首页 > 移动开发 > IOS开发

自己封装的轮播工具

2015-12-25 10:43 411 查看
最近闲来无事,刚好自己封装了一个轮播的demo,可以实现设置时间间隔,是否轮播,是否显示indicator等,使用的时候直接设置参数就可以了,再也不用那么的麻烦了。

下面结合代码来阐述一下自己的思路吧,首先有两种模式,可以自动播放和不自动播放两种模式。

-(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  轮播 scrollView iOS