-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDZBannerView.m
More file actions
662 lines (594 loc) · 20.9 KB
/
Copy pathCDZBannerView.m
File metadata and controls
662 lines (594 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//
// CDZBannerView.m
//
//
// Created by baight on 13-5-29.
// Copyright (c) 2013年 baight. All rights reserved.
//
#import "CDZBannerView.h"
#import <QuartzCore/QuartzCore.h>
#import "UIImageView+WebCache.h"
#define OFFSET_X 7.0f
#define SPACE_V 5.0f
#define HEIGHT 50.0f
#define TEXT_COLOR [UIColor whiteColor]
// enormously modified by zhengchen2
// 想要实现 UIScrollView 的循环滚动,解决方法(http://www.cnblogs.com/ydhliphonedev/archive/2012/05/03/2480256.html)如下:
/*
首先,你需要准备三屏的数据
以手指向右拖动为例,【屏幕】指的是scorllview的显示区域
article1 article2 article3
【屏幕】
拖动以后成这样:
article1 article2 article3
【屏幕】
将article3放到第一个去(设定article3的frame),这是屏幕还显示的是article1的内容
article3 article1 article2
【屏幕】
将屏幕移到中间:使用setContentOffset,禁用动画,这样骗过人眼
article3 article1 article2
【屏幕】
最后更新指针顺序:
article1 article2 article3
【屏幕】
无缝循环实现了。
by gqzhu
*/
@implementation CDZBannerView
-(id)init{
if(self = [super initWithFrame:CGRectMake(0, 0, 100, 100)]){
}
return self;
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self myInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame delegate:(id<CDZBannerViewDelegate>)delegate{
self = [super initWithFrame:frame];
if (self) {
[self setDelegate:delegate];
[self myInit];
}
return self;
}
- (id)initWithFrame:(CGRect)frame delegate:(id<CDZBannerViewDelegate>)delegate imageArray:(NSArray*)array{
self = [super initWithFrame:frame];
if (self) {
self.delegate = delegate;
_imageArray = array;
[self myInit];
[self updateData];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self){
[self myInit];
}
return self;
}
-(void)myInit{
_canCycleScroll = YES;
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.scrollsToTop = NO;
_scrollView.userInteractionEnabled = NO;
_scrollView.showsHorizontalScrollIndicator = NO; // 不显示水平滚动条
_scrollView.showsVerticalScrollIndicator = NO; // 不显示垂直滚动条
_scrollView.pagingEnabled = YES; // 整页翻动
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width*3,_scrollView.frame.size.height); // 总共三屏数据
_scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0); // 显示第二屏
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[self addSubview:_scrollView];
_pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - 30, self.bounds.size.width, 20)];
_pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_pageControl.userInteractionEnabled = NO;
[self addSubview:_pageControl];
_screen0 = [[UIImageView alloc] init];
_screen0.clipsToBounds = YES;
[_screen0 setFrame:CGRectMake(0, 0,self.bounds.size.width, self.bounds.size.height)];
_screen0.tag=0;
[_scrollView addSubview:_screen0];
_screen1 = [[UIImageView alloc] init];
_screen1.clipsToBounds = YES;
[_screen1 setFrame:CGRectMake(self.bounds.size.width, 0,self.bounds.size.width, self.bounds.size.height)];
_screen1.tag=1;
[_scrollView addSubview:_screen1];
_screen2 = [[UIImageView alloc] init];
_screen2.clipsToBounds = YES;
[_screen2 setFrame:CGRectMake(self.bounds.size.width*2, 0,self.bounds.size.width, self.bounds.size.height)];
_screen2.tag=2;
[_scrollView addSubview:_screen2];
}
// 跳转到下一页数据
-(void)switchToNextPage{
// 正在拖动
if(_scrollView.isDragging == true){
return;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchToNextPage) object:nil];
if(_canCycleScroll || (_currentPage != 0 && _currentPage != _imageArray.count-1)){
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width*2, 0) animated:YES];
}
else{
[_scrollView setContentOffset:CGPointMake(MIN(_scrollView.contentOffset.x + _scrollView.frame.size.width, _scrollView.frame.size.width*2), 0) animated:YES];
}
// 在动画结束后,调用 [self update3Screens]方法,并无动画滚回第二屏
_currentPage++;
if(_currentPage >= _imageArray.count){
if(_canCycleScroll){
_currentPage = 0;
}
else{
_currentPage = _imageArray.count - 1;
}
}
if(_timeInterval > 0){
[self performSelector:@selector(switchToNextPage) withObject:nil afterDelay:_timeInterval];
}
}
-(void)switchToLastPage{
// 正在拖动
if(_scrollView.isDragging == true){
return;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchToNextPage) object:nil];
if(_canCycleScroll || (_currentPage != 0 && _currentPage != _imageArray.count-1)){
[_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
else{
[_scrollView setContentOffset:CGPointMake(MAX(_scrollView.contentOffset.x - _scrollView.frame.size.width, 0), 0) animated:YES];
}
// 在动画结束后,调用 [self update3Screens]方法,并无动画滚回第二屏
_currentPage--;
if(_currentPage < 0){
if(_canCycleScroll){
_currentPage = _imageArray.count - 1;
}
else{
_currentPage = 0;
}
}
if(_timeInterval > 0){
[self performSelector:@selector(switchToNextPage) withObject:nil afterDelay:_timeInterval];
}
}
-(void)setBounds:(CGRect)bounds{
[super setBounds:bounds];
_scrollView.frame = CGRectMake(0, 0, bounds.size.width, bounds.size.height);
_scrollView.contentSize = CGSizeMake(bounds.size.width*3,bounds.size.height); // 总共三屏数据
_screen0.frame = CGRectMake(0,0,bounds.size.width,bounds.size.height);
_screen1.frame = CGRectMake(bounds.size.width,0,bounds.size.width,bounds.size.height);
_screen2.frame = CGRectMake(bounds.size.width*2,0,bounds.size.width,bounds.size.height);
[self updateData];
}
-(void)setFrame:(CGRect)frame{
[super setFrame:frame];
_scrollView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
_scrollView.contentSize = CGSizeMake(frame.size.width*3,frame.size.height); // 总共三屏数据
_screen0.frame = CGRectMake(0,0,frame.size.width,frame.size.height);
_screen1.frame = CGRectMake(frame.size.width,0,frame.size.width,frame.size.height);
_screen2.frame = CGRectMake(frame.size.width*2,0,frame.size.width,frame.size.height);
[self updateData];
}
- (void)didMoveToWindow {
if (self.window) {
if(_timeInterval > 0){
[self performSelector:@selector(switchToNextPage) withObject:nil afterDelay:_timeInterval];
}
}
else {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchToNextPage) object:nil];
}
}
#pragma mark - Event Response
- (void)singleTapGestureRecognizerForDatailClick:(UIGestureRecognizer*)gestureRecognizer{
if (_currentPage > -1 && _currentPage < _imageArray.count) {
if ([self.delegate respondsToSelector:@selector(bannerView:clickIndex:)]) {
[self.delegate bannerView:self clickIndex:_currentPage];
}
}
}
#pragma mark - UIScrollViewDelegate
// 动画停止
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
// 无动画滚回第二屏
[self update3Screens];
if(_canCycleScroll){
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:NO];
}
else{
if(_currentPage == 0){
[_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}
else if(_currentPage == _imageArray.count - 1){
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width*2, 0) animated:NO];
}
else{
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:NO];
}
}
_pageControl.currentPage = _currentPage;
if([_delegate respondsToSelector:@selector(bannerView:didSwithToPage:)]){
[_delegate bannerView:self didSwithToPage:self.currentPage];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchToNextPage) object:nil];
if(_timeInterval > 0){
[self performSelector:@selector(switchToNextPage) withObject:nil afterDelay:_timeInterval];
}
if (decelerate) {
scrollView.userInteractionEnabled = NO;
}
}
// 手动拖动跳转并跳转动画结束后,会调用此方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if(_imageArray.count == 0){
return;
}
scrollView.userInteractionEnabled = YES;
if(scrollView.contentOffset.x < scrollView.bounds.size.width/2){ // 在第一屏
_currentPage--;
}
else if(scrollView.contentOffset.x > scrollView.bounds.size.width*1.5){ // 在第三屏
_currentPage++;
}
else{ // 在第二屏
if(_currentPage == 0){
_currentPage++;
}
else if(_currentPage == _imageArray.count-1){
_currentPage--;
}
}
if(_currentPage < 0){
if(_canCycleScroll){
_currentPage = _imageArray.count-1;
}
else{
_currentPage = 0;
}
}
else if(_currentPage >= _imageArray.count){
if(_canCycleScroll){
_currentPage = 0;
}
else{
_currentPage = _imageArray.count - 1;
}
}
// 无动画滚回第二屏
[self update3Screens];
if(_canCycleScroll){
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:NO];
}
else{
if(_currentPage == 0){
[_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}
else if(_currentPage == _imageArray.count - 1){
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width*2, 0) animated:NO];
}
else{
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:NO];
}
}
_pageControl.currentPage = _currentPage;
if([_delegate respondsToSelector:@selector(bannerView:didSwithToPage:)]){
[_delegate bannerView:self didSwithToPage:self.currentPage];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 只有在拖动时,才会实时调整 _pageControl.currentPage
if(_scrollView.isDragging == false){
return;
}
// 实时调整 _pageControl.currentPage
NSInteger page;
if(scrollView.contentOffset.x < scrollView.bounds.size.width/2){ // 在第一屏
page = _currentPage-1;
}
else if(scrollView.contentOffset.x > scrollView.bounds.size.width*1.5){ // 在第三屏
page = _currentPage+1;
}
else{ // 第二屏
if(_currentPage == 0){
if(_canCycleScroll){
page = _currentPage;
}
else{
page = _currentPage+1;
}
}
else if(_currentPage == _imageArray.count-1){
if(_canCycleScroll){
page = _currentPage;
}
else{
page = _currentPage-1;
}
}
else{
page = _currentPage;
}
}
if(page < 0){
if(_canCycleScroll){
page = _imageArray.count-1;
}
else{
page = 0;
}
}
else if(page >= _imageArray.count){
if(_canCycleScroll){
page = 0;
}
else{
page = _imageArray.count-1;
}
}
_pageControl.currentPage = page;
}//*/
#pragma mark - Private Method
// 根据 _imageArray 更新 _pageControl 的宽度和页数,开启自动循环滚动
- (void)updateData{
_pageControl.numberOfPages = _imageArray.count;
_pageControl.currentPage = _currentPage;
[self update3Screens]; // 更新三屏数据
// 显示第二屏数据
if(_canCycleScroll){
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:NO];
}
else{
if(_currentPage == 0){
[_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}
else if(_currentPage == _imageArray.count-1){
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width*2, 0) animated:NO];
}
else{
[_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:NO];
}
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchToNextPage) object:nil];
if(_imageArray.count < 1){
_scrollView.userInteractionEnabled = NO;
_scrollView.scrollEnabled = NO;
_pageControl.hidden = YES;
}
else if(_imageArray.count == 1){
_scrollView.userInteractionEnabled = YES;
_scrollView.scrollEnabled = NO;
_pageControl.hidden = YES;
}
else{
_scrollView.userInteractionEnabled = YES;
_scrollView.scrollEnabled = YES;
_pageControl.hidden = NO;
if(_timeInterval > 0){
[self performSelector:@selector(switchToNextPage) withObject:nil afterDelay:_timeInterval];
}
}
}
// 根据 _currentPage 来更新三屏的数据
-(void)update3Screens{
if(_imageArray.count <= 0){
_screen0.image = _placeholderImage;
_screen1.image = _placeholderImage;
_screen2.image = _placeholderImage;
}
else{
[self updateScreen0];
[self updateScreen1];
[self updateScreen2];
}
}
-(void)updateScreen0{
id info;
if(_currentPage == 0){
if(_canCycleScroll){
info = [_imageArray objectAtIndex:_imageArray.count-1];
}
else{
info = [_imageArray objectAtIndex:0];
}
}
else if(_currentPage == _imageArray.count-1){
if(_canCycleScroll){
info = [_imageArray objectAtIndex:_currentPage-1];
}
else{
if(_imageArray.count >= 3){
info = [_imageArray objectAtIndex:_imageArray.count-3];
}
else{
info = [_imageArray firstObject];
}
}
}
else{
info = [_imageArray objectAtIndex:_currentPage-1];
}
[self updateScreen:_screen0 urlOrString:info];
}
-(void)updateScreen1{
id info;
if(_canCycleScroll){
info = [_imageArray objectAtIndex:_currentPage];
}
else{
if(_currentPage == 0){
if(_imageArray.count >= 2){
info = [_imageArray objectAtIndex:1];
}
else{
info = [_imageArray firstObject];
}
}
else if(_currentPage == _imageArray.count-1){
if(_imageArray.count >= 2){
info = [_imageArray objectAtIndex:_imageArray.count-2];
}
else{
info = [_imageArray lastObject];
}
}
else{
info = [_imageArray objectAtIndex:_currentPage];
}
}
[self updateScreen:_screen1 urlOrString:info];
}
-(void)updateScreen2{
id info;
if(_currentPage == 0){
if(_canCycleScroll){
if(_imageArray.count >= 2){
info = [_imageArray objectAtIndex:1];
}
else{
info = [_imageArray firstObject];
}
}
else{
if(_imageArray.count >= 3){
info = [_imageArray objectAtIndex:2];
}
else{
info = [_imageArray lastObject];
}
}
}
else if(_currentPage == _imageArray.count-1){ // 当前页为最后一页,所以第三屏数据应该为第一页数据
if(_canCycleScroll){
info = [_imageArray objectAtIndex:0];
}
else{
info = [_imageArray objectAtIndex:_imageArray.count-1];
}
}
else{
info = [_imageArray objectAtIndex:_currentPage+1];
}
[self updateScreen:_screen2 urlOrString:info];
}
-(void)updateScreen:(UIImageView*)screen urlOrString:(id)urlOrString{
if([urlOrString isKindOfClass:[NSURL class]]){ // 如果是 NSURL 类,则展示网络图片,网络图片未下载下来时,显示 属性placeholderImage图片
[self imageView:screen setImageWithUrl:urlOrString placeholderImage:_placeholderImage failureImage:_failureImage];
}
else if([urlOrString isKindOfClass:[NSString class]]){
NSString* str = urlOrString;
// 若是 NSString 类,并且 NSString 类是以 "http://" 开头的,将该NSString视为一个有效网络图片链接,如上。
if ([str hasPrefix:@"http"]){
[self imageView:screen setImageWithUrl:[NSURL URLWithString:str] placeholderImage:_placeholderImage failureImage:_failureImage];
}
// 若是 NSString 类,并且 NSString 类是不是以 "http://" 开头的,则将NSString视为一个有效本地图片名字,进行显示
else{
if(str.length > 0){
[screen setImage:[UIImage imageNamed:str]];
}
else{
[screen setImage:_placeholderImage];
}
}
}
}
- (void)imageView:(UIImageView*)imageView setImageWithUrl:(NSURL*)imageUrl placeholderImage:(UIImage*)placeholderImage failureImage:(UIImage*)failureImage{
if (failureImage == nil) {
failureImage = placeholderImage;
}
if (imageUrl) {
if (placeholderImage) {
[imageView setImage:placeholderImage];
}
__weak UIImageView* wself = imageView;
[[SDWebImageManager sharedManager] downloadImageWithURL:imageUrl options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (!wself) return;
dispatch_main_sync_safe(^{
__strong UIImageView *sself = wself;
if (!sself) return;
if (image) {
[sself setImage:image];
}
else {
[sself setImage:failureImage];
}
});
}];
} else {
[imageView setImage:failureImage];
}
}
#pragma mark - Getter And Setter
-(void)setCanClickImage:(BOOL)canClickImage{
_canClickImage = canClickImage;
if(_canClickImage){
if(_tapGestureRecognizer == nil){
UITapGestureRecognizer *tapGestureRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureRecognizerForDatailClick:)];
tapGestureRecognize.delegate = self;
tapGestureRecognize.numberOfTapsRequired = 1;
tapGestureRecognize.numberOfTouchesRequired = 1;
_tapGestureRecognizer = tapGestureRecognize;
}
[self addGestureRecognizer:_tapGestureRecognizer];
}
else{
if(_tapGestureRecognizer){
[self removeGestureRecognizer:_tapGestureRecognizer];
}
}
}
-(void)setContentMode:(UIViewContentMode)contentMode{
_screen0.contentMode = contentMode;
_screen1.contentMode = contentMode;
_screen2.contentMode = contentMode;
}
-(UIViewContentMode)contentMode{
return _screen1.contentMode;
}
-(void)setImageArray:(NSArray *)imageArray{
[self setImageArray:imageArray initPage:0];
}
-(void)setImageArray:(NSArray *)imageArray initPage:(NSInteger)initPage{
_imageArray = imageArray;
_currentPage = initPage;
[self updateData];
}
-(void)setCurrentPage:(NSInteger)currentPage{
if(_currentPage == currentPage){
return;
}
if(currentPage >= _imageArray.count){
currentPage = _imageArray.count - 1;
}
_currentPage = currentPage;
[self update3Screens];
_pageControl.currentPage = currentPage;
}
-(void)setPlaceholderImage:(UIImage *)placeholderImage{
_placeholderImage = placeholderImage;
if(_screen0.image == nil){
_screen0.image = placeholderImage;
}
if(_screen1.image == nil){
_screen1.image = placeholderImage;
}
if(_screen2.image == nil){
_screen2.image = placeholderImage;
}
}
-(void)setTimeInterval:(NSTimeInterval)timeInterval{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchToNextPage) object:nil];
_timeInterval = timeInterval;
if(_timeInterval > 0){
[self performSelector:@selector(switchToNextPage) withObject:nil afterDelay:_timeInterval];
}
}
@end