Cat learn iOS (40) UI Core Animation _ Jitter Effect _cakeyframeanimation

Source: Internet
Author: User

Cat Share, must boutique

Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents

Effect:

Effect One:

Effect Two:

Code:
////NYVIEWCONTROLLER.M//Image jitter////Created by Apple on 15-5-8.//Copyright (c) 2015 Znycat. All rights reserved.//#import "NYViewController.h"  @interface nyviewcontroller ()@property(nonatomic,Weak)Uiimageview*iconview;@end @implementation nyviewcontroller -(Uiimageview*) iconview{if(_iconview = =Nil) {Uiimageview*iconview = [[Uiimageviewalloc]initwithimage:[UIImageimagenamed:@"Cat"]]; IconView. Center= Cgpointmake ( -, the); [ Self. ViewAddsubview:iconview]; _iconview = IconView;return_iconview; }return_iconview;} - (void) viewdidload{[SuperViewdidload]; [ SelfIconView];}//Start jitter-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[ SelfPosition]; [ SelfRotation];}//Keyframe animation movement- (void) Position {//1. Create a core animationCakeyframeanimation *keyanima = [cakeyframeanimation animation];//1.1 Tell the system what animations to performKeyanima. KeyPath= @"Position";//Nsvalue *V1 = [Nsvalue valuewithcgpoint:cgpointmake (0)];Nsvalue *v1 = [Nsvalue valuewithcgpoint:cgpointmake ( -, -)]; Nsvalue *v2 = [Nsvalue valuewithcgpoint:cgpointmake ( -, -)]; Nsvalue *v3 = [Nsvalue valuewithcgpoint:cgpointmake ( -, -)]; Nsvalue *v4 = [Nsvalue valuewithcgpoint:cgpointmake ( -, -)]; Nsvalue *v5 = [Nsvalue valuewithcgpoint:cgpointmake ( -, -)]; Keyanima. Values= @[v1, v2, v3, V4, V5];//Keyanima.keytimes = @[@ (0.5), @ (0.5), @ (0.5)];Keyanima. Timingfunction= [Camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout];//1.2 The state after the save execution is completed    //1.2.1 Do not delete animations after executionKeyanima. Removedoncompletion=NO;//1.2.2 Save up-to-date status after executionKeyanima. Fillmode= Kcafillmodeforwards;//1.3 Set animation timeKeyanima. Duration=2;//2. Watch when the animation starts and when it's doneKeyanima. Delegate= Self;//2. Add Core animations[ Self. IconView. LayerAddanimation:keyanima Forkey:Nil];}//Animation jitter effect Rotation- (void) Rotation {//1, creating a core animationCakeyframeanimation *keyanima = [cakeyframeanimation animation];//2, tell the system what animations to perform. Keyanima. KeyPath= @"Transform.rotation";//(-m_pi_4/90.0 * 5) indicates-5 degrees. Keyanima. Values= @[@ (-m_pi_4/90.0*5), @ (M_pi_4/90.0*5), @ (-m_pi_4/90.0*5)];//1.2.1 Do not delete animations after executionKeyanima. Removedoncompletion=NO;//1.2.2 Save up-to-date status after executionKeyanima. Fillmode= Kcafillmodeforwards;//Animation Execution TimeKeyanima. Duration=0.2;//Set the number of repetitions. Keyanima. RepeatCount= Maxfloat;//2. Add Core animations[ Self. IconView. LayerAddanimation:keyanima Forkey:Nil];}@end
Introduction to Cakeyframeanimation Frame animation

Capropertyanimation subclasses, the difference with cabasicanimation is that cabasicanimation can only change from one numeric value (Fromvalue) to another (Tovalue), And Cakeyframeanimation will use a nsarray to save these values

Attribute parsing:

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), in turn
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, the values will be ignored
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.
Cabasicanimation can be seen as a maximum of only 2 keyframes cakeyframeanimation

Usage steps:

1. Create a core animation

    CAKeyframeAnimation *keyAnima = [CAKeyframeAnimation animation];

1.1 Tell the system what animations to perform

@"position";    NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];    NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(250, 100)];    NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(250, 300)];    NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(100, 300)];    NSValue *v5 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];    keyAnima.values = @[v1, v2, v3, v4, v5];    keyAnima.timingFunction =  [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

1.2 Save status after execution
1.2.1 Do not delete animations after execution finishes

keyAnima.removedOnCompletion = NO;

Save up-to-date status after 1.2.2 execution

  keyAnima.fillMode = kCAFillModeForwards;

1.3 Setting Animation time

 keyAnima.duration = 2;

2. Watch when the animation starts and when it's done

 keyAnima.delegateself;

2. Add core animations to Calayer

[selfaddAnimation:forKey:nil];

Cat learn iOS (40) UI Core Animation _ Jitter Effect _cakeyframeanimation

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.