iOS development UI article----UI Basics of Beauty Carousel (Auto-Carousel) 2

Source: Internet
Author: User

////VIEWCONTROLLER.M//Picture Carousel-(automatic)////Created by RMS on 15/11/28.//copyright©2015 year RMS. All rights reserved.//#import "ViewController.h"#defineKimagecount 8@interfaceViewcontroller () <UIScrollViewDelegate>@property (nonatomic,strong) Uiscrollview*ScrollView, @property (nonatomic,strong) Uiimageview*Leftimageview, @property (nonatomic,strong) Uiimageview*Centerimageview, @property (nonatomic,strong) Uiimageview*Rightimageview, @property (nonatomic,strong) Uipagecontrol*Pagecontrol, @property (nonatomic,strong) Nstimer*timer;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; CGFloat width=Self.view.frame.size.width; CGFloat height=Self.view.frame.size.height; Uiscrollview*scrollview =[[Uiscrollview alloc]initwithframe:self.view.bounds];        [Self.view Addsubview:scrollview]; Uipagecontrol*pagecontrol = [[Uipagecontrol alloc]initwithframe:cgrectmake (0, Height- -, Width, -)]; Pagecontrol.numberofpages=Kimagecount; Pagecontrol.currentpageindicatortintcolor=[Uicolor Redcolor]; Pagecontrol.pageindicatortintcolor=[Uicolor Blackcolor];        [Self.view Addsubview:pagecontrol]; Self.pagecontrol=Pagecontrol; Scrollview.contentsize= Cgsizemake (Width * (kimagecount +2),0); Scrollview.contentoffset= Cgpointmake (width,0); Scrollview.pagingenabled=YES; Self.scrollview=ScrollView; ScrollView.Delegate=Self ; //31231[Self Imageviewwithframe:cgrectmake (0,0, width, height) imagename:[nsstring stringWithFormat:@"%02d", Kimagecount]]; [Self imageviewwithframe:cgrectmake (width* (Kimagecount +1),0, width, height) ImageName:@"01.jpg"];  for(inti =1; I < Kimagecount +1; i++) {[Self imageviewwithframe:cgrectmake (widthI0, width, height) imagename:[nsstring stringWithFormat:@"%02d", I]]; } [self Starttimer];}-(Uiimageview *) Imageviewwithframe: (CGRect) frame imageName: (NSString *) imagename{Uiimageview*imageview =[[Uiimageview alloc]initwithframe:frame]; Imageview.image=[UIImage Imagenamed:imagename]; Imageview.contentmode=Uiviewcontentmodescaleaspectfit; Imageview.clipstobounds=YES;    [Self.scrollview Addsubview:imageview]; returnImageView;}/turn on a new timer- (void) starttimer{//Timer//scheduled scheduling (execution)//Interval interval of time (s)//Target Destination (whose method is called)//Selector Method (which method to call)//repeats do I need to repeat//did two things. 1. Create an Nstimer object//2. This nstimer is added to the main run loop by default (the default priority is lower than the priority of event handling)//Self.timer = [Nstimer scheduledtimerwithtimeinterval:2 target:self selector: @selector (nextimage) Userinfo:nil Rep    Eats:yes]; //1. Create a Nstimer objectNstimer *timer = [Nstimer timerwithtimeinterval:2target:self selector: @selector (nextimage) Userinfo:nil Repeats:yes];//2. Adding to the main run cycle//Run Loop Loop//nsdefaultrunloopmode default mode (less than event-handling priority)//Nsrunloopcommonmodes General-purpose mode (same as priority for event handling)[[Nsrunloop Mainrunloop] Addtimer:timer formode:nsrunloopcommonmodes]; //3. Record the current timerSelf.timer =timer;}-(void) nextimage{Nsinteger page=Self.pageControl.currentPage; if(page = = Self.pageControl.numberOfPages-1) {page=0;//[UIView animatewithduration:1 animations:^{Self.scrollView.contentOffset = Cgpointmake (Self.scrollView.frame.size.width * (page+1),0);//        }];    }Else{page++; [UIView animatewithduration:1animations:^{Self.scrollView.contentOffset= Cgpointmake (Self.scrollView.frame.size.width * (page+1),0);            }]; }    }- (void) Scrollviewwillbegindragging: (Uiscrollview *) scrollview{//Stop Timer//Let the timer fail, once the failure is not in use. [Self.timer invalidate];}/** * Perform this method when the user's finger is lifted from the Scrollviwew*/- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{//start the dispatch again[self starttimer]; }-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{cgpoint offset=Scrollview.contentoffset; intpage = round (Offset.x/scrollView.frame.size.width); NSLog (@"%d", page); Self.pageControl.currentPage= Page-1; if(page = = Kimagecount +1) {Scrollview.contentoffset= Cgpointmake (ScrollView.frame.size.width,0); }    if(page = =0) {Scrollview.contentoffset= Cgpointmake (ScrollView.frame.size.width * Kimagecount,0); }}@end

iOS development UI article----UI Basics of Beauty Carousel (Auto-Carousel) 2

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.