iOS之scrollView分页无限滚动框架

好多APP都有首次启动显示内容介绍的滑动页面,或者进入APP后界面上部显示的广告栏,那么这些控件的代码编写是不是很繁琐,不怕!今天给大家介绍我写的一套框架,可以直接拿来用,分分钟实现一个无限滚动的广告栏,如下图

iOS之scrollView分页无限滚动框架_第1张图片
iOS之scrollView分页无限滚动框架_第2张图片

•新创建一个UIView类

.h文件

#import 

@interface GDGInfiniteScrollView : UIView

@property(strong,nonatomic) NSArray * images;
@property(weak,nonatomic,readonly)UIPageControl * pageControl;
@property(assign,nonatomic,getter=isScrollDirectionPortrait)BOOL scrollDirectionPortrait;//滚动方向的图片

@end

.m文件

#import "GDGInfiniteScrollView.h"
static int const ImageViewCount = 3;

@interface GDGInfiniteScrollView()<UIScrollViewDelegate>
@property(weak,nonatomic)UIScrollView * scrollView;
@property(weak,nonatomic)NSTimer * timer;

@end
@implementation GDGInfiniteScrollView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //滚动视图
        UIScrollView * scrollView = [[UIScrollView alloc]init];
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.delegate = self;
        [self addSubview:scrollView];
        self.scrollView = scrollView;

        //图片控件
        for (int i = 0; i UIImageView * imageView = [[UIImageView alloc]init];
            [scrollView addSubview:imageView];
        }
        //页码视图
        UIPageControl * pageControl = [[UIPageControl alloc]init];
        [self addSubview:pageControl];
        _pageControl = pageControl;



    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];

    self.scrollView.frame = self.bounds;
    if (self.isScrollDirectionPortrait) {
        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; iUIImageView * imageView = self.scrollView.subviews[i];
        if (self.isScrollDirectionPortrait) {
            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);
        }
    }

    CGFloat pageW = 80;
    CGFloat pageH = 20;
    CGFloat pageX = self.scrollView.frame.size.width - pageW;
    CGFloat pageY = self.scrollView.frame.size.height -pageH;
    self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);



}

-(void)setImages:(NSArray *)images
{
    _images = images;

    //设置页码
    self.pageControl .numberOfPages= images.count;
    self.pageControl.currentPage = 0;

    //设置内容
    [self updateContent];
    //开始定时器
    [self startTimer];
}

#pragma mark -- 
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //找出最中间的那个图片控件
    NSInteger page = 0;
    CGFloat minDistance = MAXFLOAT;
    for (int i =0; i<self.scrollView.subviews.count; i++) {
        UIImageView * imageView = self.scrollView.subviews[i];
        CGFloat distance = 0;
        if (self.isScrollDirectionPortrait) {
            distance = ABS(imageView.frame.origin.y - scrollView.contentOffset.y);

        }else
        {
            distance = ABS(imageView.frame.origin.x -scrollView.contentOffset.x);
        }
        if (distance < minDistance) {
            minDistance = distance;
            page = imageView .tag;
        }
    }
    self.pageControl.currentPage = page;

}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updateContent];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self updateContent];
}
#pragma mark -- 内容更新
-(void)updateContent
{
    //设置图片
    for (int i = 0; i <self.scrollView.subviews.count; i++) {
        UIImageView * imageView = self.scrollView.subviews[i];
        NSInteger index = self.pageControl.currentPage;
        if (i == 0) {
            index --;
        }else if (i==2){
            index++;
        }

        if (index <0) {
            index = self.pageControl.numberOfPages -1;
        }else if (index >= self.pageControl.numberOfPages)
        {
            index = 0;
        }

        imageView.tag= index;
        imageView.image = self.images[index];

    }

    //设置偏移量在中间
    if (self.isScrollDirectionPortrait) {
        self.scrollView.contentOffset = CGPointMake(0, self.scrollView.frame.size.height);
    }else
    {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    }

}

#pragma mark -- 定时器
-(void)startTimer
{
    NSTimer * timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(next) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

-(void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}
-(void)next
{
    if (self.isScrollDirectionPortrait) {
        [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];
    }



}


@end

•在viewController .m文件里导入#import “GDGInfiniteScrollView.h”直接用,你可以自由改动滚动视图的大小、图片、页面指示器的颜色

#import "ViewController.h"
#import "GDGInfiniteScrollView.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化对象
    GDGInfiniteScrollView *  scrollView = [[GDGInfiniteScrollView alloc]init];
    //设置滚动视图的大小
    scrollView.frame = CGRectMake(30, 50, 300, 230);
    //设置循环滚动的图片
    scrollView.images = @[
                          [UIImage imageNamed:@"img_00"],
                          [UIImage imageNamed:@"img_01"],
                          [UIImage imageNamed:@"img_02"],
                          [UIImage imageNamed:@"img_03"],
                          [UIImage imageNamed:@"img_04"]
                          ];
    //设置页面指示器的颜色
    scrollView.pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    scrollView.pageControl .pageIndicatorTintColor = [UIColor grayColor];
    //添加到当前view
    [self.view addSubview:scrollView];


}


@end

Demo下载 github地址点击GDGInfiniteScrollViewDemo

你可能感兴趣的:(iOS开发实用代码)