Timer Uitimer and view object movement
In ViewController.h
#import <UIKit/UIKit.h>@interface ViewController : UIViewController{ //定义一个定时器对象 //可以在每隔固定的时间发送一个消息 //通过消息来调用相应的事件函数 //通过此函数可在固定时间段来完成一个根据时间间隔的任务 NSTimer * _timeView;}//定时器的属性对象@property(retain,nonatomic) NSTimer * timeView;@end
In VIEWCONTROLLER.M
#import "ViewController.h" @interface viewcontroller ()@end @implementation viewcontroller //Properties and member variables synchronization@synthesizeTimeview =_timeview;-(void) Viewdidload {[SuperViewdidload];additional setup after loading the view, typically from a nib. //Start Timer button UIButton* BTN =[UIButtonButtonwithtype:uibuttontyperoundedrect];//Currently we use Uibuttontyperoundedrect to set the fillet is no longer possible, need to add the following 2 sentences around the rounded corners[BTN. LayerSetmaskstobounds:YES]; [BTN. LayerSetcornerradius:10.0];//Set Rectangle four fillet radiusBtn. Frame=cgrectmake ( -, -, -, +); [Btn settitle:@"Timer button"Forstate:uicontrolstatenormal]; Btn. BackgroundColor=[UicolorGreencolor]; [BTN AddTarget: SelfAction@selector(Pressstart) forcontrolevents:uicontroleventtouchupinside]; [ Self. ViewADDSUBVIEW:BTN];//Stop Timer button UIButton* Btnstop =[UIButtonButtonwithtype:uibuttontyperoundedrect];//Currently we use Uibuttontyperoundedrect to set the fillet is no longer possible, need to add the following 2 sentences around the rounded corners[Btnstop. LayerSetmaskstobounds:YES]; [Btnstop. LayerSetcornerradius:10.0];//Set Rectangle four fillet radiusBtnstop. Frame=cgrectmake ( -, $, -, +); [Btnstop settitle:@"Stop Timer"Forstate:uicontrolstatenormal]; Btnstop. BackgroundColor=[UicolorRedcolor]; [Btnstop AddTarget: SelfAction@selector(Pressstop) forcontrolevents:uicontroleventtouchupinside]; [ Self. ViewAddsubview:btnstop];//Create a view UIView* View =[[UIViewAlloc]init]; View. Frame=cgrectmake (0,0, the, the); View. BackgroundColor=[UicolorOrangecolor]; [ Self. ViewAddsubview:view];//Set the label value of the View //The corresponding View object can be obtained through the Father View object and the view tag valueView. Tag=101;}//Press the Start button function- (void) pressstart{//nstime class method to create a timer and start this timer //P1: How long each timer function is called, in seconds //P2: Represents the object that implements this timer function //P3: Timer function Object //P4: You can pass in a parameter in the Timer function, no parameter passed in nil //P5: Whether the timer repeats operation yes means repeat, no means only one function call is completed//_timeview = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (updatetime) Userinfo:nil Repeats:yes];_timeview = [Nstimer scheduledtimerwithtimeinterval:0.03Target SelfSelector@selector(updatetime:) userinfo:@"Apple"RepeatsYES];}//Timer function//-(void) updatetime{//NSLog (@ "test11");//}//Pass the timer itself as a parameter- (void) UpdateTime: (nstimer*) timer{NSLog(@"Test11 name=%@", timer. UserInfo);//Best tag starting from 100, avoid conflicts UIView* View =[ Self. ViewViewwithtag:101]; View. Frame=cgrectmake (view. Frame. Origin. x+1, view. Frame. Origin. Y+1, the, the);}//Press the Stop button function- (void) pressstop{//Stop timer if(_timeview!=Nil) {[_timeview invalidate]; }}- (void) Didreceivememorywarning {[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}@end
iOS development from getting started to mastering--timer Uitimer and view object movement