In the project to put banner picture Uiimageview a one-piece into the Uiscrollview, by setting the pagingenabled property of Uiscrollview Yes, It can be done when the user sliding banner the picture of a single display, but when the user wants to from the first picture to the last picture, or from the last picture to the first picture of the time will be stuck, the simple point is to judge the direction of user swipe, Then let Uiscrollview automatically slide to the last one or the first one, this is not natural enough in the animation, the solution
Suppose there are 3 pictures of banner, arranged in the following way in the Uiscrollview of banner
The third one |
First one |
The second one |
The third one |
First one |
Uiscrollview default Scroll to the position of the second picture, when the user slides to the first sheet, then Uiscrollview automatically scroll to the 4th, the same as the user should slide to the last one, Uiscrollview automatically scroll to the second, these scrolling are set to animate the effect of No
The code is as follows
H file
#import <UIKit/UIKit.h>@interface**Pagecontrol; @end
M file
#import "ViewController.h"@interfaceViewcontroller () <UIScrollViewDelegate>{cgfloat w; Nsmutablearray*Imgarr; Nsmutablearray*Pagearr;}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; W=Self.view.frame.size.width; //Number of pagePagearr =[Nsmutablearray array]; for(inti =1; I <=3; i++) {[Pagearr addobject:[nsstring stringWithFormat:@"%d.jpg", I]]; } //re-get the number of Imgview, put the last picture shown in the first, and the last one placed in the first picture shownImgarr =[Nsmutablearray array]; [Imgarr Addobject:pagearr[pagearr.count-1]]; for(NSString *imgsrcinchPagearr) {[Imgarr addobject:imgsrc]; } [Imgarr addobject:pagearr[0]]; Self.mainScroll.contentSize= Cgsizemake (W*imgarr.count,0); //start to plug in the picture for(inti =0; i < Imgarr.count; i++) {NSString*IMGSRC =Imgarr[i]; Uiimageview*imgview = [[Uiimageview alloc] Initwithframe:cgrectmake (i*w,0, W, Self.mainScroll.frame.size.height)]; [Imgview setimage:[uiimage IMAGENAMED:IMGSRC]; [Self.mainscroll Addsubview:imgview]; } self.pageControl.numberOfPages=Pagearr.count; Self.mainScroll.bounces=NO; Self.mainScroll.pagingEnabled=YES; Self.mainScroll.showsHorizontalScrollIndicator=NO; Self.mainScroll.showsVerticalScrollIndicator=NO; Self.mainscroll.Delegate=Self ; //automatically move ScrollView to the first picture displayed[Self.mainscroll Setcontentoffset:cgpointmake (W,0) Animated:no];}- (void) Scrollviewdidenddecelerating: (Uiscrollview *) ScrollView {cgfloat OffsetX=Scrollview.contentoffset.x; intindex = offsetx/W; if(Index = =0) {[Self.mainscroll setcontentoffset:cgpointmake (w* (imgarr.count-2),0) Animated:no]; Self.pageControl.currentPage= Pagearr.count-1; }Else if(Index = = Imgarr.count-1) {[Self.mainscroll setcontentoffset:cgpointmake (W,0) Animated:no]; Self.pageControl.currentPage=0; }Else{self.pageControl.currentPage= Index-1; } }@end
IOS Implementation Banner Cyclic carousel