| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- //
- // 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<XLCycleScrollViewDatasource>)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
|