IOS Animation Basics-Explicit animations

Source: Internet
Author: User
Tags uikit

Summary

Explicit Animation Property Animations
cabasicanimation *animation = [cabasicanimation animation];        [self updatehandsanimated:no];         animation.keypath = @ "transform";         animation.tovalue = [NSValue  valuewithcatransform3d:transform];        animation.duration =  0.5;        animation.delegate =  self;        [animation setvalue : Handview forkey:@ "Handview"];         
Key-Frame Animations

Cakeyframeanimation is another class of uikit that is not exposed but powerful. Like Cabasicanimation, Cakeyframeanimation is also a subclass of Capropertyanimation, which still works on a single attribute, but unlike Cabasicanimation, It is not limited to setting a starting and ending value, but it can be animated according to a series of random values.

KeyFrames originate from drive animations, meaning that the dominant animation redraws the current frame (i.e. keyframes) when a significant change occurs, and that the remaining draw (which can be extrapolated through keyframes) between each frame is done by a skilled artist. Cakeyframeanimation is the same thing: you provide a significant frame, and then the core animation is inserted between each frame.

- (Ibaction) changecolor{Create a keyframe animationCakeyframeanimation *animation = [Cakeyframeanimation Animation]; Animation.keypath =@ "BackgroundColor"; Animation.duration =2.0; Animation.values = @[(__bridgeID) [Uicolor Bluecolor]. Cgcolor, (__bridgeID) [Uicolor Redcolor]. Cgcolor, (__bridge ID) [uicolor Greencolor]. Cgcolor, (__bridge ID) [uicolor Bluecolor].    Cgcolor]; //apply animation to layer [self.colorlayer addanimation:animation Forkey:nil];}     
Animation Group

Cabasicanimation and cakeyframeanimation only work on individual properties, and Caanimationgroup can group them together. Caanimationgroup is another subclass that inherits from Caanimation, and it adds a property of a animations array that can be used to assemble other animations.

- (void) viewdidload{[Super Viewdidload];Create a pathUibezierpath *bezierpath = [[Uibezierpath alloc] init]; [Bezierpath moveToPoint:Cgpointmake (0,150)]; [Bezierpath Addcurvetopoint:Cgpointmake (300,CONTROLPOINT1):Cgpointmake (75,0) ControlPoint2:Cgpointmake (225,300)];Draw the path using a CashapelayerCashapelayer *pathlayer = [Cashapelayer layer];    Pathlayer.path = Bezierpath.cgpath; Pathlayer.fillcolor = [Uicolor Clearcolor].    Cgcolor; Pathlayer.strokecolor = [Uicolor Redcolor].    Cgcolor; Pathlayer.linewidth =3.0f; [Self.containerView.layer Addsublayer:pathlayer];Add a colored layerCalayer *colorlayer = [Calayer layer]; Colorlayer.frame =CGRectMake (0,0,64,64); Colorlayer.position =Cgpointmake (0,150); Colorlayer.backgroundcolor = [Uicolor Greencolor].    Cgcolor; [Self.containerView.layer Addsublayer:colorlayer];Create the Position animationCakeyframeanimation *animation1 = [Cakeyframeanimation Animation]; Animation1.keypath =@ "position";    Animation1.path = Bezierpath.cgpath; Animation1.rotationmode = Kcaanimationrotateauto;Create the Color animationCabasicanimation *animation2 = [cabasicanimation animation];    animation2.keypath =  @ "BackgroundColor"; Animation2.tovalue = (__bridge id) [uicolor Redcolor].    Cgcolor;    //create Group Animation Caanimationgroup *groupanimation = [caanimationgroup animation];     Groupanimation.animations = @[animation1, Animation2];    Groupanimation.duration = 4.0; //add the animation to the color layer [colorlayer addanimation:groupanimation forkey:nil];}   

Transition

Sometimes, for iOS applications, you want to make some changes to the more difficult-to-animate layouts with property animations. such as exchanging text and pictures, or replacing them with a grid view, and so on. Property animations work only on the animated properties of the layer, so if you want to change a property that cannot be animated (a slice), or add or remove layers from a hierarchy, the property animation will not work.

So there is the concept of transition. Transitions do not animate between two values as smoothly as property animations, but affect the entire layer's changes. Transition animations first show the appearance of the previous layer and then transition to a new look through a swap.

To create a transition animation, we will use Catransition, which is also a subclass of another caanimation, unlike other subclasses, Catransition has a type and subtype to identify the transform effect. The Type property is a nsstring that can be set to the following types:

kCATransitionFade kCATransitionMoveIn kCATransitionPush kCATransitionReveal

You can only use these four types so far, but there are some other ways to customize transitions, the default transition type is Kcatransitionfade, and when you change the properties of a layer, you create a smooth fade effect. Kcatransitionpush, it creates a new layer that slides in from one side of the edge to the effect of pushing the old layer out of the other side.

Kcatransitionmovein and Kcatransitionreveal, like Kcatransitionpush, have implemented a directional sliding animation, but with some subtle differences, Kcatransitionmovein from the top, but does not push the old soil down like a push animation, but kcatransitionreveal slides the original layer out to show the new look, rather than sliding the new layer into it.

The next three transition types have a default animation direction, they all slide in from the left, but you can control their direction by subtype, providing the following four types:

Kcatransitionfromright

Kcatransitionfromleft

Kcatransitionfromtop

Kcatransitionfrombottom

@interfaceViewcontroller()@property (Nonatomic,WeakIboutletUiimageview *imageview;@property (Nonatomic,CopyNsarray *images;@end@implementationviewcontroller-(void) viewdidload{[Super Viewdidload];Set up imagesSelf.images = @[[UIImage imagenamed:@ "Anchor.png"], [UIImage imagenamed:@ "Cone.png"], [UIImage imagenamed:@ "Igloo.png"], [UIImage imagenamed:@ "Spaceship.png"];} - (Ibaction) switchimage{Set up Crossfade transitionCatransition *transition = [Catransition Animation]; Transition.type = Kcatransitionfade;Apply transition to ImageView backing layer [self.imageview.layer addanimation:transition forkey: nil];    //cycle to next  image    uiimage *currentimage = self.imageview.image;    NSUInteger  Index = [self.images indexofobject:currentimage];     index =  (Index + 1)  % [ self.images count];     Self.imageview.image = self.images[index];}  @end            

As you can see from the code, the Transition animation is consistent with the previous property animation or the animation group being added to the layer, all through the-addanimation:forkey: method. However, unlike property animations, you can only use catransition once for a given layer, so no matter what value you set for the animation's key, the transition animation will set its key to "transition", which is the constant kcatransition.

Custom animations

Apples through UIView +transitionfromview:toview:duration:options:completion: and +transitionwithview:duration:options: Animations: The method provides the transition characteristics of the core animation. However, the available transition options here are completely different from the constants provided by the Catransition type property. The options parameter in the UIView transition method can be specified by the following constants:

Uiviewanimationoptiontransitionflipfromleft

Use the method provided by Uikit to animate transitions

@interfaceViewcontroller()@property (Nonatomic,WeakIboutletUiimageview *imageview;@property (Nonatomic,CopyNsarray *images;@end@implementationviewcontroller-(void) viewdidload{[Super Viewdidload];Set up imagesSelf.images = @[[UIImage imagenamed:@ "Anchor.png"], [UIImage imagenamed:@ "Cone.png"], [UIImage imagenamed:@ "Igloo.png"], [UIImage imagenamed:@ "Spaceship.png"]];-(ibaction) switchimage{[UIView Transitionwithview:Self.imageview Duration:1.0 Options:Uiviewanimationoptiontransitionflipfromleft animations:^{Cycle to next imageUIImage *currentimage =Self.imageView.image;Nsuinteger index = [self.images indexofobject:currentimage];                          index =  (Index + 1)  % [ self.images count];                         self.imageView.image  = self.images[index];                     }                     completion:null";}  @end       

IOS Animation Basics-Explicit animations

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.