首页 > 代码库 > 【学习ios之路:UI系列】实现轮播图效果(UIImageView,UIScrollView,UIPageControl,NSTimer相结合)
【学习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,下载地址如下:
轮播图效果Demo
【学习ios之路:UI系列】实现轮播图效果(UIImageView,UIScrollView,UIPageControl,NSTimer相结合)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。