WMZBannerView.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. //
  2. // WMZBannerView.m
  3. // WMZBanner
  4. //
  5. // Created by wmz on 2019/9/6.
  6. // Copyright © 2019 wmz. All rights reserved.
  7. //
  8. #import "WMZBannerView.h"
  9. #import "WMZBannerFlowLayout.h"
  10. #import "WMZBannerControl.h"
  11. #import "WMZBannerOverLayout.h"
  12. #import "WMZBannerFadeLayout.h"
  13. #define COUNT 500
  14. @interface WMZBannerView()<UICollectionViewDelegate,UICollectionViewDataSource>{
  15. BOOL beganDragging;
  16. }
  17. @property(strong,nonatomic)UICollectionView *myCollectionV;
  18. @property(strong,nonatomic)UICollectionViewFlowLayout *flowL ;
  19. @property(strong,nonatomic)WMZBannerControl *bannerControl ;
  20. @property(strong,nonatomic)NSArray *data;
  21. @property(strong,nonatomic)WMZBannerParam *param;
  22. @property(strong,nonatomic)NSTimer *timer;
  23. @property(strong,nonatomic)UIView *line;
  24. @property(assign,nonatomic)NSInteger lastIndex;
  25. @end
  26. @implementation WMZBannerView
  27. - (instancetype)initConfigureWithModel:(WMZBannerParam *)param withView:(UIView*)parentView{
  28. if (self = [super init]) {
  29. self.param = param;
  30. if (parentView) {
  31. [parentView addSubview:self];
  32. }
  33. self.param.wFrame = CGRectMake(self.param.wFrame.origin.x,
  34. self.param.wFrame.origin.y,
  35. (int)self.param.wFrame.size.width,
  36. (int)self.param.wFrame.size.height);
  37. [self setFrame:self.param.wFrame];
  38. self.data = [NSArray arrayWithArray:self.param.wData];
  39. [self setUp];
  40. }
  41. return self;
  42. }
  43. /**
  44. * 调用方法
  45. *
  46. */
  47. - (instancetype)initConfigureWithModel:(WMZBannerParam *)param{
  48. if (self = [super init]) {
  49. self.param = param;
  50. self.param.wFrame = CGRectMake(self.param.wFrame.origin.x,
  51. self.param.wFrame.origin.y,
  52. (int)self.param.wFrame.size.width,
  53. (int)self.param.wFrame.size.height);
  54. [self setFrame:self.param.wFrame];
  55. self.data = [NSArray arrayWithArray:self.param.wData];
  56. [self setUp];
  57. }
  58. return self;
  59. }
  60. - (void)updateUI{
  61. self.data = [NSArray arrayWithArray:self.param.wData];
  62. [self resetCollection];
  63. }
  64. - (void)resetCollection{
  65. self.bannerControl.numberOfPages = self.data.count;
  66. [UIView animateWithDuration:0.0 animations:^{
  67. [self.myCollectionV reloadData];
  68. if (self.param.wSelectIndex|| self.param.wRepeat) {
  69. NSIndexPath *path = [NSIndexPath indexPathForRow: self.param.wRepeat?((COUNT/2)*self.data.count+self.param.wSelectIndex):self.param.wSelectIndex inSection:0];
  70. [self scrolToPath:path animated:NO];
  71. self.bannerControl.currentPage = self.param.wSelectIndex;
  72. self.param.myCurrentPath = self.param.wRepeat?((COUNT/2)*self.data.count+self.param.wSelectIndex):self.param.wSelectIndex;
  73. if (self.param.wAutoScroll) {
  74. [self createTimer];
  75. }else{
  76. [self cancelTimer];
  77. }
  78. }
  79. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  80. [self scrollEnd:[NSIndexPath indexPathForRow: self.param.wRepeat?((COUNT/2)*self.data.count+self.param.wSelectIndex):self.param.wSelectIndex inSection:0]];
  81. });
  82. } completion:^(BOOL finished) {}];
  83. if (self.param.wSpecialStyle == SpecialStyleLine&&self.param.wData.count) {
  84. [self addSubview:self.line];
  85. self.line.hidden = NO;
  86. self.line.backgroundColor = [UIColor redColor];
  87. if (self.param.wSpecialCustumLine) {
  88. self.param.wSpecialCustumLine(self.line);
  89. }
  90. CGFloat lineHeight = self.line.frame.size.height?:2;
  91. CGFloat lineWidth = self.param.wFrame.size.width/self.param.wData.count;
  92. self.line.frame = CGRectMake(0, self.param.wFrame.size.height -lineHeight, lineWidth, lineHeight);
  93. }else{
  94. self.line.hidden = YES;
  95. }
  96. }
  97. - (void)setUp{
  98. if (self.data&&self.data.count==1) {
  99. self.param.wRepeat = NO;
  100. self.param.wAutoScroll = NO;
  101. }
  102. if (self.param.wMarquee) {
  103. self.param.wAutoScroll = YES;
  104. self.param.wHideBannerControl = YES;
  105. self.param.wAutoScrollSecond = 0.05f;
  106. self.param.wRepeat = YES;
  107. }
  108. if (self.param.wScreenScale<1&&self.param.wScreenScale>0) {
  109. CGRect rect = self.param.wFrame;
  110. rect.origin.x = rect.origin.x * self.param.wScreenScale;
  111. rect.origin.y = rect.origin.y * self.param.wScreenScale;
  112. rect.size.width = rect.size.width * self.param.wScreenScale;
  113. rect.size.height = rect.size.height * self.param.wScreenScale;
  114. self.param.wFrame = rect;
  115. self.frame = self.param.wFrame;
  116. CGSize size = self.param.wItemSize;
  117. size.width *= self.param.wScreenScale;
  118. size.height *= self.param.wScreenScale;
  119. self.param.wItemSize = size;
  120. self.param.wLineSpacing*=self.param.wScreenScale;
  121. UIEdgeInsets sets = self.param.wSectionInset;
  122. sets.top*=self.param.wScreenScale;
  123. sets.right*=self.param.wScreenScale;
  124. sets.bottom*=self.param.wScreenScale;
  125. sets.left*=self.param.wScreenScale;
  126. self.param.wSectionInset = sets;
  127. }
  128. if (self.param.wItemSize.height == 0 || self.param.wItemSize.width == 0 ) {
  129. self.param.wItemSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
  130. }
  131. else if(self.param.wItemSize.height>self.frame.size.height){
  132. self.param.wItemSize = CGSizeMake(self.param.wItemSize.width, self.frame.size.height);
  133. }else if(self.param.wItemSize.width>self.frame.size.width){
  134. self.param.wItemSize = CGSizeMake(self.frame.size.width, self.param.wItemSize.height);
  135. }
  136. self.param.wItemSize = CGSizeMake((int)self.param.wItemSize.width, (int)self.param.wItemSize.height);
  137. if (self.param.wFadeOpen) {
  138. self.flowL = [[WMZBannerFadeLayout alloc] initConfigureWithModel:self.param];
  139. }else if (self.param.wCardOverLap) {
  140. self.param.wRepeat = YES;
  141. if (self.param.wScaleFactor == 0.5) {
  142. self.param.wScaleFactor = 0.8f;
  143. }
  144. self.flowL = [[WMZBannerOverLayout alloc] initConfigureWithModel:self.param];
  145. }else{
  146. self.flowL = [[WMZBannerFlowLayout alloc] initConfigureWithModel:self.param];
  147. }
  148. [self addSubview:self.myCollectionV];
  149. self.myCollectionV.scrollEnabled = self.param.wCanFingerSliding;
  150. [self.myCollectionV registerClass:[Collectioncell class] forCellWithReuseIdentifier:NSStringFromClass([Collectioncell class])];
  151. [self.myCollectionV registerClass:[CollectionTextCell class] forCellWithReuseIdentifier:NSStringFromClass([CollectionTextCell class])];
  152. if (self.param.wMyCellClassName) {
  153. [self.myCollectionV registerClass:NSClassFromString(self.param.wMyCellClassName) forCellWithReuseIdentifier:self.param.wMyCellClassName];
  154. }
  155. self.myCollectionV.frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height);
  156. self.myCollectionV.pagingEnabled = (self.param.wItemSize.width == self.myCollectionV.frame.size.width && self.param.wLineSpacing == 0)||self.param.wVertical;
  157. if ([self.myCollectionV isPagingEnabled]) {
  158. self.myCollectionV.decelerationRate = UIScrollViewDecelerationRateNormal;
  159. }
  160. self.bannerControl = [[WMZBannerControl alloc]initWithFrame:CGRectMake((self.bounds.size.width - 80)/2 , self.bounds.size.height - 30,80, 30) WithModel:self.param];
  161. if (self.param.wCustomControl) {
  162. self.param.wCustomControl(self.bannerControl);
  163. }
  164. if (!self.param.wHideBannerControl) {
  165. [self addSubview:self.bannerControl];
  166. }
  167. self.bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height*self.param.wEffectHeight)];
  168. self.bgImgView.contentMode = self.param.wImageFill?UIViewContentModeScaleAspectFill:UIViewContentModeScaleToFill;
  169. [self addSubview:self.bgImgView];
  170. [self sendSubviewToBack:self.bgImgView];
  171. self.bgImgView.hidden = !self.param.wEffect;
  172. self.bgImgView.layer.masksToBounds = YES;
  173. UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  174. UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
  175. effectView.frame = self.bgImgView.bounds;
  176. [self.bgImgView addSubview:effectView];
  177. [self resetCollection];
  178. }
  179. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  180. NSInteger index = self.param.wRepeat?indexPath.row%self.data.count:indexPath.row;
  181. id dic = self.data[index];
  182. UICollectionViewCell *tmpCell = nil;
  183. if (self.param.wMyCell) {
  184. tmpCell = self.param.wMyCell([NSIndexPath indexPathForRow:index inSection:indexPath.section], collectionView, dic,self.bgImgView,self.data);
  185. }else{
  186. //默认视图
  187. Collectioncell *cell = (Collectioncell *)[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([Collectioncell class]) forIndexPath:indexPath];
  188. cell.param = self.param;
  189. if ([dic isKindOfClass:[NSDictionary class]]) {
  190. [self setIconData:cell.icon withData:dic[self.param.wDataParamIconName]];
  191. }else{
  192. [self setIconData:cell.icon withData:dic];
  193. }
  194. tmpCell = cell;
  195. }
  196. return tmpCell;
  197. }
  198. - (void)setIconData:(UIImageView*)icon withData:(id)data{
  199. if (!data) return;
  200. if ([data isKindOfClass:[NSString class]]) {
  201. if ([(NSString*)data hasPrefix:@"http"]) {
  202. [icon sd_setImageWithURL:[NSURL URLWithString:(NSString*)data] placeholderImage:self.param.wPlaceholderImage?[UIImage imageNamed:self.param.wPlaceholderImage]:nil];
  203. }else{
  204. icon.image = [UIImage imageNamed:(NSString*)data];
  205. }
  206. }
  207. }
  208. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  209. {
  210. return self.param.wRepeat?self.data.count*COUNT:self.data.count;
  211. }
  212. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  213. if (self.param.wEventClick) {
  214. NSInteger index = self.param.wRepeat?indexPath.row%self.data.count:indexPath.row;
  215. id dic = self.data[index];
  216. self.param.wEventClick(dic, index);
  217. }
  218. if (self.param.wEventCenterClick) {
  219. NSInteger index = self.param.wRepeat?indexPath.row%self.data.count:indexPath.row;
  220. id dic = self.data[index];
  221. BOOL center = [self checkCellInCenterCollectionView:collectionView AtIndexPath:indexPath];
  222. UICollectionViewCell *currentCell = (UICollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
  223. self.param.wEventCenterClick(dic, index,center,currentCell);
  224. }
  225. if (self.param.wClickCenter) {
  226. NSArray *visibleCellIndex = [collectionView visibleCells];
  227. NSArray *sortedIndexPaths = [visibleCellIndex sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  228. NSIndexPath *path1 = (NSIndexPath *)[collectionView indexPathForCell:obj1];
  229. NSIndexPath *path2 = (NSIndexPath *)[collectionView indexPathForCell:obj2];
  230. return [path1 compare:path2];
  231. }];
  232. if (sortedIndexPaths.count>0) {
  233. NSInteger center = sortedIndexPaths.count/2;
  234. UICollectionViewCell *tmpCell = [collectionView cellForItemAtIndexPath:indexPath];
  235. for (int i = 0; i < sortedIndexPaths.count; i++) {
  236. UICollectionViewCell *cell = sortedIndexPaths[i];
  237. if (cell == tmpCell) {
  238. NSIndexPath *nextIndexPath = nil;
  239. if (i>center || i<center) {
  240. nextIndexPath = [NSIndexPath indexPathForItem: indexPath.row inSection:0];
  241. self.param.myCurrentPath = indexPath.row;
  242. [self scrolToPath:nextIndexPath animated:YES];
  243. [collectionView setUserInteractionEnabled:NO];
  244. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  245. [collectionView setUserInteractionEnabled:YES];
  246. });
  247. }
  248. break;
  249. }
  250. }
  251. }
  252. }
  253. }
  254. /*
  255. 检测是否是中间的cell 当前判断依据为最大的cell 如果cell大小一样 那么取显示的first第一个
  256. */
  257. - (BOOL)checkCellInCenterCollectionView:(UICollectionView *)collectionView AtIndexPath:(NSIndexPath *)indexPath{
  258. BOOL center = NO;
  259. NSMutableArray *arr = [NSMutableArray new];
  260. NSMutableArray *indexArr = [NSMutableArray new];
  261. for (int i = 0; i<[collectionView visibleCells].count; i++) {
  262. UICollectionViewCell *cell = [collectionView visibleCells][i];
  263. [arr addObject:[NSString stringWithFormat:@"%.0f",cell.frame.size.height]];
  264. [indexArr addObject:cell];
  265. }
  266. float max = [[arr valueForKeyPath:@"@max.floatValue"] floatValue];
  267. NSInteger cellIndex = [arr indexOfObject:[NSString stringWithFormat:@"%.0f",max]];
  268. if (cellIndex == NSNotFound) {
  269. if (arr.count%2 == 0) {
  270. cellIndex = arr.count/2 ;
  271. }else{
  272. cellIndex = arr.count/2+1 ;
  273. }
  274. }
  275. if (cellIndex<indexArr.count) {
  276. UICollectionViewCell *cell = indexArr[cellIndex];
  277. UICollectionViewCell *currentCell = (UICollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
  278. if (cell == currentCell) {
  279. center = YES;
  280. }
  281. }
  282. return center;
  283. }
  284. //滚动处理
  285. - (void)scrolToPath:(NSIndexPath*)path animated:(BOOL)animated{
  286. if (self.param.wRepeat?(path.row> self.data.count*COUNT-1):(path.row> self.data.count-1)){
  287. [self cancelTimer];
  288. return;
  289. }
  290. if (self.data.count==0) return;
  291. if (self.param.wCardOverLap||self.param.wFadeOpen) {
  292. [self.myCollectionV setContentOffset: self.param.wVertical?
  293. CGPointMake(0, path.row *self.myCollectionV.bounds.size.height):
  294. CGPointMake(path.row *self.myCollectionV.bounds.size.width, 0)
  295. animated:animated];
  296. }else{
  297. if ([self.myCollectionV isPagingEnabled]) {
  298. [self.myCollectionV scrollToItemAtIndexPath:path atScrollPosition:
  299. self.param.wVertical?UICollectionViewScrollPositionCenteredVertically:
  300. UICollectionViewScrollPositionCenteredHorizontally animated:animated];
  301. }else{
  302. [self.myCollectionV scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:animated];
  303. }
  304. }
  305. if ([self.myCollectionV isPagingEnabled]||self.param.wCardOverLap) return;
  306. if(self.param.wContentOffsetX>0.5){
  307. self.myCollectionV.contentOffset = CGPointMake(self.myCollectionV.contentOffset.x-(self.param.wContentOffsetX-0.5)*self.myCollectionV.frame.size.width, self.myCollectionV.contentOffset.y);
  308. }else if(self.param.wContentOffsetX<0.5){
  309. self.myCollectionV.contentOffset = CGPointMake(self.myCollectionV.contentOffset.x+self.myCollectionV.frame.size.width *(0.5-self.param.wContentOffsetX), self.myCollectionV.contentOffset.y);
  310. }
  311. }
  312. //定时器
  313. - (void)createTimer{
  314. if (!self.timer) {
  315. SEL sel = NSSelectorFromString(self.param.wMarquee?@"autoMarqueenScrollAction":@"autoScrollAction");
  316. self.timer = [NSTimer scheduledTimerWithTimeInterval:self.param.wAutoScrollSecond target:self selector:sel userInfo:nil repeats:YES];
  317. [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  318. }
  319. }
  320. //定时器方法 自动滚动
  321. - (void)autoScrollAction{
  322. if (beganDragging) return;
  323. if (!self.superview) return;
  324. if (!self.param.wAutoScroll) {
  325. [self cancelTimer];
  326. return;
  327. }
  328. self.param.myCurrentPath+=1;
  329. if (self.param.wRepeat&& self.param.myCurrentPath == (self.data.count*COUNT)) {
  330. self.param.myCurrentPath = 0;
  331. }
  332. else if(!self.param.wRepeat&& self.param.myCurrentPath == self.data.count){
  333. [self cancelTimer];
  334. return;
  335. }
  336. NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem: self.param.myCurrentPath inSection:0];
  337. [self scrolToPath:nextIndexPath animated:YES];
  338. }
  339. //定时器方法 跑马灯
  340. - (void)autoMarqueenScrollAction{
  341. if (!self.superview) return;
  342. if (!self.param.wAutoScroll) {
  343. [self cancelTimer];
  344. return;
  345. }
  346. if (self.param.wVertical) {
  347. CGFloat OffsetY = self.myCollectionV.contentOffset.y + self.param.wMarqueeRate;
  348. if (OffsetY >self.myCollectionV.contentSize.height) {
  349. OffsetY = self.myCollectionV.contentSize.height/2;
  350. }
  351. [self.myCollectionV setContentOffset:CGPointMake(self.myCollectionV.contentOffset.x, OffsetY) animated:NO];
  352. }else{
  353. CGFloat OffsetX = self.myCollectionV.contentOffset.x + self.param.wMarqueeRate;
  354. if (OffsetX >self.myCollectionV.contentSize.width) {
  355. OffsetX = self.myCollectionV.contentSize.width/2;
  356. }
  357. [self.myCollectionV setContentOffset:CGPointMake(OffsetX, self.myCollectionV.contentOffset.y) animated:NO];
  358. }
  359. }
  360. //定时器销毁
  361. - (void)cancelTimer{
  362. if (self.timer) {
  363. [self.timer invalidate];
  364. self.timer = nil;
  365. }
  366. }
  367. //开始拖动
  368. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  369. beganDragging = YES;
  370. if (!self.param.wMarquee) {
  371. if (self.param.wAutoScroll) {
  372. [self cancelTimer];
  373. }
  374. }
  375. }
  376. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  377. if (self.param.wCardOverLap||self.param.wFadeOpen) {
  378. if ([self.myCollectionV isPagingEnabled]&&!self.param.wMarquee) {
  379. NSInteger index = self.param.wRepeat?self.param.myCurrentPath%self.data.count:self.param.myCurrentPath;
  380. self.bannerControl.currentPage = self.param.wRepeat?index %self.data.count:index;
  381. }
  382. }else{
  383. if ([self.myCollectionV isPagingEnabled]&&!self.param.wMarquee) {
  384. NSInteger index = self.param.wVertical?
  385. scrollView.contentOffset.y/scrollView.frame.size.height:
  386. scrollView.contentOffset.x/scrollView.frame.size.width;
  387. self.param.myCurrentPath = index;
  388. self.bannerControl.currentPage = self.param.wRepeat?index %self.data.count:index;
  389. }else{
  390. self.bannerControl.currentPage = self.param.wRepeat?self.param.myCurrentPath %self.data.count:self.param.myCurrentPath;
  391. }
  392. }
  393. [self setUpSpecialFrame];
  394. }
  395. //拖动结束
  396. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
  397. beganDragging = NO;
  398. }
  399. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  400. if (!self.param.wMarquee) {
  401. if (![self.myCollectionV isPagingEnabled]) {
  402. self.bannerControl.currentPage = self.param.wRepeat?self.param.myCurrentPath%self.data.count:self.param.myCurrentPath;
  403. }
  404. if (self.param.wAutoScroll) {
  405. [self createTimer];
  406. }
  407. }
  408. [self setUpSpecialFrame];
  409. [self scrollEnd:[NSIndexPath indexPathForRow:self.param.myCurrentPath inSection:0]];
  410. [self fadeAction];
  411. }
  412. - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
  413. if (self.param.wCardOverLap) {
  414. self.param.myCurrentPath = self.param.wVertical?
  415. MAX(floor(scrollView.contentOffset.y / scrollView.bounds.size.height ), 0):
  416. MAX(floor(scrollView.contentOffset.x / scrollView.bounds.size.width ), 0);
  417. }
  418. [self scrollEnd:[NSIndexPath indexPathForRow:self.param.myCurrentPath inSection:0]];
  419. [self setUpSpecialFrame];
  420. [self fadeAction];
  421. }
  422. - (void)scrollEnd:(NSIndexPath*)indexPath{
  423. if (!self.data.count) return;
  424. if (self.param.wMarquee) return;
  425. NSInteger current = MAX(self.param.myCurrentPath, 0);
  426. NSInteger index = self.param.wRepeat?current%self.data.count:current;
  427. if (index>self.data.count-1) {
  428. index = 0;
  429. }
  430. //取上一张
  431. id dic = self.data[index];
  432. if (self.param.wEventScrollEnd) {
  433. BOOL center = [self checkCellInCenterCollectionView:self.myCollectionV AtIndexPath:indexPath];
  434. UICollectionViewCell *currentCell = (UICollectionViewCell*)[self.myCollectionV cellForItemAtIndexPath:indexPath];
  435. self.param.wEventScrollEnd(dic, index, center,currentCell);
  436. }
  437. if (self.param.wEffect) {
  438. if ([dic isKindOfClass:[NSDictionary class]]) {
  439. [self setIconData:self.bgImgView withData:dic[self.param.wDataParamIconName]];
  440. }else{
  441. [self setIconData:self.bgImgView withData:dic];
  442. }
  443. }
  444. self.bannerControl.currentPage = index;
  445. self.lastIndex = current;
  446. }
  447. //淡入淡出
  448. - (void)fadeAction{
  449. if (self.param.wFadeOpen) {
  450. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  451. WMZBannerFadeLayout *fade = (WMZBannerFadeLayout*)self.flowL;
  452. if (![fade isKindOfClass:[WMZBannerFadeLayout class]]) {
  453. return;
  454. }
  455. NSInteger current = MAX(self.param.myCurrentPath, 0);
  456. NSInteger index = self.param.wRepeat?current%self.data.count:current;
  457. self.bannerControl.currentPage = index;
  458. NSInteger itemsCount = [self.myCollectionV numberOfItemsInSection:0];
  459. NSInteger showIndex = MIN(itemsCount-1, MAX(0, current));
  460. NSInteger hideIndex = fade.right?MAX(showIndex-1, 0):MIN(showIndex+1, itemsCount-1);
  461. NSIndexPath *showIndexPath = [NSIndexPath indexPathForRow:showIndex inSection:0];
  462. NSIndexPath *hideIndexPath = [NSIndexPath indexPathForRow:hideIndex inSection:0];
  463. [self showAninationWithView:[self.myCollectionV cellForItemAtIndexPath:showIndexPath]];
  464. [self hideAninationWithView:[self.myCollectionV cellForItemAtIndexPath:hideIndexPath]];
  465. });
  466. }
  467. }
  468. //更新下划线位置
  469. - (void)setUpSpecialFrame{
  470. if (!self.param.wSpecialStyle) return;
  471. if (!self.data.count) return;
  472. if (self.param.wSpecialStyle == SpecialStyleLine) {
  473. [UIView animateWithDuration:0.5 animations:^{
  474. CGRect rect = self.line.frame;
  475. rect.origin.x = (self.param.wRepeat?self.param.myCurrentPath%self.data.count:self.param.myCurrentPath)*rect.size.width;
  476. self.line.frame = rect;
  477. }];
  478. }
  479. }
  480. - (void)showAninationWithView:(UIView*)view{
  481. [view.layer removeAllAnimations];
  482. CABasicAnimation *scale = [CABasicAnimation animation];
  483. scale.keyPath = @"transform.scale";
  484. scale.fromValue = [NSNumber numberWithFloat:1.3];
  485. scale.toValue = [NSNumber numberWithFloat:1.0];
  486. CABasicAnimation *showViewAnn = [CABasicAnimation animationWithKeyPath:@"opacity"];
  487. showViewAnn.fromValue = [NSNumber numberWithFloat:0.5];
  488. showViewAnn.toValue = [NSNumber numberWithFloat:1];
  489. CAAnimationGroup *group = [CAAnimationGroup animation];
  490. group.animations = @[scale, showViewAnn];
  491. group.duration = 0.6;
  492. [view.layer addAnimation:group forKey:nil];
  493. }
  494. - (void)hideAninationWithView:(UIView*)view{
  495. [view.layer removeAllAnimations];
  496. CABasicAnimation *scale = [CABasicAnimation animation];
  497. scale.keyPath = @"transform.scale";
  498. scale.fromValue = [NSNumber numberWithFloat:1];
  499. scale.toValue = [NSNumber numberWithFloat:1.3];
  500. CABasicAnimation *showViewAnn = [CABasicAnimation animationWithKeyPath:@"opacity"];
  501. showViewAnn.fromValue = [NSNumber numberWithFloat:1];
  502. showViewAnn.toValue = [NSNumber numberWithFloat:0];
  503. CAAnimationGroup *group = [CAAnimationGroup animation];
  504. group.animations = @[scale, showViewAnn];
  505. group.duration = 0.6;
  506. [view.layer addAnimation:group forKey:nil];
  507. }
  508. - (UICollectionView *)myCollectionV{
  509. if (!_myCollectionV) {
  510. _myCollectionV = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:self.flowL];
  511. _myCollectionV.delegate = self;
  512. _myCollectionV.dataSource = self;
  513. _myCollectionV.showsVerticalScrollIndicator = NO;
  514. _myCollectionV.showsHorizontalScrollIndicator = NO;
  515. _myCollectionV.backgroundColor = [UIColor clearColor];
  516. _myCollectionV.decelerationRate = _param.wDecelerationRate;
  517. }
  518. return _myCollectionV;
  519. }
  520. - (WMZBannerControl *)bannerControl{
  521. if (!_bannerControl) {
  522. _bannerControl = [[WMZBannerControl alloc]initWithFrame:CGRectZero WithModel:_param];
  523. }
  524. return _bannerControl;
  525. }
  526. - (UIView *)line{
  527. if (!_line) {
  528. _line = [UIView new];
  529. }
  530. return _line;
  531. }
  532. - (void)dealloc{
  533. //单纯调用这里无法消除定时器
  534. [self cancelTimer];
  535. }
  536. //要配合这里调用
  537. - (void)willMoveToSuperview:(UIView *)newSuperview {
  538. [super willMoveToSuperview:newSuperview];
  539. if (!newSuperview &&self.timer) {
  540. // 销毁定时器
  541. [self.timer invalidate];
  542. self.timer = nil;
  543. }
  544. }
  545. @end
  546. @implementation Collectioncell
  547. -(instancetype)initWithFrame:(CGRect)frame
  548. {
  549. self = [super initWithFrame:frame];
  550. if (self){
  551. self.icon = [UIImageView new];
  552. self.icon.layer.masksToBounds = YES;
  553. [self.contentView addSubview:self.icon];
  554. self.icon.frame = self.contentView.bounds;
  555. self.contentView.layer.masksToBounds = YES;
  556. self.contentView.layer.cornerRadius = 5;
  557. }
  558. return self;
  559. }
  560. - (void)setParam:(WMZBannerParam *)param{
  561. _param = param;
  562. self.icon.contentMode = param.wImageFill?UIViewContentModeScaleAspectFill:UIViewContentModeScaleToFill;
  563. }
  564. @end
  565. @implementation CollectionTextCell
  566. -(instancetype)initWithFrame:(CGRect)frame
  567. {
  568. self = [super initWithFrame:frame];
  569. if (self){
  570. self.contentView.backgroundColor = [UIColor whiteColor];
  571. self.label = [UILabel new];
  572. self.label.font = [UIFont systemFontOfSize:17.0];
  573. self.label.textColor = [UIColor redColor];
  574. [self.contentView addSubview:self.label];
  575. self.label.frame = CGRectMake(10, 0, frame.size.width-20, frame.size.height);
  576. }
  577. return self;
  578. }
  579. - (void)setParam:(WMZBannerParam *)param{
  580. _param = param;
  581. self.label.textColor = self.param.wMarqueeTextColor;
  582. }
  583. @end