首先说一下实现的思想:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSInteger leftIndex; NSInteger rightIndex; if (scrollView.contentOffset.x == bannerScrollViewW * 2) { ; /** 向左滑 计算 左 中 右 的下标索引*/ leftIndex = self.centerIndex % self.imageNames.count; self.centerIndex = (self.centerIndex+1) % self.imageNames.count; rightIndex = (self.centerIndex +1) % self.imageNames.count; //NSLog(@"往左滑"); }else if (scrollView.contentOffset.x == 0) { /** 向右滑 计算 左 中 右 的下标索引*/ rightIndex = self.centerIndex; self.centerIndex = (self.centerIndex - 1) < 0?(self.imageNames.count - 1):(self.centerIndex - 1); leftIndex = (self.centerIndex - 1) < 0?(self.imageNames.count - 1):(self.centerIndex - 1); //NSLog(@"往右滑"); }else { // 没有滑走 什么都不做,直接return return; } /** 设置图片 */ self.leftImageView.image = [UIImage imageNamed:self.imageNames[leftIndex]]; self.centerImageView.image = [UIImage imageNamed:self.imageNames[self.centerIndex]]; self.rightImageView.image = [UIImage imageNamed:self.imageNames[rightIndex]]; /** 设置pageControl currentPage 因为永远显示中间的图片,故此currentPage=centerIndex */ self.pageControl.currentPage = self.centerIndex; // 将 bannerScrollView 拉回到中间图片的位置 显示图片 [scrollView setContentOffset:CGPointMake(bannerScrollViewW, 0) animated:NO]; }
- (void)swipeGestureRight:(UISwipeGestureRecognizer *)gesture { self.centerIndex = (self.centerIndex+1) % self.imageNames.count; self.centerImageView.image = [UIImage imageNamed:self.imageNames[self.centerIndex]]; CATransition *animation=[CATransition animation]; animation.delegate = self; animation.type = @"push"; animation.subtype = @"fromRight"; animation.duration = 1; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; [self.centerImageView.layer addAnimation:animation forKey:@"cube"]; /** 设置pageControl currentPage 因为永远显示中间的图片,故此currentPage=centerIndex */ self.pageControl.currentPage = self.centerIndex; NSLog(@"right"); } - (void)animationDidStart:(CAAnimation *)anim { self.isAnimation = YES; [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:3]]; } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { self.isAnimation = NO; } - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { return !self.isAnimation; }
完整的代码:http://pan.baidu.com/s/1c1u6NCG
原文:http://www.cnblogs.com/CyanStone/p/5076718.html