IOS _ unlimited scrolling and iOS _ scrolling

Source: Internet
Author: User

IOS _ unlimited scrolling and iOS _ scrolling
Finally:
Using CollectionView, with pageContrl + timer idea, one group is used, but modelArrCount * 5000 items are told to the Controller, and when cellForRow is used, modelArrCount is created based on the index modulo, extract the data source (actually only eight items), and the width of the item is a code snippet of the width of the screen:

 
 
 
# Import "BeyondViewController. h "// Fast Frame # import" UIView + Frame. h "# import" BeyondNewsCell. h "# import" BeyondNews. h "# import" MJExtension. h "// only one group, but this group has 5000*8 rows # define TotalItems (5000 * self. newses. count) // didAppear appears in the middle and alignment on the left # define MiddleItem (NSUInteger) (TotalItems * 0.5) @ interface BeyondViewController () <UICollectionViewDataSource, UICollectionViewDelegate> @ property (weak, nonatomic) Define UICollectionView * collectionView; @ property (weak, nonatomic) IBOutlet UIPageControl * pageCtrl; @ property (strong, nonatomic) NSArray * newses; @ property (strong, nonatomic) NSTimer * timer; @ end @ implementation BeyondViewController # pragma mark-lazy loading-(NSArray *) newses {if (! _ Newses) {self. newses = [BeyondNews objectArrayWithFilename: @ "newses. plist "]; // the total number of pages self. pageCtrl. numberOfPages = self. newses. count;} return _ newses;} # pragma mark-lifecycle-(void) viewDidLoad {[super viewDidLoad]; // register cell [UICollectionViewCell does not allow code creation] [self. collectionView registerNib: [UINib nibWithNibName: @ "BeyondNewsCell" bundle: nil] forCellWithReuseIdentifier: @ "news"]; // Add a timer [self addTimer];}-(void) viewDidAppear :( BOOL) animated {[super viewDidAppear: animated]; [self. collectionView scrollToItemAtIndexPath: [NSIndexPath indexPathForItem: MiddleItem inSection: 0] atScrollPosition: UICollectionViewScrollPositionLeft animated: NO] ;#pragma mark-clock method-(void) addTimer {self. timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @ selector (schedule) userInfo: nil repeats: YES]; [[nsunloop currentRunLoop] addTimer: self. timer forMode: nsunloopcommonmodes];}-(void) removeTimer {[self. timer invalidate]; self. timer = nil;} // timer method-(void) schedule {// get the currently displayed item NSIndexPath * visiablePath = [self. collectionView indexPathsForVisibleItems] firstObject]; NSUInteger visiableItem = visiablePath. item; if (visiablePath. item % self. newses. count) = 0) {// 0th images [self. collectionView detail: [NSIndexPath indexPathForItem: MiddleItem inSection: 0] atScrollPosition: incluanimated: NO]; visiableItem = MiddleItem;} // scroll to the next item NSUInteger nextItem = visiableItem + 1; [self. collectionView scrollToItemAtIndexPath: [NSIndexPath indexPathForItem: nextItem inSection: 0] atScrollPosition: Required animated: YES];} # pragma mark-data source and proxy method-(NSInteger) Detail :( UICollectionView *) collectionView {return 1;}-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section {return TotalItems;}-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {static NSString * cellID = @ "news"; // directly retrieve BeyondNewsCell * cell = [collectionView detail: cellID forIndexPath: indexPath]; // UICollectionViewCell is not created based on the id. You can only specify the cell height from xib to load // UICollectionViewFlowLayout * layout. news = self. newses [indexPath. item % self. newses. count]; return cell;} // important: Listen to the events that the proxy stops rolling-(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 proxy // start dragging-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {[self removeTimer];} // stop dragging-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {[self addTimer];} @ end


Categories used:
//// Frame. h // 08-infinite scrolling // 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-infinite scrolling // 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: Specifies the size of the item in FlowLayout.
Xib creates a CollectionCell. Pay attention to specifying the reuse ID.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.