iOS自动轮播器

直接上代码 粘贴即可以用
@property(nonatomic,strong)UIScrollView *scrollview;

@property(nonatomic,strong)UIPageControl * pageControl;
@property (nonatomic, strong) NSTimer *timer;
@property(nonatomic,strong)UIImageView * scrollImage;
self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-myWebView.frame.size.height-100, self.view.frame.size.width, 100)];

self.scrollview.delegate = self;
self.scrollview.showsHorizontalScrollIndicator = NO;
self.scrollview.shouldGroupAccessibilityChildren = NO;
self.scrollview.pagingEnabled = YES;
[self.view addSubview:self.scrollview];
self.pageControl =[[UIPageControl alloc]init];
self.pageControl.numberOfPages = 3;
self.pageControl.currentPageIndicatorTintColor = [UIColor clearColor];
self.pageControl.pageIndicatorTintColor = [UIColor clearColor];
[self.view addSubview:self.pageControl];
[self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.scrollview.mas_centerX);
make.centerY.equalTo(self.scrollview.mas_centerY).with.offset(30);
make.height.mas_equalTo(15);
make.width.mas_equalTo(30);
【iOS自动轮播器】}];
//图片的宽
CGFloat imageW = self.scrollview.frame.size.width;
//图片高
CGFloat imageH = self.scrollview.frame.size.height;
//图片的Y
CGFloat imageY = 0;
//图片中数
NSInteger totalCount = 3;
for (int i = 0; i < totalCount; i++) {
self.scrollImage= [[UIImageView alloc] init];
//图片X
CGFloat imageX = i * imageW;
//设置frame
self.scrollImage.frame = CGRectMake(imageX, imageY, imageW, imageH);
//设置图片
self.scrollImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d", i + 1]];
//隐藏指示条
self.scrollview.showsHorizontalScrollIndicator = NO;
[self.scrollview addSubview:self.scrollImage];
}
// 设置scrollview的滚动范围
CGFloat contentW = totalCount *imageW;
//不允许在垂直方向上进行滚动
self.scrollview.contentSize = CGSizeMake(contentW, 0);
//3.设置分页
self.scrollview.pagingEnabled = YES;
//4.监听scrollview的滚动
self.scrollview.delegate = self;
[self addTimer];




// scrollview滚动的时候调用

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//计算页码
CGFloat scrollviewW =scrollView.frame.size.width;
CGFloat x = scrollView.contentOffset.x;
int page = (x + scrollviewW / 2) /scrollviewW;
self.pageControl.currentPage = page;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//关闭定时器(注意点; 定时器一旦被关闭,无法再开启)
[self removeTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//开启定时器
[self addTimer];
}
-(void)addTimer{
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
- (void)nextImage{
int page = (int)self.pageControl.currentPage;
if (page == 2) {
page = 0;
}else
{
page++;
}
//滚动scrollview
CGFloat x = page * self.scrollview.frame.size.width;
self.scrollview.contentOffset = CGPointMake(x, 0);
}
- (void)removeTimer{
[self.timer invalidate];
}

    推荐阅读