Cat Learn iOS (33) UI quartz2d snowflake falling effect Brush frame

Source: Internet
Author: User

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

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.