美文网首页
UIScrollView 分页 无限滚动

UIScrollView 分页 无限滚动

作者: solozyx | 来源:发表于2016-08-15 19:20 被阅读33次

    ZYXInfiniteScrollView.h

    #import <UIKit/UIKit.h>
    
    @interface ZYXInfiniteScrollView : UIView  // Infinite 无限
    @property (strong, nonatomic) NSArray *images;
    @property (weak, nonatomic, readonly) UIPageControl *pageControl;
    @property (assign, nonatomic, getter=isScrollDirectionPortrait) BOOL scrollDirectionPortrait;
    @end
    

    ZYXInfiniteScrollView.m

    
    
    
    
    
    
    
    #import "ZYXInfiniteScrollView.h"
    
    static int const ImageViewCount = 3;
    
    @interface ZYXInfiniteScrollView() <UIScrollViewDelegate>
    @property (weak, nonatomic) UIScrollView *scrollView;
    @property (weak, nonatomic) NSTimer *timer;
    @end
    
    @implementation ZYXInfiniteScrollView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        NSLog(@"%@",NSStringFromCGRect(frame));
        if (self = [super initWithFrame:frame]) {
            // 滚动视图
            UIScrollView *scrollView = [[UIScrollView alloc] init];
            scrollView.showsHorizontalScrollIndicator = NO;
            scrollView.showsVerticalScrollIndicator = NO;
            scrollView.pagingEnabled = YES;
            scrollView.bounces = NO;
            scrollView.delegate = self;
            [self addSubview:scrollView];
            self.scrollView = scrollView;
            
            // 图片控件
            for (int i = 0; i<ImageViewCount; 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; i<ImageViewCount; i++) {
            UIImageView *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 - <UIScrollViewDelegate>
    - (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
    

    ZYXInfiniteScrollView 使用

    ZYXInfiniteScrollView *scrollView = [[ZYXInfiniteScrollView alloc] init];
      scrollView.scrollDirectionPortrait = NO; // 图片的滚动方向为水平方向 
      scrollView.frame = CGRectMake(0, 20, self.view.frame.size.width, 170);
      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];
      [self.view addSubview:scrollView];
    

    相关文章

      网友评论

          本文标题:UIScrollView 分页 无限滚动

          本文链接:https://www.haomeiwen.com/subject/xvwusttx.html