WMZBannerView.m 29 KB

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