UI-based Uiscrollview for manual rotation of picture groups and automatic carousel

Source: Internet
Author: User

#import "RootViewController.h"@interfaceRootviewcontroller () {Uipagecontrol*_pagecontrol; Nstimer*_timer; Uiscrollview*SCRV; UILabel*label;}@end@implementationRootviewcontroller- (ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{ Self=[Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if(self) {//Custom Initialization    }
returnSelf ;}- (void) viewdidload{[Super Viewdidload]; [Self creatscrollview]; [Self creatpagecontroller]; [Self addtimer]; //Do any additional setup after loading the view.}
//Join timer, perform a page-break operation in the timer method for auto- Carousel-(void) addtimer{_timer=[nstimer Scheduledtimerwithtimeinterval:1target:self selector: @selector (tonextpage) Userinfo:nil repeats:yes];}-(void) tonextpage{Nsinteger page=_pagecontrol.currentpage; //Here the use of the judging method, are based on the interface of the conversion, the last side of the first image added to the first picture with the top off, resulting in visual error, rescue . if(page==0) {Scrv.contentoffset=cgpointmake (0,0); } page++; CGFloat X=page*Self.view.frame.size.width; [UIView animatewithduration:1animations:^{Scrv.contentoffset=cgpointmake (X,0); }]; if(page==8) {page=0; } _pagecontrol.currentpage=page; Label.text=[nsstring stringWithFormat:@"%D/8", page+1];}
//Define page controls so that page controls follow the picture transformation -(void) creatpagecontroller{_pagecontrol=[[uipagecontrol Alloc]initwithframe:cgrectmake (0, -, the, +)]; _pagecontrol.backgroundcolor=[Uicolor Blackcolor];
//page control transparency _pagecontrol.alpha=0.5;
//define the page control to display the number of white spots, the actual picture is 9, the last one is consistent with the first, because only the implementation from left to right, just add one at the end. If you need to go from right to left, you can add one for the last one before the first one, and change the initial offset to achieve multiple directional conversions _pagecontrol.numberofpages=8;
Although the actual picture is 9 or 10, but the real image of the exhibition only 8, so page page is still 8, the first exhibition begins must be 0; _pagecontrol.currentpage=0; [Self.view Addsubview:_pagecontrol]; //Define page numbers, do not write irrelevant label here =[[uilabel Alloc]initwithframe:cgrectmake ( the,460, -, -)]; Label.text=@"1/8"; Label.backgroundcolor=[Uicolor Cyancolor]; Label.textalignment=Nstextalignmentcenter; [Self.view Addsubview:label];}
//1?? Initialize ScrollView, load pictures, I use anime 00.jpg--anime 08.jpg, so you can loop create -(void) creatscrollview{ //definition and screen size, etc. high-width scrollview scrv=[[Uiscrollview alloc]initwithframe:self.view.bounds]; //Load Picture for(inti =0; I <9; i++) {Uiimageview* Imagev=[[uiimageview Alloc]initwithframe:cgrectmake (self.view.frame.size.width*i,0, the, Self.view.frame.size.height)]; [Imagev setimage:[uiimage imagenamed:[nsstring stringWithFormat:@"Anime%02d.jpg", i+1]]]; [SCRV Addsubview:imagev]; } //Define the properties of the ScrollView scrv.contentsize=cgsizemake (self.view.frame.size.width*9,0);
//Define the initial offset if the first sheet is preceded by the last one, change the initial to 320,0; Scrv.contentoffset=cgpointmake (0,0); //Press page to jump
Scrv.pagingenabled=YES;
Scrv.showsverticalscrollindicator=NO;
//Set proxy scrv.Delegate=Self ; [Self.view ADDSUBVIEW:SCRV]; }#pragmaMark **uiscrollviewdelegate**//At the end of the deceleration method, determine the page position, so that the page control follow the picture transformation here for manual rotation. -(void) Scrollviewdidenddecelerating: (Uiscrollview *) scrollview{NSLog (@"deceleration"); Nsinteger page=scrv.contentoffset.x/ the; if(page==8) {Scrollview.contentoffset=cgpointmake (0,0); Page=0; } _pagecontrol.currentpage=page;}- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

Uiscrollview scroll bar control is mostly applied to the picture exhibition in mobile software

UI-based Uiscrollview for manual rotation of picture groups and automatic carousel

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.