// // XLCycleScrollView.m // CycleScrollViewDemo // // Created by xie liang on 9/14/12. // Copyright (c) 2012 xie liang. All rights reserved. // #import "XLCycleScrollView.h" @implementation XLCycleScrollView @synthesize scrollView = _scrollView; @synthesize pageControl = _pageControl; @synthesize currentPage = _curPage; @synthesize datasource = _datasource; @synthesize delegate = _delegate; - (void)dealloc { // [_scrollView release]; // [_pageControl release]; // [_curViews release]; // [super dealloc]; } - (id)initWithFrame:(CGRect)frame pcRect:(CGRect)pcRect { self = [super initWithFrame:frame]; if (self) { // Initialization code _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds]; _scrollView.delegate = self; _scrollView.contentSize = CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height); _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0); _scrollView.pagingEnabled = YES; [self addSubview:_scrollView]; _pageControl = [[StyledPageControl alloc] initWithFrame:pcRect]; _pageControl.userInteractionEnabled = NO; [_pageControl setBackgroundColor:[UIColor clearColor]]; [self addSubview:_pageControl]; _curPage = 0; } return self; } - (void)setDataource:(id)datasource { _datasource = datasource; [self reloadData]; } - (void)reloadData { _totalPages = [_datasource numberOfPages]; if (_totalPages == 0) { return; } _pageControl.numberOfPages =(int) _totalPages; [self loadData]; } - (void)loadData { _pageControl.currentPage = (int)_curPage; //从scrollView上移除所有的subview NSArray *subViews = [_scrollView subviews]; if([subViews count] != 0) { [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)]; } [self getDisplayImagesWithCurpage:(int)_curPage]; for (int i = 0; i < 3; i++) { UIView *v = [_curViews objectAtIndex:i]; v.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [v addGestureRecognizer:singleTap]; // [singleTap release]; v.frame = CGRectOffset(v.frame, v.frame.size.width * i, 0); [_scrollView addSubview:v]; } [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)]; } - (void)getDisplayImagesWithCurpage:(int)page { int pre = [self validPageValue:_curPage-1]; int last = [self validPageValue:_curPage+1]; if (!_curViews) { _curViews = [[NSMutableArray alloc] init]; } [_curViews removeAllObjects]; [_curViews addObject:[_datasource pageAtIndex:pre]]; [_curViews addObject:[_datasource pageAtIndex:page]]; [_curViews addObject:[_datasource pageAtIndex:last]]; } - (int)validPageValue:(NSInteger)value { if(value == -1) value = _totalPages - 1; if(value == _totalPages) value = 0; return (int)value; } - (void)handleTap:(UITapGestureRecognizer *)tap { if ([_delegate respondsToSelector:@selector(didClickPage:atIndex:)]) { [_delegate didClickPage:self atIndex:_curPage]; } } - (void)setViewContent:(UIView *)view atIndex:(NSInteger)index { if (index == _curPage) { [_curViews replaceObjectAtIndex:1 withObject:view]; for (int i = 0; i < 3; i++) { UIView *v = [_curViews objectAtIndex:i]; v.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [v addGestureRecognizer:singleTap]; // [singleTap release]; v.frame = CGRectOffset(v.frame, v.frame.size.width * i, 0); [_scrollView addSubview:v]; } } } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)aScrollView { int x = aScrollView.contentOffset.x; //往下翻一张 if(x >= (2*self.frame.size.width)) { _curPage = [self validPageValue:_curPage+1]; [self loadData]; } //往上翻 if(x <= 0) { _curPage = [self validPageValue:_curPage-1]; [self loadData]; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView { [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:YES]; } @end