Turn off the love paper tail-----quartz2d timer Cadisplaylink Snow effect

Source: Internet
Author: User

Timer Cadisplaylink Snow Effect

1. Timer Snowflake Overall Idea:
Draw a snowflake on the controller view surface first.
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. Add Timer Implementation Scheme
The first is the use of nstime
The second is the use of Cadisplaylink
Finally, the Cadisplaylink scheme is adopted.

2.1 Why use Cadisplaylink solution without nstime?
The first thing to know about Setneedsdisplay
The Setneedsdisplay layer will call the DrawRect method to 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 stuttering.

When using Cadisplaylink, its timer method is called every time the screen is refreshed (usually the screen refreshes 60 times a second)
It coincides with the timing of the setneedsdisplay call to the DrawRect method, and does not occur between waiting intervals. 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 (setneedsdisplay)];
To get Cadisplaylink to work, you have to add it to the main run loop.
As long as it's added to the main run loop, it's not related to mode
[link Addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode];

3. The specific implementation code is as follows:

1-(void) awakefromnib{2Cadisplaylink *link =[Cadisplaylink displaylinkwithtarget:self selector: @selector (Setneedsdisplay)];3     [link Addtorunloop:[nsrunloop mainrunloop] formode:nsdefaultrunloopmode];4 5 }6 7 8- (void) DrawRect: (cgrect) rect {9 if(_snowy >rect.size.height) {Ten_snowy =0; One       } AUIImage *image = [UIImage imagenamed:@"Snowflakes"]; -[Image Drawatpoint:cgpointmake (0, _snowy)]; -_snowy + =Ten; the}

Turn off the love paper tail-----quartz2d timer Cadisplaylink Snow effect

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.