IOS Coreanimation Frames-per-frame animation Cadisplaylink

Source: Internet
Author: User
Tags set background

This article refers to: http://www.cnblogs.com/kenshincui/p/3972100.html#autoid-3-0-0 summary:


Frames-per-frame animation Cadisplaylink

Animation effect:


This method is executed every time a screen refresh is implemented in conjunction with the Runloop (nearly 60 per second)


Update the picture in this method, or update a state of the layer to animate and not feel the animation's stagnation effect


Of course Uiimageview by setting the properties of Animationimages and then startanimating method to play this group of photos, also

You can animate frames-by-frame, but there are a lot of performance issues , and once you've set up the process in the middle of the picture, you can't control

Made,

using timer Nstimer to update the image can also achieve frame-by-frames effect , this can solve the load of multiple images at once

Performance issues, and playback can be controlled, but may cause the animation to become intermittent because the system executes a method

The continuity problem

Here the system provides the Cadisplaylink object, Cadidsplaylink is a timer, different from the timer

Yes,the refresh period of the Cadidsplaylink is exactly the same as the screen, and the screen refresh cycle is 60 times per second, making it completely feel

To the stagnation of the animation

The iOS program goes into a message loop after it runs (the message loop is called the "main Run Loop"), and the loop is quite

In a dead loop , always waiting for user input, adding cadisplaylink to the main run cycle queue, its clock week

is consistent with the main run cycle, and the main run cycle is the screen refresh cycle. Cadidsplaylink added to the main transport

After the line loop queue, the target method is called and the frame-by-frames animation is completed.


It is to be stressed here that the performance of frame-by- frames is bound to be low, but for some things the movement has to choose to use

Frames-by-frame animations, such as human motion, which is a highly complex movement, basic animations, keyframe animations are impossible to solve

Of All of us must pay attention to reducing the complexity of the algorithm as much as possible in the cyclic method, while ensuring that the memory peaks during the cycle

Values as low as possible. The following is a sample of the movement of a fish to show you a frame by animation.

linkviewcontroller.m//cakeyframeanimation////Created by the Emperor Phlogistic Demon on 16/5/26.//copyright©2016 year Emperor Phlogistic demon. All rights reserved.//#import "LinkViewController.h" #define Image_count 10@interface Linkviewcontroller () {Calayer *_l    Ayer    int _index; Nsmutablearray *_images;}        @end @implementation linkviewcontroller-(void) viewdidload {[Super viewdidload]; Set background//layer has a property to directly set the content----> Background Image self.view.layer.contents = (id) [UIImage imagenamed:@ "BG"].        Cgimage;    Create an image display layer _layer = [[Calayer alloc] init];    _layer.bounds = CGRectMake (0, 0, 87, 32);    _layer.position = Cgpointmake (self.view.center.x, SELF.VIEW.CENTER.Y);        [Self.view.layer Addsublayer:_layer];    Because the small fish picture is relatively small, so directly create _images = [Nsmutablearray array];        for (int i = 0; i < ++i) {nsstring *imagename = [NSString stringwithformat:@ "Fish%i.png", I];        UIImage *image = [UIImage imagenamed:imagename];    [_images Addobject:image]; }//define Clock object CadisPlaylink *displaylink = [Cadisplaylink displaylinkwithtarget:self selector: @selector (step)];        Adding a clock object to the main run loop [DisplayLink Addtorunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode];        Add animation [self addanimation]; Do any additional setup after loading the view.} #pragma mark----Add keyframe animations---> Give fish a swimming route-(void) addanimation{//1. Create an Animated object cakeyframeanimation *animation = [CAKe        Yframeanimation animationwithkeypath:@ "position"];    Cgmutablepathref path = cgpathcreatemutable ();    Cgpathmovetopoint (Path, NULL, 430, 600);    Cgpathaddcurvetopoint (Path, NULL, 100, 600, 400, 200,-10, 50);               Animation.path = path;    Animation.repeatcount = Huge_valf;    Animation.duration = 8.0;        Animation.removedoncompletion = NO;         [_layer addanimation:animation forkey:@ "Fishanimation"];    Cgpathrelease (path);    This method is executed once #pragma mark---every screen refresh (nearly 60 times per second)-(void) step{//define a variable record number of executions static int a = 0;    if (++a% 10 = = 0) {    UIImage *image = _images[_index]; _layer.contents = (ID) image. Cgimage;    Update picture _index = (_index + 1)% Image_count;    }}-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [SEG    UE Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end


IOS Coreanimation Frames-per-frame animation Cadisplaylink

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.