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