// // CycleScrollView.m // PagedScrollView // // Created by 陈政 on 14-1-23. // Copyright (c) 2014年 Apple Inc. All rights reserved. // #import "CycleScrollView.h" #import "NSTimer+Addition.h" @interface CycleScrollView () @property (nonatomic , assign) NSInteger currentPageIndex; @property (nonatomic , assign) NSInteger totalPageCount; @property (nonatomic , strong) NSMutableArray *contentViews; @property (nonatomic , strong) UIScrollView *scrollView; @property (nonatomic , strong) NSTimer *animationTimer; @property (nonatomic , assign) NSTimeInterval animationDuration; @end @implementation CycleScrollView @synthesize pageControl; - (void)setTotalPagesCount:(NSInteger (^)(void))totalPagesCount { _totalPageCount = totalPagesCount(); if (_totalPageCount > 0) { if (self.hasOnlyTwoDateSource) { pageControl.numberOfPages = 2; } else { pageControl.numberOfPages = _totalPageCount; } [self configContentViews]; [self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration]; } if (_totalPageCount <= 0) { [self.scrollView setScrollEnabled:NO]; } else{ [self.scrollView setScrollEnabled:YES]; } } - (id)initWithFrame:(CGRect)frame animationDuration:(NSTimeInterval)animationDuration { self = [self initWithFrame:frame]; if (animationDuration > 0.0) { self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:(self.animationDuration = animationDuration) target:self selector:@selector(animationTimerDidFired:) userInfo:nil repeats:YES]; [self.animationTimer pauseTimer]; } return self; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code self.autoresizesSubviews = YES; self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds]; self.scrollView.autoresizingMask = 0xFF; self.scrollView.contentMode = UIViewContentModeCenter; self.scrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame)); self.scrollView.delegate = self; self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.scrollView.frame), 0); self.scrollView.pagingEnabled = YES; [self addSubview:self.scrollView]; self.currentPageIndex = 0; // UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 120.0, 40.0)]; // [view setBackgroundColor:[UIColor redColor]]; // [self addSubview:view]; // 初始化 pagecontrol self.pageControl = [[UIPageControl alloc] init]; // 初始化mypagecontrol [pageControl setFrame:CGRectMake(self.frame.size.width - 100, self.frame.size.height - 20, 80.0, 15.0)]; if ([[UIDevice currentDevice].systemVersion floatValue] > 6.0) { [pageControl setPageIndicatorTintColor:[UIColor whiteColor]]; [pageControl setCurrentPageIndicatorTintColor:[UIColor grayColor]]; } pageControl.currentPage = 0; // [pageControl setBackgroundColor:[UIColor redColor]]; [self addSubview:pageControl]; } return self; } #pragma mark - #pragma mark - 私有函数 - (void)configContentViews { [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; [self setScrollViewContentDataSource]; NSInteger counter = 0; for (UIImageView *contentView in self.contentViews) { contentView.userInteractionEnabled = YES; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewTapAction:)]; [contentView addGestureRecognizer:tapGesture]; CGRect rightRect = contentView.frame; rightRect.origin = CGPointMake(CGRectGetWidth(self.scrollView.frame) * (counter ++), 0); contentView.frame = rightRect; [self.scrollView addSubview:contentView]; } [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)]; } /** * 设置scrollView的content数据源,即contentViews */ - (void)setScrollViewContentDataSource { NSInteger previousPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1]; NSInteger rearPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1]; if (self.contentViews == nil) { self.contentViews = [@[] mutableCopy]; } [self.contentViews removeAllObjects]; if (self.fetchContentViewAtIndex) { [self.contentViews addObject:self.fetchContentViewAtIndex(previousPageIndex)]; [self.contentViews addObject:self.fetchContentViewAtIndex(_currentPageIndex)]; [self.contentViews addObject:self.fetchContentViewAtIndex(rearPageIndex)]; } } - (NSInteger)getValidNextPageIndexWithPageIndex:(NSInteger)currentPageIndex; { if(currentPageIndex == -1) { return self.totalPageCount - 1; } else if (currentPageIndex == self.totalPageCount) { return 0; } else { return currentPageIndex; } } #pragma mark - #pragma mark - UIScrollViewDelegate - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self.animationTimer pauseTimer]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { int contentOffsetX = scrollView.contentOffset.x; if(contentOffsetX >= (2 * CGRectGetWidth(scrollView.frame))) { self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex + 1]; // NSLog(@"next,当前页:%d",self.currentPageIndex); if (self.hasOnlyTwoDateSource) { pageControl.currentPage = self.currentPageIndex % 2; } else { pageControl.currentPage = self.currentPageIndex % self.totalPageCount; } [self configContentViews]; } if(contentOffsetX <= 0) { self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1]; // NSLog(@"previous,当前页:%d",self.currentPageIndex); if (self.hasOnlyTwoDateSource) { pageControl.currentPage = self.currentPageIndex % 2; } else { pageControl.currentPage = self.currentPageIndex % self.totalPageCount; } [self configContentViews]; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [scrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:YES]; } #pragma mark - #pragma mark - 响应事件 - (void)animationTimerDidFired:(NSTimer *)timer { CGPoint newOffset = CGPointMake(self.scrollView.contentOffset.x + CGRectGetWidth(self.scrollView.frame), self.scrollView.contentOffset.y); [self.scrollView setContentOffset:newOffset animated:YES]; } - (void)contentViewTapAction:(UITapGestureRecognizer *)tap { if (self.TapActionBlock) { self.TapActionBlock(self.currentPageIndex); pageControl.currentPage = self.currentPageIndex; } } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end