【学习ios之路:UI系列】实现轮播图效果(UIImageView,UIScrollView,UIPageControl,NSTimer相结合)
实现效果,在不点击的情况下,自定滚动,点击时,停止.如下图
部分代码如下:
//调用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]; } }
该效果,已经整理成为Demo,下载地址如下:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。