Ei kuvausta

DPBannerView.m 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // DPBannerView.m
  3. // DacaiProject
  4. //
  5. // Created by WUFAN on 14-8-29.
  6. // Copyright (c) 2014年 dacai. All rights reserved.
  7. //
  8. #import "DPBannerView.h"
  9. @implementation DPBannerScrollView
  10. - (instancetype)initWithFrame:(CGRect)frame {
  11. if (self = [super initWithFrame:frame]) {
  12. [self addObserver:self forKeyPath:@"panGestureRecognizer.state" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
  13. }
  14. return self;
  15. }
  16. - (void)dealloc {
  17. [self removeObserver:self forKeyPath:@"panGestureRecognizer.state"];
  18. }
  19. - (void)setDelegate:(id<DPBannerScrollViewDelegate>)delegate {
  20. [super setDelegate:delegate];
  21. _bannerDelegateHas.scrollViewDidBeginTracking = [delegate respondsToSelector:@selector(scrollViewDidBeginTracking:)];
  22. _bannerDelegateHas.scrollViewDidEndTracking = [delegate respondsToSelector:@selector(scrollViewDidEndTracking:)];
  23. }
  24. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  25. switch (self.panGestureRecognizer.state) {
  26. case UIGestureRecognizerStateEnded:
  27. case UIGestureRecognizerStateCancelled:
  28. case UIGestureRecognizerStateFailed:
  29. if (_bannerDelegateHas.scrollViewDidEndTracking) {
  30. [(id<DPBannerScrollViewDelegate>)self.delegate scrollViewDidEndTracking:self];
  31. }
  32. break;
  33. default:
  34. break;
  35. }
  36. }
  37. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  38. if (_bannerDelegateHas.scrollViewDidBeginTracking) {
  39. [(id<DPBannerScrollViewDelegate>)self.delegate scrollViewDidBeginTracking:self];
  40. }
  41. }
  42. @end
  43. @interface DPBannerView () <UIScrollViewDelegate> {
  44. @private
  45. NSInteger _currentIndex;
  46. }
  47. @property (nonatomic, assign) CGRect lastBounds;
  48. @property (nonatomic, strong, readonly) UIImageView *preView;
  49. @property (nonatomic, strong, readonly) UIImageView *curView;
  50. @property (nonatomic, strong, readonly) UIImageView *nxtView;
  51. @property (nonatomic, assign, readonly) NSInteger preIndex;
  52. @property (nonatomic, assign, readonly) NSInteger curIndex;
  53. @property (nonatomic, assign, readonly) NSInteger nxtIndex;
  54. //@property (nonatomic, strong, readonly) UIPageControl *pageControl;
  55. @end
  56. @implementation DPBannerView
  57. @dynamic index;
  58. @dynamic preIndex;
  59. @dynamic curIndex;
  60. @dynamic nxtIndex;
  61. - (id)initWithFrame:(CGRect)frame {
  62. self = [super initWithFrame:frame];
  63. if (self) {
  64. // Initialization code
  65. [self _initialize];
  66. [self addSubview:self.scrollView];
  67. // [self addSubview:self.pageControl];
  68. [self.scrollView addSubview:self.preView];
  69. [self.scrollView addSubview:self.curView];
  70. [self.scrollView addSubview:self.nxtView];
  71. [self addGestureRecognizer:({
  72. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pvt_onTap)];
  73. tap;
  74. })];
  75. [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
  76. }
  77. return self;
  78. }
  79. - (void)pvt_onTap {
  80. if (self.delegate && [self.delegate respondsToSelector:@selector(bannerView:didSelectedAtIndex:)]) {
  81. [self.delegate bannerView:self didSelectedAtIndex:self.curIndex];
  82. }
  83. }
  84. - (void)layoutSubviews {
  85. self.scrollView.frame = self.bounds;
  86. // self.pageControl.frame = CGRectMake(0, CGRectGetMaxY(self.bounds) - 20, CGRectGetWidth(self.bounds), 20);
  87. if (!CGRectEqualToRect(self.lastBounds, self.bounds)) {
  88. self.lastBounds = self.bounds;
  89. // 调整图片大小
  90. CGRect frame = self.bounds;
  91. self.preView.frame = frame;
  92. frame.origin.x += CGRectGetWidth(frame);
  93. self.curView.frame = frame;
  94. frame.origin.x += CGRectGetWidth(frame);
  95. self.nxtView.frame = frame;
  96. self.scrollView.contentSize = CGSizeMake(3 * CGRectGetWidth(frame), CGRectGetHeight(frame));
  97. self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(frame), 0);
  98. }
  99. }
  100. #pragma mark - 无限循环
  101. - (void)reloadData {
  102. if (self.imageUrls.count) {
  103. if (self.preIndex < self.imageUrls.count) [self.preView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[self.preIndex]]];
  104. if (self.curIndex < self.imageUrls.count) [self.curView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[self.curIndex]]];
  105. if (self.nxtIndex < self.imageUrls.count) [self.nxtView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[self.nxtIndex]]];
  106. // self.preView.image = self.images[self.preIndex];
  107. // self.curView.image = self.images[self.curIndex];
  108. // self.nxtView.image = self.images[self.nxtIndex];
  109. } else {
  110. self.preView.image = self.curView.image = self.nxtView.image = nil;
  111. }
  112. [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.bounds), 0)];
  113. // [self.pageControl setCurrentPage:_currentIndex];
  114. }
  115. - (void)autoSwitchBannerView {
  116. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
  117. if (self.duration > 0) {
  118. if (self.imageUrls.count > 1) {
  119. [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.bounds) * 2 + 1, 0) animated:YES];
  120. }
  121. [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
  122. }
  123. }
  124. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  125. if (self.scrollView.contentOffset.x < 0) {
  126. _currentIndex = self.preIndex;
  127. if ([self.delegate respondsToSelector:@selector(bannerView:didShowAtIndex:)]) {
  128. [self.delegate bannerView:self didShowAtIndex:_currentIndex];
  129. }
  130. } else if (self.scrollView.contentOffset.x > CGRectGetWidth(self.bounds) * 2) {
  131. _currentIndex = self.nxtIndex;
  132. if ([self.delegate respondsToSelector:@selector(bannerView:didShowAtIndex:)]) {
  133. [self.delegate bannerView:self didShowAtIndex:_currentIndex];
  134. }
  135. } else {
  136. return;
  137. }
  138. [self reloadData];
  139. }
  140. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  141. NSInteger index = self.scrollView.contentOffset.x / CGRectGetWidth(self.bounds);
  142. if (index == 0) {
  143. _currentIndex = self.preIndex;
  144. if ([self.delegate respondsToSelector:@selector(bannerView:didShowAtIndex:)]) {
  145. [self.delegate bannerView:self didShowAtIndex:_currentIndex];
  146. }
  147. } else if (index == 2) {
  148. _currentIndex = self.nxtIndex;
  149. if ([self.delegate respondsToSelector:@selector(bannerView:didShowAtIndex:)]) {
  150. [self.delegate bannerView:self didShowAtIndex:_currentIndex];
  151. }
  152. } else {
  153. return;
  154. }
  155. [self reloadData];
  156. }
  157. - (void)scrollViewDidBeginTracking:(UIScrollView *)scrollView {
  158. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
  159. }
  160. - (void)scrollViewDidEndTracking:(UIScrollView *)scrollView {
  161. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
  162. [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
  163. }
  164. #pragma mark - method
  165. - (void)startAutoSwitch{
  166. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
  167. [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
  168. }
  169. - (void)stopAutoSwitch{
  170. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
  171. }
  172. #pragma mark - getter, setter
  173. - (void)setDuration:(CGFloat)duration {
  174. _duration = duration;
  175. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSwitchBannerView) object:nil];
  176. [self performSelector:@selector(autoSwitchBannerView) withObject:nil afterDelay:self.duration];
  177. }
  178. - (void)setIndex:(NSInteger)index {
  179. _currentIndex = index;
  180. [self reloadData];
  181. }
  182. - (NSInteger)index {
  183. return _currentIndex;
  184. }
  185. - (NSInteger)preIndex {
  186. return _currentIndex == 0 ? (self.imageUrls.count - 1) : (_currentIndex - 1);
  187. }
  188. - (NSInteger)curIndex {
  189. return _currentIndex;
  190. }
  191. - (NSInteger)nxtIndex {
  192. return _currentIndex == (self.imageUrls.count - 1) ? 0 : (_currentIndex + 1);
  193. }
  194. - (void)setImageUrls:(NSArray *)imageUrls {
  195. _imageUrls = imageUrls;
  196. _currentIndex = 0;
  197. _scrollView.scrollEnabled = imageUrls.count > 1;
  198. // _pageControl.numberOfPages = imageUrls.count;
  199. [self reloadData];
  200. }
  201. #pragma mark - initialize
  202. - (void)_initialize {
  203. [self setBackgroundColor:[UIColor clearColor]];
  204. // _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
  205. // _pageControl.hidesForSinglePage = YES;
  206. // _pageControl.userInteractionEnabled = NO;
  207. // view
  208. _scrollView = [[DPBannerScrollView alloc] init];
  209. _scrollView.delegate = self;
  210. _scrollView.pagingEnabled = YES;
  211. _scrollView.scrollEnabled = NO;
  212. _scrollView.showsVerticalScrollIndicator = NO;
  213. _scrollView.showsHorizontalScrollIndicator = NO;
  214. _scrollView.delaysContentTouches = NO;
  215. _scrollView.scrollsToTop = NO;
  216. _scrollView.bounces = NO;
  217. _preView = [[UIImageView alloc] init];
  218. _curView = [[UIImageView alloc] init];
  219. _nxtView = [[UIImageView alloc] init];
  220. // loop
  221. _duration = 5.0f;
  222. }
  223. - (void)setDefalutContentModel:(UIViewContentMode)defalutContentModel{
  224. _preView.contentMode = defalutContentModel;
  225. _curView.contentMode = defalutContentModel;
  226. _nxtView.contentMode = defalutContentModel;
  227. }
  228. @end