iOS Basics (23)--automatic switch between timer nstimer and pictures

Source: Internet
Author: User

First, Nstimer

Nstimer is an object that can execute the method we specify at a certain point in time from now to the back or periodically. You can send the information you make to the target object at a certain time interval. and update the behavior of an object. You can choose to stop, open, and even destroy it sometime in the future.

1, the creation of Nstimer

+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;

+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(ID) UserInfo repeats: (BOOL) Yesorno;

Note that the two methods are different;

The first method requires a manual addtimer:formode: Add a timer to a runloop:

Such as:

Nstimer *tImer = [nstimer timerwithtimeinterval: 1. 0 target:selfselector:@ Selector(timeraction:) userInfo:nil repeats: YES];

[[nsrunloop currentrunloop] addtimer: timer formode:nsdefaultrunloopmode];

The second scheduled method will be added directly to the current runloop with the default mode:

Such as:

Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (timeraction:) UserInfo: Nil Repeats:yes];

Several parameters:

TimerInterval : Time to wait before executing

target : The object that needs to execute the method

Selector : The method to be executed

repeats : whether to cycle

2. Release method

[Timer invalidate];

If the timer is no longer used, it is best to set nil, and if you continue to use it, you do not need to set

Timer = nil;

3. Pause/Restart of timer

The method of closing the timer above is permanent and cannot be stopped using the above stop method when the timer is paused.

Pause timer (temporarily off)

[Timer setfiredate: [nsdate distantfuture];

Turn on timer

[Timer setfiredate: [nsdate distantpast];

Second, Uiscrollview, Uipagecontrol, nstimer to achieve automatic image switching

Demo

1 @interfaceViewcontroller () <UIScrollViewDelegate>{2 3Uipagecontrol *PageCtrl;4Uiscrollview *ScrollView;5Nsarray *Imagearray;6     7Nstimer *timer;8     intLastPage;9     intcurrentpage;Ten  One } A  - @end -  the @implementationViewcontroller -  -- (void) Viewdidload { - [Super Viewdidload]; +     //additional setup after loading the view, typically from a nib. -     floatwidth =Self.view.frame.size.width; +      A     //Create Uiscrollview atScrollView = [[Uiscrollview alloc] initWithFrame: (cgrect) {0, -, Width, the}]; -ScrollView.Delegate=Self ; -scrollview.pagingenabled =YES; -Scrollview.contentsize = Cgsizemake (width*5, the); -Scrollview.showshorizontalscrollindicator =NO; -Scrollview.showsverticalscrollindicator =NO; in [Self.view Addsubview:scrollview]; -      to     //Turn on Timer method + [self timeon]; -      the     //Create Uipagecontrol *PageCtrl = [[Uipagecontrol alloc] initWithFrame: (cgrect) {0, the, Width, -}]; $ [Self.view Addsubview:pagectrl];Panax NotoginsengPagectrl.numberofpages =5; -Pagectrl.currentpageindicatortintcolor =[Uicolor Graycolor]; thePagectrl.pageindicatortintcolor = [Uicolor colorwithwhite:0.8Alpha1]; + [PageCtrl addtarget:self Action: @selector (pageaction:) forcontrolevents:uicontroleventtouchupinside]; A      the      +     floatImageWidth = width- the; -     floatImageHeight = Max; $      $Imagearray [Email protected][@"TU3",@"TU1",@"TU2",@"Tu4",@"Tu5"]; -      -     //iterate through the Imagearray array and add images to Uiscrollview the      for(intI=0; i<imagearray.count; i++) { -NSString *image =[Imagearray objectatindex:i];Wuyi          theScrollpageview *images = [[Scrollpageview alloc] initWithFrame: (cgrect) {4+width*0.5(In3, imagewidth,imageheight} setimage:[uiimage Imagenamed:image]]; - [ScrollView addsubview:images]; Wu     } - } About  $ //Automate picture Switching -- (void) changeimage{ -CurrentPage = lastpage+1; -[ScrollView Setcontentoffset: (cgpoint) {currentpage*375,0} Animated:yes]; ALastPage =currentpage; +     if(lastpage>3) { theLastPage =-1; -     } $ } the  the- (void) Scrollviewdidscroll: (Uiscrollview *) scrollview{ the     intpage = scrollview.contentoffset.x/ScrollView.frame.size.width; thePagectrl.currentpage =page; - } in  the //going to start dragging off the timer the- (void) Scrollviewwillbegindragging: (Uiscrollview *) scrollview{ About [self timeoff]; the } the  the //end Drag to open timer +- (void) Scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{ - [self timeon]; the }Bayi  the //Turn on Timer the- (void) timeon{ -      -Timer = [Nstimer scheduledtimerwithtimeinterval:1 the target:self the selector: @selector (changeimage) the Userinfo:nil the Repeats:yes]; -      the [[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes]; the } the 94 //Turn off the timer the- (void) timeoff{ the      the [timer invalidate];98Timer =Nil; About } - 101- (void) Pageaction: (uipagecontrol*) page{102     intPagenum = (int) Pagectrl.currentpage;103Cgsize size =scrollView.frame.size;104[ScrollView Setcontentoffset: (cgpoint) {(pagenum+1) *size.width,0} Animated:yes]; the [timer invalidate];106}

iOS Basics (23)--automatic switch between timer nstimer and pictures

Related Article

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.