iOS Development Caanimation Detailed

Source: Internet
Author: User

Core Animation, a key animation, is a very powerful set of animation processing APIs that can be used to make very flashy animations, often with less effort. In other words, very powerful functionality can be achieved with a small amount of code. Core animation can be used on Mac OS X and iOS platforms. The animation execution of Core animation is performed in the background and does not block the main thread. It is important to note that the Core animation is directly acting on the Calayer, not uiview.

1. Steps to use
    1. Using it requires first adding the quartzcore.framework framework and introducing the main header file <QuartzCore/QuartzCore.h> (iOS7 not required);
    2. Initializes a Caanimation object and sets some animation-related properties;
    3. By calling Calayer's Addanimation:forkey: method to add the Caanimation object to the Calayer so that the animation can begin;
    4. You can stop the animation in Calayer by calling Calayer's Removeanimationforkey: method.
2. Structure

Caanimation is the parent of all animated objects and is responsible for controlling the duration and speed of the animation, an abstract class that cannot be used directly, and should use its specific subclasses. Property:

duration:动画的持续时间repeatCount:动画的重复次数repeatDuration:动画的重复时间removedOnCompletion:默认为YES,代表动画执行完毕后就从图层上移除,图形会恢复到动画执行前的状态。如果想让图层保持显示动画执行后的状态,那就设置为NO,不过还要设置fillMode为kCAFillModeForwardsfillMode:决定当前对象在非active时间段的行为。比如动画开始之前,动画结束之后beginTime:可以用来设置动画延迟执行时间,若想延迟2s,就设置为CACurrentMediaTime()+2,CACurrentMediaTime()为图层的当前时间timingFunction:速度控制函数,控制动画运行的节奏delegate:动画代理

