GGBannerView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // GGBannerView.m
  3. // GGBannerViewDemo
  4. //
  5. // Created by GuinsooMBP on 15/8/29.
  6. // Copyright (c) 2015年 gaonan. All rights reserved.
  7. //
  8. #import "GGBannerView.h"
  9. #import "GGBannerCollectionViewCell.h"
  10. @interface GGBannerView ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
  11. @property (nonatomic, strong) NSArray *imageArray;
  12. @property (nonatomic, strong) UICollectionView *bannerCollectionView;
  13. @property (nonatomic, strong) UIPageControl *pageController;
  14. @property (nonatomic, strong) NSTimer *timer;
  15. @property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
  16. @property (nonatomic, assign) CGFloat unitLength;
  17. @property (nonatomic, assign) CGFloat offsetLength;
  18. @property (nonatomic, assign) CGFloat contentLength;
  19. @property (nonatomic, assign) CGFloat oldOffsetLength;
  20. @property(nonatomic, strong) QMUIGhostButton *clickBtn;//立即体验按钮
  21. @end
  22. @implementation GGBannerView
  23. - (instancetype)initWithFrame:(CGRect)frame {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. [self initSubviews];
  27. }
  28. return self;
  29. }
  30. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  31. self = [super initWithCoder: aDecoder];
  32. if (self) {
  33. [self initSubviews];
  34. }
  35. return self;
  36. }
  37. - (void)layoutSubviews {
  38. [super layoutSubviews];
  39. self.flowLayout.itemSize = self.frame.size;
  40. }
  41. #pragma mark - public method
  42. - (void)configBanner:(NSArray *)imageArray {
  43. self.imageArray = imageArray;
  44. self.pageController.numberOfPages = imageArray.count;
  45. [self.bannerCollectionView reloadData];
  46. }
  47. #pragma mark - private method
  48. - (void)initSubviews {
  49. [self addSubview:self.bannerCollectionView];
  50. [self addSubview:self.pageController];
  51. self.pageController.hidden = YES;
  52. self.bannerCollectionView.frame = CGRectMake(0, -20, kScreenW, kScreenH + 20);
  53. // [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_bannerCollectionView]-0-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_bannerCollectionView)]];
  54. // [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_bannerCollectionView]-0-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_bannerCollectionView)]];
  55. // [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_pageController]-10-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_pageController)]];
  56. // [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_pageController]-0-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_pageController)]];
  57. self.scrollEnabled = YES;
  58. self.interval = 0.0;
  59. self.scrollDirection = GGBannerViewScrollDirectionHorizontal;
  60. }
  61. - (void)addTimer {
  62. if (self.interval == 0) {
  63. return;
  64. }
  65. self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(changePage) userInfo:nil repeats:YES];
  66. [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  67. }
  68. - (void)removeTimer {
  69. [self.timer invalidate];
  70. self.timer = nil;
  71. }
  72. - (void)changePage {
  73. if (self.imageArray.count == 1) {
  74. if (self.delegate && [self.delegate respondsToSelector:@selector(bannerViewFinsh)]) {
  75. [self.delegate bannerViewFinsh];
  76. }
  77. return;
  78. }
  79. CGFloat newOffSetLength = self.offsetLength + self.unitLength;
  80. //在换页到最后一个的时候多加一点距离,触发回到第一个图片的事件
  81. if (newOffSetLength == self.contentLength - self.unitLength) {
  82. newOffSetLength += 1;
  83. if (self.delegate && [self.delegate respondsToSelector:@selector(bannerViewFinsh)]) {
  84. [self.delegate bannerViewFinsh];
  85. }
  86. return;
  87. }
  88. CGPoint offSet;
  89. if (self.scrollDirection == GGBannerViewScrollDirectionHorizontal) {
  90. offSet = CGPointMake(newOffSetLength, -MG_TOP_MARGIN - MG_BOTTOM_MARGIN + 10);
  91. }else{
  92. offSet = CGPointMake(0,newOffSetLength);
  93. }
  94. [self.bannerCollectionView setContentOffset:offSet animated:YES];
  95. //修复在滚动动画进行中切换tabbar或push一个新的controller时导致图片显示错位问题。
  96. //原因:系统会在view not-on-screen时移除所有coreAnimation动画,导致动画无法完成,轮播图停留在切换中间的状态。
  97. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  98. //动画完成后的实际offset和应该到达的offset不一致,重置offset。
  99. if (self.offsetLength!=newOffSetLength && self.offsetLength!=0) {
  100. self.bannerCollectionView.contentOffset = offSet;
  101. }
  102. });
  103. }
  104. - (NSString *)getImageUrlForIndexPath:(NSIndexPath *)indexPath {
  105. if (!(self.imageArray.count > 0)) {
  106. return nil;
  107. }
  108. if (indexPath.row == self.imageArray.count){
  109. return self.imageArray.firstObject;
  110. } else {
  111. return self.imageArray[indexPath.row];
  112. }
  113. }
  114. #pragma mark - collectionView delegate
  115. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  116. if(self.imageArray.count == 1) {
  117. return 1;
  118. }
  119. return self.imageArray.count + 1;
  120. }
  121. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  122. GGBannerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"banner" forIndexPath:indexPath];
  123. NSString *url = [self getImageUrlForIndexPath:indexPath];
  124. if ([self.delegate respondsToSelector:@selector(imageView:loadImageForUrl:)]) {
  125. [self.delegate imageView:cell.imageView loadImageForUrl:url];
  126. }
  127. if (indexPath.row == self.imageArray.count - 1) {
  128. [cell.contentView addSubview:self.clickBtn];
  129. }
  130. return cell;
  131. }
  132. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  133. if ([self.delegate respondsToSelector:@selector(bannerView:didSelectAtIndex:)]) {
  134. [self.delegate bannerView:self didSelectAtIndex:self.pageController.currentPage];
  135. }
  136. }
  137. #pragma mark - scrollView delegate
  138. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  139. [_timer invalidate];
  140. }
  141. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  142. // self.pageController.currentPage = self.offsetLength / self.unitLength;
  143. [self addTimer];
  144. }
  145. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  146. if (self.imageArray.count>0) {
  147. NSInteger page = (NSInteger)(self.offsetLength / self.unitLength) % self.imageArray.count;
  148. UICollectionView *collectionView = (UICollectionView *)scrollView;
  149. self.pageController.currentPage = page;
  150. if (self.oldOffsetLength > self.offsetLength) {
  151. if (self.offsetLength < 0)
  152. {
  153. [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.imageArray.count inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  154. }
  155. }else{
  156. //修复从index0 到 lastIndex 来回滚动问题
  157. if (self.oldOffsetLength == 0 && self.offsetLength == self.imageArray.count * self.unitLength) {
  158. return;
  159. }
  160. if (self.offsetLength >= self.contentLength - self.unitLength) {
  161. [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  162. }
  163. }
  164. self.oldOffsetLength = self.offsetLength;
  165. }
  166. }
  167. #pragma mark - setter && getter
  168. - (UICollectionView *)bannerCollectionView {
  169. if (!_bannerCollectionView) {
  170. _bannerCollectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
  171. _bannerCollectionView.dataSource = self;
  172. _bannerCollectionView.delegate = self;
  173. _bannerCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
  174. [_bannerCollectionView registerClass:[GGBannerCollectionViewCell class] forCellWithReuseIdentifier:@"banner"];
  175. _bannerCollectionView.pagingEnabled = YES;
  176. _bannerCollectionView.showsHorizontalScrollIndicator = NO;
  177. _bannerCollectionView.showsVerticalScrollIndicator = NO;
  178. }
  179. return _bannerCollectionView;
  180. }
  181. - (UICollectionViewFlowLayout *)flowLayout {
  182. if (!_flowLayout) {
  183. _flowLayout = [[UICollectionViewFlowLayout alloc]init];
  184. _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  185. _flowLayout.minimumInteritemSpacing = 0;
  186. _flowLayout.minimumLineSpacing = 0;
  187. }
  188. return _flowLayout;
  189. }
  190. - (UIPageControl *)pageController {
  191. if (!_pageController) {
  192. _pageController = [[UIPageControl alloc] init];
  193. _pageController.currentPage = 0;
  194. _pageController.numberOfPages = self.imageArray.count;
  195. _pageController.backgroundColor = [UIColor clearColor];
  196. _pageController.currentPageIndicatorTintColor = [UIColor whiteColor];
  197. _pageController.pageIndicatorTintColor = [UIColor lightGrayColor];
  198. _pageController.translatesAutoresizingMaskIntoConstraints = NO;
  199. }
  200. return _pageController;
  201. }
  202. - (void)setScrollDirection:(GGBannerViewScrollDirection)scrollDirection {
  203. if (_scrollDirection != scrollDirection) {
  204. _scrollDirection = scrollDirection;
  205. if (scrollDirection == GGBannerViewScrollDirectionVertical) {
  206. self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  207. }else{
  208. self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  209. }
  210. [self.bannerCollectionView reloadData];
  211. }
  212. }
  213. - (void)setScrollEnabled:(BOOL)scrollEnabled {
  214. _scrollEnabled = scrollEnabled;
  215. self.bannerCollectionView.scrollEnabled = _scrollEnabled;
  216. }
  217. - (CGFloat)unitLength {
  218. return self.scrollDirection == GGBannerViewScrollDirectionHorizontal ? CGRectGetWidth(self.frame) : CGRectGetHeight(self.frame);
  219. }
  220. - (CGFloat)offsetLength {
  221. return self.scrollDirection == GGBannerViewScrollDirectionHorizontal ? self.bannerCollectionView.contentOffset.x : self.bannerCollectionView.contentOffset.y;
  222. }
  223. - (CGFloat)contentLength {
  224. return self.scrollDirection == GGBannerViewScrollDirectionHorizontal ? self.bannerCollectionView.contentSize.width : self.bannerCollectionView.contentSize.height;
  225. }
  226. - (void)setInterval:(NSTimeInterval)interval {
  227. _interval = interval;
  228. [self removeTimer];
  229. if (interval != 0) {
  230. [self addTimer];
  231. }
  232. }
  233. -(QMUIGhostButton *)clickBtn{
  234. if (!_clickBtn) {
  235. _clickBtn = [QMUIGhostButton buttonWithType:UIButtonTypeCustom];
  236. _clickBtn.frame = CGRectMake(0, kScreenH - kRealValue(38) - kRealValue(40), kRealValue(120), kRealValue(40));
  237. _clickBtn.centerX = kScreenW / 2;
  238. _clickBtn.titleLabel.font = [UIFont systemFontOfSize:12];
  239. [_clickBtn setTitle:ASLocalizedString(@"立即体验") forState:UIControlStateNormal];
  240. // [_clickBtn setBackgroundImage:[UIImage imageNamed:@"bogo_lg_clickJump_Btn"] forState:UIControlStateNormal];
  241. [_clickBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  242. }
  243. return _clickBtn;
  244. }
  245. -(void)clickBtn:(UIButton *)sender{
  246. }
  247. /*
  248. // Only override drawRect: if you perform custom drawing.
  249. // An empty implementation adversely affects performance during animation.
  250. - (void)drawRect:(CGRect)rect {
  251. // Drawing code
  252. }
  253. */
  254. @end