Cat Share, must boutique
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
Effect:
Can be added to the random number to achieve true falling effect oh.
Code:
-(ID) Initwithcoder: (Nscoder *) adecoder{//Please note that it is important to initialize the constructor of the parent class first if( Self=[SuperInitwithcoder:adecoder]) {NSLog(@"Initwithcoder:");//nstimer is typically used to periodically update some non-interface data to tell how often to call once //Using a timer, the use of the timer will be the phenomenon of lag //[Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (updateimage) Userinfo:nil repeats : YES]; //Cadisplaylink brush frame, default refresh 60 times per second //After the timer is created, it is not executed by default and needs to be loaded into the message loopCadisplaylink *display= [Cadisplaylink displaylinkwithtarget: SelfSelector@selector(UpdateImage)]; [Display Addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode]; }return Self;} -(void) updateimage{//Call this method to redraw the screen[ SelfSetneedsdisplay];} -(void) awakefromnib{NSLog(@"Awakefromnib");} - (void) DrawRect: (CGRect) rect{//Draw the picture onto the view //Each time the method is called to redraw the screen, the value of Imagey is +5 Self. Imagey+=5;//Judging, when snowflakes go out of the screen, let the picture begin to land from the beginning if( Self. Imagey>rect. Size. Height) { Self. Imagey=0; }UIImage*image=[UIImageimagenamed:@"Snow"]; [Image Drawatpoint:cgpointmake (188, Self. Imagey)];UIImage*image2=[UIImageimagenamed:@"Cat"]; [Image2 Drawatpoint:cgpointmake (Ten, Self. Imagey)];}
Important Notes
(1) Sequence of calls to the following two methods
-(void) awakefromnib
-(ID) Initwithcoder: (Nscoder *) Adecoder
Tip: If view is created from Xib or storyboard, you can call the Awakefromnib method to archive. Creating a view from a file will actually call the Initwithcoder method first. Xib and storyboard are also documents.
Above two methods,-(ID) Initwithcoder: (Nscoder *) Adecoder will be called first. Implementing this method requires implementing the Nscoding protocol, which is already implemented by default because of the UIView created.
You can go to the head file to view:
Timer
The first one:
[Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (updateimage) Userinfo:nil Repeats:YES];
Description: Nstimer is generally used to periodically update some non-interface data, telling how long to call once
The second one:
CADisplayLink *display= [CADisplayLink displayLinkWithTarget:self selector:@selector(updateImage)]; [display addToRunLoop:[NSRunLoopmainRunLoop] forMode:NSDefaultRunLoopMode];
Description: Cadisplaylink brush frame, default refresh 60 times per second. After the timer is created, it is not executed by default and needs to be loaded into the message loop
Cat Learn iOS (33) UI quartz2d snowflake falling effect Brush frame