3. Sub-class
  1. capropertyanimation, which is a subclass of Caanimation and an abstract class, should use its two subclasses to create an animated object: Cabasicanimation and Cakeyframeanimation.

    Attribute parsing:

    1. KeyPath: By specifying a property name of Calayer as KeyPath (nsstring type), and modifying the value of this property of Calayer to achieve the corresponding animation effect. For example, if you specify @ "position" as KeyPath, the value of the position property of the Calayer is modified to achieve the animated effect of panning.
  2. cabasicanimation

    Property resolution:

    1. Fromvalue:keypath The initial value of the corresponding property. The
    2. Tovalue:keypath the end value of the corresponding property.
    3. As the animation progresses, the value of the keypath corresponding property is gradually changed from Fromvalue to Tovalue for the duration of the duration length.
    4. If Fillmode=kcafillmodeforwards and Removedoncomletion=no are complete, the layer remains displayed after the animation is executed. But in essence, the property value of the layer is still the initial value before the animation is executed, and it is not really changed. For example, Calayer's position initial value is (0,0), Cabasicanimation Fromvalue is (10,10), Tovalue is (100,100), although the animation after the completion of the layer remains at (100,100) this position, The position of the physical layer is still (0,0).
  3. cakeyframeanimation, capropertyanimation sub-class, and cabasicanimation the difference is: Cabasicanimation can only from a numeric value (Fromvalue) Change to another value (Tovalue), and Cakeyframeanimation uses a nsarray to save the values.

    Attribute parsing:

    1. Values: This is the Nsarray object. The elements inside are called "keyframes" (keyframe). The animated object displays each keyframe in the values array in the specified time (duration).
    2. Path: You can set a cgpathref\cgmutablepathref so that the layer moves along the path. Path only works on Calayer's anchorpoint and position. If you set the path, values will be ignored.
    3. Keytimes: You can specify a corresponding point in time for the corresponding keyframe, and each time value in the range from 0 through 1.0,keytimes corresponds to each frame in values. When the keytimes is not set, the time of each keyframe is equally divided.
    4. The cabasicanimation can be seen as a cakeyframeanimation with a maximum of 2 keyframes.
  4. Caanimationgroup, a subclass of Caanimation, can hold a set of animated objects, and after the Caanimationgroup object is added to the layer, all animated objects in the group can run concurrently simultaneously.

    Attribute parsing:

    1. Animations: A nsarray used to hold a set of animated objects.
    2. By default, a set of animated objects runs at the same time, or you can change the start time of an animation by setting the BeginTime property of the animated object.
  5. Catransition, a subclass of Caanimation, is used to animate transitions, providing the ability to animate layers to move out of the screen and into the screen. iOS has less transition animations than Mac OS X. The Uinavigationcontroller is an animated effect that pushes the controller's view onto the screen through catransition.

    Attribute parsing:

    1. Type: Animation transition types.
    2. Subtype: Animation transition direction.
    3. Startprogress: The start of the animation (as a percentage of the overall animation).
    4. Endprogress: The end of the animation (as a percentage of the overall animation).
      1. /* 过渡效果
    5.   fade     //交叉淡化过渡(不支持过渡方向) kCATransitionFade
    6.   push     //新视图把旧视图推出去  kCATransitionPush
    7.   moveIn   //新视图移到旧视图上面   kCATransitionMoveIn  reveal   //将旧视图移开,显示下面的新视图  kCATransitionReveal  cube     //立方体翻滚效果  oglFlip  //上下左右翻转效果  suckEffect   //收缩效果,如一块布被抽走(不支持过渡方向)  rippleEffect //滴水效果(不支持过渡方向)  pageCurl     //向上翻页效果  pageUnCurl   //向下翻页效果  cameraIrisHollowOpen  //相机镜头打开效果(不支持过渡方向)  cameraIrisHollowClose //相机镜头关上效果(不支持过渡方向) */ /* 过渡方向  kCATransitionFromRight  kCATransitionFromLeft  kCATransitionFromBottom   kCATransitionFromTop  */// CATransition的使用 CATransition *anim = [CATransition animation];anim.type = @"cube"// 动画过渡类型anim.subtype = kCATransitionFromTop; // 动画过渡方向anim.duration = 1; // 动画持续1s// 代理,动画执行完毕后会调用delegate的animationDidStop:finished:anim.delegate = self;
      1. UIView Animation

        Uikit integrates animations directly into the UIView class, and UIView provides animated support for these changes when some of the internal properties change.

        The work required to perform the animation is done automatically by the UIView class, but you still want to notify the view when you want to perform the animation, for which you need to place the code that changes the property in [UIView Beginanimations:nil Context:nil] and [UIView Commitanimations] Between

        Common method Parsing:

        + (void) Setanimationdelegate: (ID) delegate

        Sets the animation proxy object, which sends a message to the proxy object when the animation starts or ends

        + (void) Setanimationwillstartselector: (SEL) Selector

        When the animation is about to begin, execute the selector of the delegate object and pass in the Beginanimations:context: the parameters passed into the selector

        + (void) Setanimationdidstopselector: (SEL) Selector

        When the animation ends, the selector of the delegate object is executed, and the parameters passed in Beginanimations:context: are passed into the selector

        // describes the need to perform animations [UIView Beginanimations:nil context:nil]; // set the duration of an animation [UIView setanimationduration:1]; // Set Transition animations [UIView setanimationtransition:uiviewanimationtransitioncurlup forView:self.view Cache:yes]; // swap the location of a child view [Self.view Exchangesubviewatindex:0 withsubviewatindex:1]; // Submit Animation [UIView commitanimations];

        UIView Animation

        + (void) Setanimationduration: (nstimeinterval) Duration//duration of the animation, in seconds+ (void) Setanimationdelay: (nstimeinterval) Delay//animation delays delay seconds before starting+ (void) Setanimationstartdate: (NSDate *) StartDate//start time of the animation, default to now+ (void) Setanimationcurve: (Uiviewanimationcurve) Curve//The rhythm of the animation control, specifically see the following "Remarks"+ (void) Setanimationrepeatcount: (float) RepeatCount//number of repetitions of an animation+ (void) Setanimationrepeatautoreverses: (BOOL) repeatautoreverses//If set to Yes, the effect of each repetition of the animation is the opposite of the previous one+ (void) Setanimationtransition: (uiviewanimationtransition) Transitionforview: (UIView *) View cache: (BOOL) cache

        Block animation

        + (void) Animatewithduration: (nstimeinterval) Duration delay: (nstimeinterval) Delay options: (uiviewanimationoptions) Options animations: (void (^) (void)) animations completion: (void (^) (BOOL finished)) completion

        Parameter resolution:

        Duration: Duration of animation

        Delay: Animation delays delay after seconds start

        Options: Rhythm Control for animations

        Animations: Place code that changes the properties of the view in this block

        Completion: This block is automatically called after the animation is finished

        + (void) Transitionwithview: (UIView *) View Duration: (nstimeinterval) Duration options: (uiviewanimationoptions) Options animations: (void (^) (void)) animations completion: (void (^) (BOOL finished)) completion

        Parameter resolution:

        Duration: Duration of animation

        View: Views that require a transition animation

        Options: Types of transition animations

        Animations: Place code that changes the properties of the view in this block

        Completion: This block is automatically called after the animation is finished

        + (void) Transitionfromview: (UIView *) Fromview Toview: (UIView *) Toview Duration: (nstimeinterval) Duration options: ( uiviewanimationoptions) Options Completion: (void (^) (BOOL finished)) completion

        After the method call is complete, the following two lines of code are executed:

        Add Toview to Parent view

        [Fromview.superview Addsubview:toview];

        Remove the Fromview from the parent view

        [Fromview.superview Removefromsuperview];

        Parameter resolution:

        Duration: Duration of animation

        Options: Types of transition animations

        Animations: Place code that changes the properties of the view in this block

        Completion: This block is automatically called after the animation is finished

        Frame Animations for Uiimageview

        Uiimageview allows a series of images to be displayed sequentially within a given time.

        Related attribute parsing:

        Animationimages: The picture to be displayed (a nsarray with UIImage).

        Animationduration: The time required to fully display all the pictures in the animationimages.

        Animationrepeatcount: The number of times the animation was executed (default = 0, which represents an infinite loop)

        Related method parsing:

        -(void) startanimating; Starts the animation.

        -(void) stopanimating; Stops the animation.

        -(BOOL) isanimating; Whether the animation is running.

        Uiactivityindicatorview

        is a rotational progress wheel that can be used to inform the user that an operation is in progress and is normally initialized with Initwithactivityindicatorstyle.

        Method parsing:

        -(void) startanimating; start animation

        -(void) stopanimating; Stop animation

        -(BOOL) isanimating; Whether the animation is running

        The Uiactivityindicatorviewstyle has 3 values to choose from:

        Uiactivityindicatorviewstylewhitelarge//Large white indicator

        Uiactivityindicatorviewstylewhite//Standard size white indicator

        Uiactivityindicatorviewstylegray//Grey indicator for white background

iOS Development Caanimation Detailed

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.