iOS開發UI篇—無限輪播(循環利用)
一、無限輪播
1.簡單說明
在開發中常需要對廣告或者是一些圖檔進行自動的輪播,也就是所謂的無限滾動。
在開發的時候,我們通常的做法是使用一個UIScrollView,在UIScrollView上面添加多個imageView,然後設定imageView的圖檔,和scrollView的滾動範圍。
以前的做法:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuE2MwYmZkVTN4gTYkZWZ3kzYzkDOmVzNkhTY3kTY2QTNfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
一般而言,輪播的廣告或者是圖檔數量都不會太多(3~5張)。是以,并不會太多的去考慮性能問題。但是如果圖檔過多(比如有16張圖檔,就需要建立16個imageView),那麼就不得不考慮性能問題了。
更甚,如果深入做一個圖檔浏覽的小程式,那麼可能會處理成百上千張圖檔,這會造成極大的記憶體浪費且性能低下。
圖檔數量衆多:
當使用者在檢視第一張圖檔的時候,後面的7張建立的時間太早,且使用者可能根本就沒機會看見(看完前面幾張就沒有興趣再看後面的内容 了)。
優化思路:隻有在需要用到的時候,再建立,建立的imageView進行村循環利用。比較好的做法,不論有多少張圖檔,隻需要建立3個imageView就夠了。
本文介紹使用Collectionview來實作無限滾動的循環利用。它支援垂直和水準方向上的滾動。
二、實作
1.說明:
CollectionCell的用法和tableViewCell的用法不太一樣,CollectionCell
需要注冊,告訴它這種辨別對應的cell是什麼類型的cell,如果緩存池中沒有,那麼它就檢測當時這種辨別注冊的是什麼類型的cell,就會自動建立這種類型的Cell。
2.實作步驟
(1)向storyboard中添加一個UICollectionView,調整控件的寬高。
(2)設定其寬高==一張圖檔的寬高==其一個cell的寬高
設定cell的格子的大小。其預設為向上滾動的,調整為水準滾動。
1 //
2 // YYViewController.m
3 // 07-無限滾動(循環利用)
4 //
5 // Created by apple on 14-8-3.
6 // Copyright (c) 2014年 yangyong. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10
11 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
12 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
13
14 @end
15
16 @implementation YYViewController
17
18 - (void)viewDidLoad
19 {
20 [super viewDidLoad];
21 //注冊cell
22 static NSString *ID=@"cell";
23 [self.collectinView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
24
25 }
26
27 #pragma mark- UICollectionViewDataSource
28 //一共多少組,預設為1組
29 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
30 {
31 return 1;
32 }
33 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
34 {
35 return 16;
36 }
37
38 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
39 {
40 static NSString *ID=@"cell";
41 UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
42 cell.backgroundColor=YYRandomColor;
43 return cell;
44 }
45
46 #pragma mark-UICollectionViewDelegate
47 @end
界面展示:
列印檢視有沒有實作cell的循環利用。
可以看出,整個程式中隻建立了兩個cell。
(4)展示圖檔,自定義cell(兩種做法,可以使用xib也可以使用代碼)。
自定義一個cell,用來展示圖檔。
實作代碼:
YYimageCell.h檔案
1 //
2 // YYimageCell.h
3 // 07-無限滾動(循環利用)
4 //
5 // Created by apple on 14-8-3.
6 // Copyright (c) 2014年 yangyong. All rights reserved.
7 //
8
9 #import <UIKit/UIKit.h>
10
11 @interface YYimageCell : UICollectionViewCell
12 @property(nonatomic,copy)NSString *icon;
13 @end
YYimageCell.m檔案
1 //
2 // YYimageCell.m
3 // 07-無限滾動(循環利用)
4 //
5 // Created by apple on 14-8-3.
6 // Copyright (c) 2014年 yangyong. All rights reserved.
7 //
8
9 #import "YYimageCell.h"
10
11 @interface YYimageCell ()
12 @property(nonatomic,strong)UIImageView *imageView;
13 @end
14 @implementation YYimageCell
15
16 - (id)initWithFrame:(CGRect)frame
17 {
18 self = [super initWithFrame:frame];
19 if (self) {
20
21 UIImageView *imageView=[[UIImageView alloc]init];
22 [self addSubview:imageView];
23 self.imageView=imageView;
24 }
25 return self;
26 }
27
28 -(void)setIcon:(NSString *)icon
29 {
30 _icon=[icon copy];
31 self.imageView.image=[UIImage imageNamed:icon];
32 }
33
34 -(void)layoutSubviews
35 {
36 [super layoutSubviews];
37 self.imageView.frame=self.bounds;
38 }
39
40 @end
YYViewController.m檔案
1 //
2 // YYViewController.m
3 // 07-無限滾動(循環利用)
4 //
5 // Created by apple on 14-8-3.
6 // Copyright (c) 2014年 yangyong. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10 #import "YYimageCell.h"
11
12 #define YYCell @"cell"
13
14 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
15 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
16
17 @end
18
19 @implementation YYViewController
20
21 - (void)viewDidLoad
22 {
23 [super viewDidLoad];
24 //注冊cell
25 // static NSString *ID=@"cell";
26 [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
27
28 }
29
30 #pragma mark- UICollectionViewDataSource
31 //一共多少組,預設為1組
32 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
33 {
34 return 1;
35 }
36 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
37 {
38 return 16;
39 }
40
41 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
42 {
43 // static NSString *ID=@"cell";
44 YYimageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYCell forIndexPath:indexPath];
45 cell.backgroundColor=YYRandomColor;
46 NSLog(@"%p,%d",cell,indexPath.item);
47 cell.icon=[NSString stringWithFormat:@"minion_%02d",indexPath.item+1];
48 return cell;
49 }
50
51 #pragma mark-UICollectionViewDelegate
52 @end
界面實作:
(5)細節處理
設定分頁
調整間距
隐藏水準滾動條。
清除其顔色。