XLCycleScrollView.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // XLCycleScrollView.m
  3. // CycleScrollViewDemo
  4. //
  5. // Created by xie liang on 9/14/12.
  6. // Copyright (c) 2012 xie liang. All rights reserved.
  7. //
  8. #import "XLCycleScrollView.h"
  9. @implementation XLCycleScrollView
  10. @synthesize scrollView = _scrollView;
  11. @synthesize pageControl = _pageControl;
  12. @synthesize currentPage = _curPage;
  13. @synthesize datasource = _datasource;
  14. @synthesize delegate = _delegate;
  15. - (void)dealloc
  16. {
  17. // [_scrollView release];
  18. // [_pageControl release];
  19. // [_curViews release];
  20. // [super dealloc];
  21. }
  22. - (id)initWithFrame:(CGRect)frame pcRect:(CGRect)pcRect
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. // Initialization code
  27. _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
  28. _scrollView.delegate = self;
  29. _scrollView.contentSize = CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height);
  30. _scrollView.showsHorizontalScrollIndicator = NO;
  31. _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
  32. _scrollView.pagingEnabled = YES;
  33. [self addSubview:_scrollView];
  34. _pageControl = [[StyledPageControl alloc] initWithFrame:pcRect];
  35. _pageControl.userInteractionEnabled = NO;
  36. [_pageControl setBackgroundColor:[UIColor clearColor]];
  37. [self addSubview:_pageControl];
  38. _curPage = 0;
  39. }
  40. return self;
  41. }
  42. - (void)setDataource:(id<XLCycleScrollViewDatasource>)datasource
  43. {
  44. _datasource = datasource;
  45. [self reloadData];
  46. }
  47. - (void)reloadData
  48. {
  49. _totalPages = [_datasource numberOfPages];
  50. if (_totalPages == 0) {
  51. return;
  52. }
  53. _pageControl.numberOfPages =(int) _totalPages;
  54. [self loadData];
  55. }
  56. - (void)loadData
  57. {
  58. _pageControl.currentPage = (int)_curPage;
  59. //从scrollView上移除所有的subview
  60. NSArray *subViews = [_scrollView subviews];
  61. if([subViews count] != 0) {
  62. [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  63. }
  64. [self getDisplayImagesWithCurpage:(int)_curPage];
  65. for (int i = 0; i < 3; i++) {
  66. UIView *v = [_curViews objectAtIndex:i];
  67. v.userInteractionEnabled = YES;
  68. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
  69. action:@selector(handleTap:)];
  70. [v addGestureRecognizer:singleTap];
  71. // [singleTap release];
  72. v.frame = CGRectOffset(v.frame, v.frame.size.width * i, 0);
  73. [_scrollView addSubview:v];
  74. }
  75. [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)];
  76. }
  77. - (void)getDisplayImagesWithCurpage:(int)page {
  78. int pre = [self validPageValue:_curPage-1];
  79. int last = [self validPageValue:_curPage+1];
  80. if (!_curViews) {
  81. _curViews = [[NSMutableArray alloc] init];
  82. }
  83. [_curViews removeAllObjects];
  84. [_curViews addObject:[_datasource pageAtIndex:pre]];
  85. [_curViews addObject:[_datasource pageAtIndex:page]];
  86. [_curViews addObject:[_datasource pageAtIndex:last]];
  87. }
  88. - (int)validPageValue:(NSInteger)value {
  89. if(value == -1) value = _totalPages - 1;
  90. if(value == _totalPages) value = 0;
  91. return (int)value;
  92. }
  93. - (void)handleTap:(UITapGestureRecognizer *)tap {
  94. if ([_delegate respondsToSelector:@selector(didClickPage:atIndex:)]) {
  95. [_delegate didClickPage:self atIndex:_curPage];
  96. }
  97. }
  98. - (void)setViewContent:(UIView *)view atIndex:(NSInteger)index
  99. {
  100. if (index == _curPage) {
  101. [_curViews replaceObjectAtIndex:1 withObject:view];
  102. for (int i = 0; i < 3; i++) {
  103. UIView *v = [_curViews objectAtIndex:i];
  104. v.userInteractionEnabled = YES;
  105. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
  106. action:@selector(handleTap:)];
  107. [v addGestureRecognizer:singleTap];
  108. // [singleTap release];
  109. v.frame = CGRectOffset(v.frame, v.frame.size.width * i, 0);
  110. [_scrollView addSubview:v];
  111. }
  112. }
  113. }
  114. #pragma mark - UIScrollViewDelegate
  115. - (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
  116. int x = aScrollView.contentOffset.x;
  117. //往下翻一张
  118. if(x >= (2*self.frame.size.width)) {
  119. _curPage = [self validPageValue:_curPage+1];
  120. [self loadData];
  121. }
  122. //往上翻
  123. if(x <= 0) {
  124. _curPage = [self validPageValue:_curPage-1];
  125. [self loadData];
  126. }
  127. }
  128. - (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView {
  129. [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:YES];
  130. }
  131. @end