UIScrollView 分页 无限滚动

ZYXInfiniteScrollView.h

#import 

@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() 
@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
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 找出最中间的那个图片控件
    NSInteger page = 0;
    CGFloat minDistance = MAXFLOAT;
    for (int i = 0; i= 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 分页 无限滚动)