First of all, I say the idea, automatic scrolling implementation is realized by the timer. Of course, given that we may have to do manual sliding during the timing cycle, we are going to
The current point of the Pagecontrol is judged.
Circular scrolling is done by adding 2 to the image on the slide.
If you have 5 elements in your slideshow that need to be looped:
[0, 1, 2, 3, 4]
So when you add these four elements to the Uiscrollview, you need to add two more, and that's it:
[4, 0, 1, 2, 3, 4, 0]
Then only in the Scrollviewdidenddecelerating proxy method to judge that offset is the first and last
int offset = sv.contentoffset.x/320;
When offset = = 0 or equal to the last one, you setcontentoffset to the corresponding element
For example, when Offset==0,setcontentoffset to a fourth element, remember that animated is set to No.
All right, here's the idea, here's the code.
Add the displayed picture to the Scrview and add a click gesture to the image
- for(inti =0; i < Arrbanner.count; i++) { //-------------------Add a picture to ScrviewUiimageview *imgview = [[Uiimageview alloc] Initwithframe:cgrectmake ( the* (i +1),0, the, -)]; Nsdictionary*dicbanner =[Arrbanner objectatindex:i]; Self.model=[[Basejsonmodel alloc] initwithdatadic:dicbanner]; NSString*strbanner = [NSString stringWithFormat:@"http://app2.jzh.cn/%@", _model. Banner_pic]; Nsurl*bannerurl =[Nsurl Urlwithstring:strbanner];//UIImage *imgbanner = [UIImage imagewithdata:[nsdata datawithcontentsofurl:bannerurl];[Imgview Setimagewithurl:bannerurl Placeholderimage:nil]; [Self.scrview Addsubview:imgview]; //--------------------Add gestures to a pictureimgview.userinteractionenabled =YES; Imgview.tag= -+i; UITapGestureRecognizer*tapgesture =[[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (dissertation:)]; [Imgview Addgesturerecognizer:tapgesture]; [Tapgesture release]; }
Add reserved ImageView in Scrview
1 //take the last picture of the array on page No. 02Nsdictionary *dicbanlast = [Arrbanner objectatindex:arrbanner.count-1];3NSString *lastimg = [Dicbanlast objectforkey:@"Baner_pic"];4NSString *bannerlast = [NSString stringWithFormat:@"http://app2.jzh.cn/%@", lastimg];5Nsurl *bannerlasturl =[Nsurl Urlwithstring:bannerlast];6Uiimageview *imageview =[[Uiimageview alloc] init];7 [ImageView Setimagewithurl:bannerlasturl placeholderimage:nil];8NSLog (@"%@", lastimg);9Imageview.frame = CGRectMake (0,0, the, -);//Add the last 1 pages in the home loopTen [Self.scrview Addsubview:imageview]; One //take the first image of the array on the last 1 pages ANsdictionary *dicbanfirst = [Arrbanner objectatindex:0]; -NSString *firstimg = [Dicbanfirst objectforkey:@"Baner_pic"]; -NSString *bannerfirst = [NSString stringWithFormat:@"http://app2.jzh.cn/%@", firstimg]; theNsurl *bannerfirsturl =[Nsurl Urlwithstring:bannerfirst]; -ImageView =[[Uiimageview alloc] init]; - [ImageView Setimagewithurl:bannerfirsturl placeholderimage:nil]; -Imageview.frame = CGRectMake (( the* (Arrbanner.count +1)) ,0, the, -);//Add page 1th in the last loop + [Self.scrview Addsubview:imageview]; - +[Self.scrview Setcontentsize:cgsizemake ( the* (Arrbanner.count +2), -)];//+ on page 1th and 4th principle: 4-[1-2-3-4]-1 A[Self.scrview Setcontentoffset:cgpointmake (0,0)]; at[Self.scrview Scrollrecttovisible:cgrectmake ( the,0, the, -) Animated:no];//default from ordinal 1-bit Placement 1th page, ordinal 0 position position on page 4th
Uipagecontrol
Self.pageControl.numberOfPages = arrbanner.count; 0 ; [Self.pagecontrol addtarget:self Action: @selector (Turnpage) forcontrolevents:uicontroleventvaluechanged];
1 -(void) turnpage2{3 int page = Self.pageControl.currentPage; 4 [Self.scrview scrollrecttovisible:cgrectmake (page+1),0, - ) Animated:no]; 5 }
Timer Add loop Event
1 // timer loop 2 [nstimer scheduledtimerwithtimeinterval:3 target:self selector: @selector (runtimepage) Userinfo:nil Repeats:yes];
1 -(void ) runtimepage 2 { 3 int page = Self.pageControl.currentPage; // Gets the current page 4 page++; 5 page = page > Arrbanner.count-1 ? 0 : page; 6 self.pageControl.currentPage = page; 7 [self turnpage]; 8 }
Implementation of Scrview Agent
1 #pragmaMark-scrollviewdelegate2- (void) Scrollviewdidscroll: (Uiscrollview *) ScrollView3 {4CGFloat PageWidth =Self.scrView.frame.size.width;5 intpage = Floor ((self.scrview.contentoffset.x-pagewidth/(arrbanner.count+2))/pagewidth) +1;6Page--;//default starts from second page7Self.pageControl.currentPage =page;8 }9 //deceleration StopTen- (void) Scrollviewdidenddecelerating: (Uiscrollview *) ScrollView One { ACGFloat PageWidth =Self.scrView.frame.size.width; - - intCurrentPage = Floor ((self.scrview.contentoffset.x-pagewidth/(arrbanner.count+2))/PageWidth) +1; the //int currentpage_ = (int) self.scrollview.contentoffset.x/320;//same as the above two lines - //NSLog (@ "currentpage_==%d", Currentpage_); - if(currentpage==0) - { +[Self.scrview Scrollrecttovisible:cgrectmake ( the* Arrbanner.count,0, the, -) Animated:no];//serial 0 last 1 page - } + Else if(currentpage== (arrbanner.count+1)) A { at[Self.scrview Scrollrecttovisible:cgrectmake ( the,0, the, -) Animated:no];//the last +1, the 1th page of the cycle - } - -}
In addition to Imgview specific click events not added, the other code should be so, because this is the effect of the previous writing, only recently remembered to write a blog, there may be some omissions, welcomed the proposed, to communicate
Auto-cycle scrolling for IOS slides