07-Timer Snowflakes

Source: Internet
Author: User

07-Timer snowflake

1. Timer Snowflake Overall idea: first draw a snowflake on the controller view surface.    After the view has finished loading, add a timer.    The method of drawing is called in the Timer method.    In the drawing method, the Y value of the snowflake is modified without paragraph.    When the Y value of the snowflake exceeds the height of the screen, the Y value of the snowflake is reset to 0. start at the top. 2. Adding a timer implementation The first uses the Nstime the second kind uses the Cadisplaylink finally uses the Cadisplaylink scheme.       2.1 Why use Cadisplaylink solution without nstime?       The first thing to know about Setneedsdisplay Setneedsdisplay is to call the DrawRect method redraw.       But it's not going to redraw right away. It simply sets a redraw flag and then calls the DrawRect method when the next screen refreshes.       If you use Nstime, it is assumed that 0.01 calls are redrawn at a time. Suppose the screen is refreshed once for 0.02 seconds. The middle will wait 0.01 seconds. That is, each time you wait for 0.01 seconds to accumulate. Let the change become more and more lag. When using Cadisplaylink, its timer method is that the screen will be called every time it refreshes (usually the screen refreshes 60 times a second) and Setneedsdisplay calls Drawrec    The timing of the T method coincides with the waiting interval. No screen lag occurs.        2.2 How do I add a timer using Cadisplaylink?        Target: Which object to listen to.        Selector: The method name of the listener. Cadisplaylink *link = [Cadisplaylink displaylinkwithtarget:self selector: @selector (setneeds        Display)];        To get Cadisplaylink to work, you have to add it to the main run loop.  As long as it is added to the main run loop, it has no relation to the schema [link addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode]; 3. The specific implementation code is as follows:-(void) AWAKEFromnib{Cadisplaylink *link = [Cadisplaylink displaylinkwithtarget:self sele            ctor: @selector (Setneedsdisplay)];          [link Addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode];            }-(void) DrawRect: (cgrect) Rect {if (_snowy > Rect.size.height) {_snowy = 0;            } UIImage *image = [UIImage imagenamed:@ "Snowflake"];            [Image drawatpoint:cgpointmake (0, _snowy)];         _snowy + = 10; }

07-Timer Snowflakes

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.