Uiscrollview three ways to realize infinite scrolling.
Of course, just some of my usage, of course, there are a lot of ways to achieve, if you have a good idea of implementation can also tell me, learn from each other ~
Uiscrollview Infinite Scrolling
The first way:
principle: Use the end position to reset the value of the ScrollView contentoffset , let the person produce the visual infinite loop, the advantage: The code is understandable, the disadvantage, will create the extra memory.
The second way:
principle: Use the two variables in the middle to the current view and the buffered view, create only two view, and place the current view in the middle. Determine the position of the slide, prioritize the view to the buffer, and then end the swipe to update the image of the current view
the Third Way (which is not explained here):
principle: Using CollectionView to implement, the code is very simple. Once the data source is passed in Images.count *, and then the default load, jump to (Images.count *)/2.0 + ((Images.count *)/2.0)% images the location of the . Count is implemented
GitHub code Address:https://github.com/MakeZL/UIScrollViewLoopDemo Click to open the link
All right. Let's first look at the first implementation of the relatively simple
<span style= "FONT-SIZE:14PX;" >/** * The first way: Use the end position to reset the value of the ScrollView Contentoffset, giving the person a visual infinite loop advantage: The code is easy to understand, and creates extra memory. */-(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 creation child control [Self.slideimages enumerateobjectsusingblock:^ (NSString *imagename, Nsuinteger idx, BOOL *stop) {UII Mageview *imageview = [[Uiimageview alloc] init]; Imageview.image = [UIImage imagenamed:imagename]; Imageview.frame = CGRectMake ((idx+1) * scrollviewsize.width, 0, Scrollviewsize.width, Scrollviewsize.height); [ScrollView Addsubview:imageview]; }]; Place the last picture in 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]; Put the first picture in the last position, resulting in a visual loop 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)];} </span>
and then uiscrollview the delegate inside to achieve
<span style= "FONT-SIZE:14PX;" >-(void) scrollviewdidenddecelerating: (Uiscrollview *) scrollview{ Nsinteger page = Scrollview.contentoffset.x/scrollview.frame.size.width; If the current page is page No. 0, jump to the last place in 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 jumps to the location of the first element of the array [ScrollView setcontentoffset: Cgpointmake (scrollView.frame.size.width, 0)];} } </span>
is not very simple. Let's look at the second kind
The second way, with the cache:
<span style= "FONT-SIZE:14PX;" >/** * Second way: Use the middle of the two variables to the current view and buffered view, create up to three view, the current view in the middle. Determine the position of the slide, priority to buffer the view to find advantages: less memory consumption, disadvantage: The code is more complex than a silk */-(void) realizescrollloop2{status = Scrollviewloopstatusresu ing 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; }</span>
Implement Uiscrollviewdelegate
<span style= "FONT-SIZE:14PX;" >-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{if (status = = Scrollviewloopstatusresuing) {if (SCR Ollview.contentoffset.x > _currentview.frame.origin.x) {nsinteger val = self.index + 1; if (Self.index >= [self.slideimages Count]-1) {val = 0; }//Take buffer of view self.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; }}}</span>
at the end of the time, the current CurrentView image is assigned the latest value.
<span style= "FONT-SIZE:14PX;" >-(void) scrollviewdidenddecelerating: (Uiscrollview *) scrollview{ //Whether it is sliding 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.slid Eimages count]-1) { self.index = 0; } _currentview.image = [UIImage imagenamed:[nsstring stringwithformat:@ "0%zd.jpg", Self.index]];} </span>
This will enable an infinite loop.
Ios_uiscrollview implements infinite scrolling, ideas and code