Objective-C无限循环轮播器的实现方法
来源:爱站网时间:2021-01-29编辑:网友分享
无限循环轮播器是可以帮我们我们轮播广告的工具,但是也有很多用户们不知道如何实现,那么下文中我们就一起去看看Objective-C无限循环轮播器的实现方法。
无限循环轮播器是可以帮我们我们轮播广告的工具,但是也有很多用户们不知道如何实现,那么下文中我们就一起去看看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
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 ()@property(weak, nonatomic) UIScrollView * scrollView; @property(weak, nonatomic) NSTimer * timer; /** pageIndex */ @property(nonatomic, assign) NSInteger pageIndex; @end @implementation SNInfiniteScrollView - (void)setImages:(NSArray *)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 = 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 - - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 找出最中间的那个图片控件 NSInteger page = self.pageIndex; CGPoint point = CGPointZero; for (int i = 0; i *)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
文中主要介绍的就是Objective-C无限循环轮播器的实现方法,这些方法都非常简单快捷,是不是觉得很容易呢。