IOS_UIScrollView for unlimited scrolling, ideas and code

Source: Internet
Author: User

IOS_UIScrollView for unlimited scrolling, ideas and code

There are three ways to implement unlimited scrolling in UIScrollView.

Of course it's just some of my usage. Of course there are many implementation methods. If you have a good idea, you can tell me how to learn from each other ~

UIScrollView unlimited scrolling

Method 1:

Principle: Use the end position to reset the ScrollView ContentOffset value to generate an infinite loop visually. Advantages: easy to understand the code, disadvantages, and excessive memory will be created.

Method 2:

Principle: Use two variables in the middle to View and buffer the current View. Only two views are created and the current View is placed in the middle. Determine the Slide position, give priority to the buffered View, and then end the slide to update the image of the current View.

Method 3 (not described here ):

Principle: CollectionView is used for implementation, and the code is also very simple. Input images to the data source at one time. count * 1000, and then jump to (images. count * 1000)/2.0 + (images. count * 1000)/2.0) % images. the count position is implemented.


Github Code address: https://github.com/makezl/uiscrollviewloopdemoclick to open the link


Okay. First, let's look at the first method, which is relatively simple.

/*** Method 1: Use the end position to reset the value of ScrollView ContentOffset to generate an infinite loop visually. Advantages: the code is easy to understand, and redundant memory is created. */-(Void) realizeScrollLoop1 {UIScrollView * scrollView = [[UIScrollView alloc] init]; scrollView. showsHorizontalScrollIndicator = NO; scrollView. showsVerticalScrollIndicator = NO; scrollView. pagingEnabled = YES; scrollView. frame = self. view. bounds; scrollView. delegate = self; [self. view addSubview: scrollView]; self. scrollView = scrollView; [scrollView setContentSize: CGSizeMake ([self. slideImages count] + 2) * scrollView. frame. size. width, 0)]; CGSize scrollViewSize = scrollView. frame. size; // traverse and create a subcontrol [self. slideImages enumerateObjectsUsingBlock: ^ (NSString * imageName, NSUInteger idx, BOOL * stop) {UIImageView * imageView = [[UIImageView alloc] init]; imageView. image = [UIImage imageNamed: imageName]; imageView. frame = CGRectMake (idx + 1) * scrollViewSize. width, 0, scrollViewSize. width, scrollViewSize. height); [scrollView addSubview: imageView] ;}]; // obtain the last image to the first position: UIImageView * imageView = [[UIImageView alloc] init]; imageView. image = [UIImage imageNamed: self. slideImages [[self. slideImages count]-1]; imageView. frame = CGRectMake (0, 0, scrollViewSize. width, scrollViewSize. height); [scrollView addSubview: imageView]; // place the first image to the last position, resulting in a visually circulating UIImageView * lastImageView = [[UIImageView alloc] init]; lastImageView. image = [UIImage imageNamed: self. slideImages [0]; lastImageView. frame = CGRectMake (scrollViewSize. width * ([self. slideImages count] + 1), 0, scrollViewSize. width, scrollViewSize. height); [scrollView addSubview: lastImageView]; [scrollView setContentOffset: CGPointMake (scrollViewSize. width, 0)];}


Then implement it in the delegate of UIScrollView.

-(Void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {NSInteger page = scrollView. contentOffset. x/scrollView. frame. size. width; // if the current page is 0th, jump to the last part of the array to jump if (page = 0) {[scrollView setContentOffset: CGPointMake (scrollView. frame. size. width * ([[self slideImages] count]), 0)];} else if (page = [[self slideImages] count] + 1) {// if the last page is displayed, jump to the location of the first element of the array [scrollView setContentOffset: CGPointMake (scrollView. frame. size. width, 0)] ;}}



Is it easy. Next let's look at the second


Second, with cache:

/*** Method 2: Use two variables in the middle to View the current View and buffer the View. A maximum of three views can be created and the current View can be placed in the middle. Determine the sliding position and prioritize the buffer View finding. Advantage: less memory consumption, disadvantage: code is a bit more complicated */-(void) realizeScrollLoop2 {status = ScrollViewLoopStatusResuing; UIScrollView * scrollView = [[UIScrollView alloc] init]; scrollView. pagingEnabled = YES; scrollView. frame = self. view. bounds; scrollView. delegate = self; [self. view addSubview: scrollView]; self. scrollView = scrollView; CGSize scrollViewSize = scrollView. frame. size; scrollView. contentSize = CGSizeMake (3 * scrollViewSize. width, 0); scrollView. contentOffset = CGPointMake (scrollViewSize. width, 0); UIImageView * currentView = [[UIImageView alloc] init]; currentView. tag = 0; currentView. frame = CGRectMake (scrollViewSize. width, 0, scrollViewSize. width, scrollViewSize. height); currentView. image = [UIImage imageNamed: @ "00.jpg"]; [scrollView addSubview: currentView]; self. currentView = currentView; [self resuingView]; self. index = 0 ;}

Implement UIScrollViewDelegate

-(Void) scrollViewDidScroll :( UIScrollView *) scrollView {if (status = ScrollViewLoopStatusResuing) {if (scrollView. contentOffset. x> _ currentView. frame. origin. x) {NSInteger val = self. index + 1; if (self. index> = [self. slideImages count]-1) {val = 0;} // obtain the View self of the buffer. resuingView. image = [UIImage imageNamed: [NSString stringWithFormat: @ "0%zd.jpg", val]; self. resuingView. x = CGRectGetMinX (_ currentView. frame) + _ currentView. width; self. isLastScrollDirection = YES;} else {NSInteger val = self. index-1; if (val <0) {val = [self. slideImages count]-1;} self. resuingView. image = [UIImage imageNamed: [NSString stringWithFormat: @ "0%zd.jpg", val]; self. resuingView. x = 0; self. isLastScrollDirection = NO ;}}}


At the end, assign the current currentView image to the latest value.

-(Void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {// whether to slide to the right if (self. isLastScrollDirection) {self. index ++;} else {self. index --;} // complete if (self. index <0) {self. index = [self. slideImages count]-1;} else if (self. index> [self. slideImages count]-1) {self. index = 0;} _ currentView. image = [UIImage imageNamed: [NSString stringWithFormat: @ "0%zd.jpg", self. index];}


In this way, an infinite loop can be achieved.


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.