码迷,mamicode.com
首页 > 移动开发 > 详细

【学习ios之路:UI系列】实现轮播图效果(UIImageView,UIScrollView,UIPageControl,NSTimer相结合)

时间:2015-01-29 22:37:59      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:

 实现效果,在不点击的情况下,自定滚动,点击时,停止.如下图

技术分享

部分代码如下:

//调用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相结合)

标签:

原文地址:http://blog.csdn.net/zfx5130/article/details/43278285

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!