| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- //
- // CycleScrollView.m
- // PagedScrollView
- //
- // Created by 陈政 on 14-1-23.
- // Copyright (c) 2014年 Apple Inc. All rights reserved.
- //
- #import "CycleScrollView.h"
- #import "NSTimer+Addition.h"
- #import "SliderPage.h"
- #import "HWWeakTimer.h"
- @interface CycleScrollView () <UIScrollViewDelegate>
- @property (nonatomic , assign) NSInteger currentPageIndex;
- @property (nonatomic , assign) NSInteger totalPageCount;
- @property (nonatomic , strong) NSMutableArray *contentViews;
- @property (nonatomic , strong) UIScrollView *scrollView;
- @property (nonatomic , weak) NSTimer *animationTimer;
- @property (nonatomic , assign) NSTimeInterval animationDuration;
- @property (nonatomic , strong) UIPageControl *pageControl;
- @end
- @implementation CycleScrollView
- -(void) dealloc
- {
- [self.animationTimer invalidate];
- DebugLog(@"CycleScrollView dealloc");
-
-
- }
- -(void) stopTimmer
- {
- // return;
- [self.animationTimer invalidate];
-
- // [[NSNotificationCenter defaultCenter] removeObserver:self name:CYCLESCROLL_STOPTIMMER object:nil];
- }
- -(void) animationDuration:(NSTimeInterval)animationDuration
- {
- // return;
-
- if (animationDuration > 0.0) {
- // self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:(self.animationDuration = animationDuration)
- // target:self
- // selector:@selector(animationTimerDidFired:)
- // userInfo:nil
- // repeats:YES];
-
- self.animationTimer = [HWWeakTimer scheduledTimerWithTimeInterval:(self.animationDuration = animationDuration)
- target:self
- selector:@selector(animationTimerDidFired:)
- userInfo:nil
- repeats:YES];
-
- // self.animationTimer =[HWWeakTimer scheduledTimerWithTimeInterval:3.0f block:^(id userInfo) {
- // NSLog(@"%@", userInfo);
- // } userInfo:@"Fire" repeats:YES];
- [self.animationTimer pauseTimer];
- }
- }
- - (void)setTotalPagesCount
- {
-
- _totalPageCount = self.arr_Pages.count;
- if (_totalPageCount > 0) {
- [self configContentViews];
- [self.animationTimer resumeTimerAfterTimeInterval:self.animationDuration];
- self.pageControl.numberOfPages =_totalPageCount;
- }
- }
- -(void) addPage:(UIView*) page
- {
- [self.arr_Pages addObject:page];
- }
- -(void)reset
- {
- // self.page_count = 0;
- self.arr_Pages =[@[] mutableCopy];
- // Initialization code
- self.autoresizesSubviews = YES;
- // self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
- self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;//0xFF;
- self.scrollView.contentMode = UIViewContentModeCenter;
- self.scrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
- // CGSize sss= self.scrollView.contentSize;
- // CGRect rrr =self.scrollView.frame;
-
- self.scrollView.delegate = self;
- self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.scrollView.frame), 0);
- self.scrollView.pagingEnabled = YES;
- // [self addSubview:self.scrollView];
-
- // CGRect pageFrame =self.bounds;
- // pageFrame.origin.y = self.frame.size.height -16-20;
- // pageFrame.size.height = 20;
- // pageFrame
- // self.pageControl = [[UIPageControl alloc] initWithFrame:pageFrame];//CGRectMake(0, self.frame.size.height -16-10, 320, 10)];
- self.pageControl.userInteractionEnabled = NO;
- // [self addSubview:self.pageControl];
- // int count =self.totalPageCount;
- // self.pageControl.backgroundColor = [UIColor purpleColor];
- self.pageControl.numberOfPages = self.totalPageCount;
- self.pageControl.currentPage = 0;
-
- self.scrollView.showsHorizontalScrollIndicator = false;
- self.scrollView.showsVerticalScrollIndicator = false;
-
-
- self.currentPageIndex = 0;
-
-
-
-
- }
- - (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];
- // }
-
- [self animationDuration:animationDuration];
- 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 = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;//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;
-
-
- CGRect pageFrame =self.bounds;
- pageFrame.origin.y = self.frame.size.height -16-20;
- pageFrame.size.height = 20;
- self.pageControl = [[UIPageControl alloc] initWithFrame:pageFrame];
- self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
- [self addSubview:self.pageControl];
-
-
- // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopTimmer) name:CYCLESCROLL_STOPTIMMER object:nil];
- }
- return self;
- }
- #pragma mark -
- #pragma mark - 私有函数
- - (void)configContentViews
- {
- [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
- [self setScrollViewContentDataSource];
-
- NSInteger counter = 0;
- for (UIView *contentView in self.contentViews) {
- contentView.userInteractionEnabled = YES;
-
- CGRect rightRect = self.frame;//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)
- {
-
-
-
- SliderPage* previousPage =self.arr_Pages[previousPageIndex];
- [previousPage loadImage];
-
-
-
-
- [self.contentViews addObject:previousPage];
-
-
- SliderPage* currentPage =self.arr_Pages[_currentPageIndex];
- [currentPage loadImage];
- [self.contentViews addObject:currentPage];
-
-
- // UIView* currentView =self.fetchContentViewAtIndex(_currentPageIndex);
- currentPage.gestureRecognizers= nil;
- UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewTapAction:)];
- [currentPage addGestureRecognizer:tapGesture];
-
-
- SliderPage* rearPage =self.arr_Pages[rearPageIndex];
- [rearPage loadImage];
-
- [self.contentViews addObject:rearPage];
- }
- }
- - (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];
- // DebugLog(@"next,当前页:%d",self.currentPageIndex);
- [self configContentViews];
- }
- if(contentOffsetX <= 0) {
- self.currentPageIndex = [self getValidNextPageIndexWithPageIndex:self.currentPageIndex - 1];
- // DebugLog(@"previous,当前页:%d",self.currentPageIndex);
- [self configContentViews];
- }
- self.pageControl.currentPage = self.currentPageIndex;
- }
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- {
- [scrollView setContentOffset:CGPointMake(CGRectGetWidth(scrollView.frame), 0) animated:NO];
- }
- #pragma mark -
- #pragma mark - 响应事件
- - (void)animationTimerDidFired:(NSTimer *)timer
- {
- CGPoint newOffset = CGPointMake(/*self.scrollView.contentOffset.x +*/ 2* CGRectGetWidth(self.scrollView.frame), self.scrollView.contentOffset.y);
- [self.scrollView setContentOffset:newOffset animated:NO];
- }
- - (void)contentViewTapAction:(UITapGestureRecognizer *)tap
- {
- if (self.TapActionBlock) {
- self.TapActionBlock(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
|