Objective-C实现无限循环轮播器

 更新时间:2016年05月19日 16:05:14   作者:zhangmi  
这篇文章主要介绍了Objective-C实现无限循环轮播器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

先看看效果图:

具体实现代码:

1. 控制器    

//
// AppDelegate.m
// 无限轮播器
//
// Created by zhangmi on 16/5/16.
// Copyright © 2016年 Paramount Pictures. All rights reserved.
//
#import "ViewController.h"
#import "SNInfiniteScrollView.h"
 
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
  
 NSMutableArray * images = [NSMutableArray array];
 for (int i = 0; i < 5; i++) {
  NSString * imageName = [NSString stringWithFormat:@"ad_%02d", i]; //img01
  UIImage * image = [UIImage imageNamed:imageName];
  [images addObject:image];
 }
 UIView * scrollView = [SNInfiniteScrollView scrollViewWithFrame:CGRectMake(0, 20, 414, 200) superView:self.view images:images scrollDirection:ScrollDirectionHorizontal pageIndicatorTintColor:[UIColor lightGrayColor] currentPageIndicatorTintColor:[UIColor orangeColor] imageViewcontentMode:UIViewContentModeScaleAspectFit];
  
 [self.view addSubview:scrollView];
}
 
@end

2. 显示内容界面设置

//
// AppDelegate.m
// 无限轮播器
//
// Created by zhangmi on 16/5/16.
// Copyright © 2016年 Paramount Pictures. All rights reserved.
//
 
#import "SNInfiniteScrollView.h"
 
static int const ImageViewCount = 3;
#define scrollViewWidth self.scrollView.frame.size.width
#define scrollViewHeight self.scrollView.frame.size.height
 
@interface SNInfiniteScrollView () <UIScrollViewDelegate>
 
@property(weak, nonatomic) UIScrollView * scrollView;
@property(weak, nonatomic) NSTimer * timer;
/** pageIndex */
@property(nonatomic, assign) NSInteger pageIndex;
 
@end
 
@implementation SNInfiniteScrollView
 
- (void)setImages:(NSArray<UIImage *> *)images {
  
 _images = images;
  
 // 设置页码
 self.pageIndex = 0;
  
 // 设置内容
 [self updateContent];
  
 // 开始定时器
 [self startTimer];
}
 
/** 代码创建的时候调用. */
- (instancetype)initWithFrame:(CGRect)frame {
 if (self = [super initWithFrame:frame]) {
  // 滚动视图
  UIScrollView * scrollView = [[UIScrollView alloc] init];
   
  self.scrollView = scrollView;
  scrollView.delegate = self;
  // scroller属性
  scrollView.showsHorizontalScrollIndicator = NO;
  scrollView.showsVerticalScrollIndicator = NO;
  scrollView.pagingEnabled = YES;
  scrollView.bounces = NO;
  // 添加scrollView
  [self addSubview:scrollView];
   
  // 图片控件
  for (int i = 0; i < ImageViewCount; i++) {
   UIImageView * imageView = [[UIImageView alloc] init];
   // 图片不变形处理.
   imageView.contentMode = self.imageViewcontentMode;
   [scrollView addSubview:imageView];
  }
 }
 return self;
}
/** 布局 子控件, 只执行一次 */
- (void)layoutSubviews {
 [super layoutSubviews];
  
 self.scrollView.frame = self.bounds;
 if (self.scrollDirection == ScrollDirectionVertical) {
  self.scrollView.contentSize = CGSizeMake(0, ImageViewCount * self.bounds.size.height);
 } else {
  self.scrollView.contentSize = CGSizeMake(ImageViewCount * self.bounds.size.width, 0);
 }
  
 for (int i = 0; i < ImageViewCount; i++) {
  UIImageView * imageView = self.scrollView.subviews[i];
   
  if (self.scrollDirection == ScrollDirectionVertical) {
   imageView.frame = CGRectMake(0, i * self.scrollView.frame.size.height, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
  } else {
   imageView.frame = CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
  }
 }
 // 设置内容
 [self updateContent];
}
#pragma mark - 内容更新
- (void)updateContent {
 // 设置图片
 for (int i = 0; i < self.scrollView.subviews.count; i++) {
   
  NSInteger pageIndex = self.pageIndex;
  // 遍历每一个imageView
  UIImageView * imageView = self.scrollView.subviews[i];
   
  if (i == 0) {
   pageIndex--;
  } else if (i == 2) {
   pageIndex++;
  }
   
  if (pageIndex < 0) {
   pageIndex = self.images.count - 1;
  } else if (pageIndex >= self.images.count) {
   pageIndex = 0;
  }
  // 图片角标 赋值给 imageView的tag
  imageView.tag = pageIndex;
  imageView.image = self.images[imageView.tag];
 }
  
 // 设置偏移量在中间 // 不能使用带动画 contentOffset
 if (self.scrollDirection == ScrollDirectionVertical) {
  self.scrollView.contentOffset = CGPointMake(0, scrollViewHeight);
 } else {
  self.scrollView.contentOffset = CGPointMake(scrollViewWidth, 0);
 }
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
 // 找出最中间的那个图片控件
 NSInteger page = self.pageIndex;
 CGPoint point = CGPointZero;
 for (int i = 0; i < self.scrollView.subviews.count; i++) {
  UIImageView * imageView = self.scrollView.subviews[i];
  point = [scrollView convertPoint:imageView.frame.origin toView:self.superview];
  //=****** other way ****************** stone ***
  if (self.scrollDirection == ScrollDirectionVertical) {
   if (ABS(point.y - self.frame.origin.y) < 1.0) {
    page = imageView.tag;
   }
  } else {
   if (ABS(point.x - self.frame.origin.x) < 1.0) {
    page = imageView.tag;
   }
  }
 }
 self.pageIndex = page;
 self.pageControl.currentPage = page;
  
 //拖动结束会调用 [self updateContent];
 //#warning mark - 没有动画正常 , 有动画不动, 一直是原点
 // [self updateContent]; // 没有动画正常 , 有动画不动, 一直是原点
}
/** 开始拖拽 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
 // 停止定时器
 [self stopTimer];
}
/** 结束拖拽 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
 // 开启定时器
 [self startTimer];
}
/** 减速完毕 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
 // 更新内容 , 如果contentOffset 不带动画的话 不走这个方法
 [self updateContent];
}
/** 结束滚动动画 */ // 这是保险的做法吧... 如果contentOffset 不带动画的话 不走这个方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
 // 更新内容
 [self updateContent];
}
 
