实现效果,在不点击的情况下,自定滚动,点击时,停止.如下图
部分代码如下:
//调用NSTimer方法,自定计时 - (void)autoScroll { self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scrollToRight) userInfo:nil repeats:YES]; }
//实现触发方法 - (void)scrollToRight { [UIView animateWithDuration:0.5 animations:^{ //修改滚动图片的偏移量 self.shuffleScroll.contentOffset = CGPointMake(self.shuffleScroll.contentOffset.x + kScreenWidth, 0); } completion:^(BOOL finished) { [self scrollViewDidEndDecelerating:self.shuffleScroll]; finished = YES; }]; }给图片添加手势,处理手势事件
- (void)handleLongPressAction:(UILongPressGestureRecognizer *)longPress { //取消滚动 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoScroll) object:nil]; if (longPress.state == UIGestureRecognizerStateBegan) { [self.timer invalidate]; NSLog(@"定时器无效"); } if (longPress.state == UIGestureRecognizerStateEnded) { [self performSelector:@selector(autoScroll) withObject:nil afterDelay:0.5]; NSLog(@"开启定时器"); } }布局图片.并添加手势处理
- (void)setupImageView { for (int i = 0; i < self.imageArr.count; i++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake (kScreenWidth * i, 0, kScreenWidth, self.frame.size.height)]; imageView.image = self.imageArr[i]; imageView.userInteractionEnabled = YES; [self.shuffleScroll addSubview:imageView]; [imageView release]; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressAction:)]; longPress.minimumPressDuration = 0.01; [imageView addGestureRecognizer:longPress]; [longPress release]; } }
【学习ios之路:UI系列】实现轮播图效果(UIImageView,UIScrollView,UIPageControl,NSTimer相结合)
原文:http://blog.csdn.net/zfx5130/article/details/43278285