iOS開發UI篇—無限輪播(迴圈展示)

來源:互聯網
上載者:User

標籤:

iOS開發UI篇—無限輪播(迴圈展示)

一、簡單說明

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

  

方法一:使用一個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 @end20 21 @implementation YYViewController22 23 #pragma mark-懶載入24 //-(NSArray *)news25 //{26 //    if (_news==nil) {27 //        _news=[YYnews objectArrayWithFilename:@"newses.plist"];28 //    }29 //    return _news;30 //}31 -(NSMutableArray *)news32 {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)viewDidLoad44 {45     [super viewDidLoad];46     //註冊cell47 //    [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- UICollectionViewDataSource56 //一共多少組,預設為1組57 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView58 {59     return 1;60 }61 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section62 {63     return self.news.count;64 }65 66 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath67 {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-UICollectionViewDelegate75 @end

  列印查看所處的索引(全程依然只建立了兩個cell):

  

說明:

  [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 @end20 21 @implementation YYViewController22 23 #pragma mark-懶載入24 -(NSArray *)news25 {26     if (_news==nil) {27         _news=[YYnews objectArrayWithFilename:@"newses.plist"];28     }29     return _news;30 }31 //-(NSMutableArray *)news32 //{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)viewDidLoad44 {45     [super viewDidLoad];46     //註冊cell47 //    [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- UICollectionViewDataSource58 //一共多少組,預設為1組59 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView60 {61     return 100;62 }63 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section64 {65     return self.news.count;66 }67 68 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath69 {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-UICollectionViewDelegate77 @end

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

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

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

iOS開發UI篇—無限輪播(迴圈展示)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.