天天看點

iOS開發UI篇—無限輪播(循環展示)

iOS開發UI篇—無限輪播(循環展示)

一、簡單說明

  之前的程式還存在一個問題,那就是不能循環展示,因為plist檔案中隻有五個數組,是以第一個和最後一個之後就沒有了,下面介紹處理這種循環展示問題的小技巧。

  

iOS開發UI篇—無限輪播(循環展示)

方法一:使用一個for循環,循環200次,建立200*=1000個模型,且預設程式啟動後處在第100組的位置,向前有500個模型,向後也有500個模型,産生一種循環展示的假象。

  代碼如下:

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 "MJExtension.h"
11 #import "YYnews.h"
12 #import "YYcell.h"
13 
14 #define YYIDCell @"cell"
15 
16 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
17 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
18 @property(nonatomic,strong)NSMutableArray *news;
19 @end
20 
21 @implementation YYViewController
22 
23 #pragma mark-懶加載
24 //-(NSArray *)news
25 //{
26 //    if (_news==nil) {
27 //        _news=[YYnews objectArrayWithFilename:@"newses.plist"];
28 //    }
29 //    return _news;
30 //}
31 -(NSMutableArray *)news
32 {
33     if (_news==nil) {
34         _news=[NSMutableArray array];
35         for (int i=0; i<200; i++) {
36             NSArray *array=[YYnews objectArrayWithFilename:@"newses.plist"];
37             [_news addObjectsFromArray:array];
38         }
39     }
40     return _news;
41 }
42 
43 - (void)viewDidLoad
44 {
45     [super viewDidLoad];
46     //注冊cell
47 //    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
48     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
49     
50     //預設處于第0組的第500個模型的左邊
51     [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
52     
53 }
54 
55 #pragma mark- UICollectionViewDataSource
56 //一共多少組,預設為1組
57 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
58 {
59     return 1;
60 }
61 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
62 {
63     return self.news.count;
64 }
65 
66 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
67 {
68     YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
69     cell.news=self.news[indexPath.item];
70     NSLog(@"%p,%d",cell,indexPath.item);
71     return cell;
72 }
73 
74 #pragma mark-UICollectionViewDelegate
75 @end      

  列印檢視所處的索引(全程依然隻建立了兩個cell):

iOS開發UI篇—無限輪播(循環展示)

說明:

  [self.collectinView scrollToItemAtIndexPath:<#(NSIndexPath *)#> atScrollPosition:<#(UICollectionViewScrollPosition)#> animated:<#(BOOL)#>]

 //預設處于第0組的第500個模型的左邊

方法二:設定其有100組,那麼一共有100*5=500個模型。且設定預設處于第50組的索引為0處。

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 "MJExtension.h"
11 #import "YYnews.h"
12 #import "YYcell.h"
13 
14 #define YYIDCell @"cell"
15 
16 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
17 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
18 @property(nonatomic,strong)NSArray *news;
19 @end
20 
21 @implementation YYViewController
22 
23 #pragma mark-懶加載
24 -(NSArray *)news
25 {
26     if (_news==nil) {
27         _news=[YYnews objectArrayWithFilename:@"newses.plist"];
28     }
29     return _news;
30 }
31 //-(NSMutableArray *)news
32 //{
33 //    if (_news==nil) {
34 //        _news=[NSMutableArray array];
35 //        for (int i=0; i<200; i++) {
36 //            NSArray *array=[YYnews objectArrayWithFilename:@"newses.plist"];
37 //            [_news addObjectsFromArray:array];
38 //        }
39 //    }
40 //    return _news;
41 //}
42 
43 - (void)viewDidLoad
44 {
45     [super viewDidLoad];
46     //注冊cell
47 //    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
48     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
49     
50     //預設處于第0組的第500個模型的左邊
51 //    [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
52     
53      [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
54     
55 }
56 
57 #pragma mark- UICollectionViewDataSource
58 //一共多少組,預設為1組
59 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
60 {
61     return 100;
62 }
63 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
64 {
65     return self.news.count;
66 }
67 
68 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
69 {
70     YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
71     cell.news=self.news[indexPath.item];
72     NSLog(@"%p,%d",cell,indexPath.item);
73     return cell;
74 }
75 
76 #pragma mark-UICollectionViewDelegate
77 @end      

注意:上面的兩種方法都建立了大量的無用的模型,不太可取。且在實際開發中,建議模型的總數不要太大,因為在其内部需要周遊計算所有控件的frame。

  如果模型數量太大,會占用資源。

改進建議:可以監聽手指在上面的滾動,當停止滾動的時候,又重新設定初始的中間位置。