iOS_無限滾動,iOS_滾動

來源:互聯網
上載者:User

iOS_無限滾動,iOS_滾動
最終:
使用CollectionView實現,帶pageContrl + timer定時器思路,使用1組,但是告訴控制器有modelArrCount*5000個item,並且cellForRow時,建立根據index模數modelArrCount,取出資料來源(實際只有8個),並且item的寬度就是一個螢幕的寬度程式碼片段:




#import "BeyondViewController.h"// 快速Frame#import "UIView+Frame.h"#import "BeyondNewsCell.h"#import "BeyondNews.h"#import "MJExtension.h"// 只有一組,但是該組 有5000*8行#define TotalItems (5000 * self.newses.count)// 第一次出現的時候,didAppear時,就出現在中間,左邊對齊#define MiddleItem (NSUInteger)(TotalItems * 0.5)@interface BeyondViewController () <UICollectionViewDataSource, UICollectionViewDelegate>@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;@property (weak, nonatomic) IBOutlet UIPageControl *pageCtrl;@property (strong, nonatomic) NSArray *newses;@property (strong, nonatomic) NSTimer *timer;@end@implementation BeyondViewController#pragma mark - 懶載入- (NSArray *)newses{    if (!_newses) {        self.newses = [BeyondNews objectArrayWithFilename:@"newses.plist"];        // 總頁數        self.pageCtrl.numberOfPages = self.newses.count;    }    return _newses;}#pragma mark - 生命週期- (void)viewDidLoad{    [super viewDidLoad];        // 註冊cell【UICollectionViewCell不讓代碼建立】    [self.collectionView registerNib:[UINib nibWithNibName:@"BeyondNewsCell" bundle:nil] forCellWithReuseIdentifier:@"news"];        // 添加定時器    [self addTimer];    }- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:MiddleItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];}#pragma mark - 時鐘方法- (void)addTimer{    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(schedule) userInfo:nil repeats:YES];    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];}- (void)removeTimer{    [self.timer invalidate];    self.timer = nil;}// 定時器方法- (void)schedule{    // 得到當前顯示的item    NSIndexPath *visiablePath = [[self.collectionView indexPathsForVisibleItems] firstObject];        NSUInteger visiableItem = visiablePath.item;    if ((visiablePath.item % self.newses.count)  == 0) { // 第0張圖片        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:MiddleItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];        visiableItem = MiddleItem;    }        // 滾動到下一個item    NSUInteger nextItem = visiableItem + 1;    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];}#pragma mark - 資料來源和代理方法- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{    return 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return TotalItems;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellID = @"news";    // 直接取    BeyondNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];    // UICollectionViewCell沒有根據id進行建立方法,只能 從xib載入    //    UICollectionViewFlowLayout *layout 中 可以指定cell的高度        cell.news = self.newses[indexPath.item % self.newses.count];        return cell;}// 重要,監聽代理 停止滾動的事件- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{    NSIndexPath *visiablePath = [[collectionView indexPathsForVisibleItems] firstObject];    self.pageCtrl.currentPage = visiablePath.item % self.newses.count;}#pragma mark - scrollView代理// 開始拖拽- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{    [self removeTimer];}// 停止拖拽- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{    [self addTimer];}@end


使用到的分類:
////  Frame.h//  08-無限滾動////  Created by beyond on 15-3-27.//  Copyright (c) 2015年 itcast. All rights reserved.//#import <UIKit/UIKit.h>@interface UIView (Frame)@property (assign, nonatomic) CGFloat x;@property (assign, nonatomic) CGFloat y;@property (assign, nonatomic) CGFloat width;@property (assign, nonatomic) CGFloat height;@property (assign, nonatomic) CGSize size;@property (assign, nonatomic) CGPoint origin;@end


////  Frame.m//  08-無限滾動////  Created by beyond on 15-3-27.//  Copyright (c) 2015年 itcast. All rights reserved.//#import "UIView+Frame.h"@implementation UIView (Frame)- (void)setX:(CGFloat)x{    CGRect frame = self.frame;    frame.origin.x = x;    self.frame = frame;}- (CGFloat)x{    return self.frame.origin.x;}- (void)setY:(CGFloat)y{    CGRect frame = self.frame;    frame.origin.y = y;    self.frame = frame;}- (CGFloat)y{    return self.frame.origin.y;}- (void)setWidth:(CGFloat)width{    CGRect frame = self.frame;    frame.size.width = width;    self.frame = frame;}- (CGFloat)width{    return self.frame.size.width;}- (void)setHeight:(CGFloat)height{    CGRect frame = self.frame;    frame.size.height = height;    self.frame = frame;}- (CGFloat)height{    return self.frame.size.height;}- (void)setSize:(CGSize)size{    CGRect frame = self.frame;    frame.size = size;    self.frame = frame;}- (CGSize)size{    return self.frame.size;}- (void)setOrigin:(CGPoint)origin{    CGRect frame = self.frame;    frame.origin = origin;    self.frame = frame;}- (CGPoint)origin{    return self.frame.origin;}@end


MainStoryBoard,在FlowLayout中指定item的size
Xib建立CollectionCell,注意指定重用ID

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.