#pragma mark - 定时器处理
- (void)startTimer {
  
 NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(next:) userInfo:nil repeats:YES];
 // [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
 [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
 self.timer = timer;
}
 
- (void)stopTimer {
 [self.timer invalidate];
 self.timer = nil;
}
 
- (void)next:(NSTimer *)timer {
 if (self.scrollDirection == ScrollDirectionVertical) {
  [self.scrollView setContentOffset:CGPointMake(0, 2 * self.scrollView.frame.size.height) animated:YES];
 } else {
  [self.scrollView setContentOffset:CGPointMake(2 * self.scrollView.frame.size.width, 0) animated:YES];
 }
}
//=****** 简单调用 ****************** stone ***
+ (instancetype)scrollViewWithFrame:(CGRect)frame superView:(UIView *)superView images:(NSArray<UIImage *> *)images scrollDirection:(ScrollDirection)scrollDirection pageIndicatorTintColor:(UIColor *)pageIndicatorTintColor currentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor imageViewcontentMode:(UIViewContentMode)imageViewcontentMode {
  
 //=****** 添加自定义scrollView ****************** stone ***
 SNInfiniteScrollView * scrollView = [[SNInfiniteScrollView alloc] init];
 scrollView.frame = frame;
 scrollView.imageViewcontentMode = imageViewcontentMode;
 scrollView.scrollDirection = scrollDirection;
 //=****** 添加image ****************** stone ***
 scrollView.images = images;
 //=****** 添加pageControl ****************** stone ***
 UIPageControl * pageControl = [[UIPageControl alloc] init];
 scrollView.pageControl = pageControl;
 pageControl.enabled = NO;
 pageControl.currentPageIndicatorTintColor = currentPageIndicatorTintColor;
 pageControl.pageIndicatorTintColor = pageIndicatorTintColor;
 pageControl.numberOfPages = scrollView.images.count;
 pageControl.bounds = CGRectMake(0, 0, scrollView.bounds.size.width, 44);
 pageControl.center = CGPointMake(scrollView.bounds.size.width * 0.5, scrollView.bounds.size.height * 0.9);
 [scrollView addSubview:pageControl];
 [superView addSubview:scrollView];
 //=************************ stone ***
 return scrollView;
}
 
@end

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关文章

  • iOS多控制器实现带滑动动画

    iOS多控制器实现带滑动动画

    这篇文章主要为大家详细介绍了iOS多控制器实现带滑动动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • 谈谈XCode9的新变化

    谈谈XCode9的新变化

    这篇文章主要介绍了谈谈XCode9的新变化,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • iOS高仿微信相册界面翻转过渡动画效果

    iOS高仿微信相册界面翻转过渡动画效果

    在图片界面点击右下角的查看评论会翻转到评论界面,评论界面点击左上角的返回按钮会反方向翻转回图片界面,真正的实现方法,与传统的导航栏过渡其实只有一行代码的区别,下面小编通过本文给大家介绍下ios高仿微信相册界面翻转过渡动画效果,一起看看吧
    2016-11-11
  • iOS Swift Lazy var View失效问题解决

    iOS Swift Lazy var View失效问题解决

    这篇文章主要为大家介绍了iOS Swift Lazy var View失效问题解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 详解iOS中多倒计时场景的解决方案

    详解iOS中多倒计时场景的解决方案

    在我们开发APP的过程中,或多或少都遇到过需要使用倒计时的场景,这篇文章主要介绍了详解iOS中多倒计时场景的解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • iOS开发中UITabBarController的使用示例

    iOS开发中UITabBarController的使用示例

    这篇文章主要介绍了iOS开发中UITabBarController的使用示例,代码基于Objective-C进行演示,需要的朋友可以参考下
    2015-09-09
  • iOS推送增加右侧显示图Service Extension

    iOS推送增加右侧显示图Service Extension

    这篇文章主要为大家介绍了iOS推送增加右侧显示图Service Extension,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • iOS 隐藏tabbar代码详解

    iOS 隐藏tabbar代码详解

    这篇文章主要介绍了iOS 隐藏tabbar代码详解的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • iOS 解决按钮背景图片拉伸问题(推荐)

    iOS 解决按钮背景图片拉伸问题(推荐)

    这篇文章主要介绍了iOS 解决按钮背景图片拉伸问题(推荐),需要的朋友可以参考下
    2017-10-10
  • iOS实现波浪效果

    iOS实现波浪效果

    这篇文章主要为大家详细介绍了iOS实现波浪效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05

最新评论