Motion Design for IOS (21)

Source: Internet
Author: User

Once you have completed the perfect motion of your animation, you only need to insert the damping, stiffness and mass values into your animation code, and then whatever you animate will be the same as the correct values for your previous action.

We also need to let JNWSpringAnimation the object know what we want the start and end values of the animated property to be. This is used to draw the spring and keyframe values.

jnwspringanimation *scale = [ Jnwspringanimation animationwithkeypath:@ "Transform.scale" ]  Scale.damping  = 9   Scale.stiffness  = 100   Scale.mass  = 2   Scale.fromvalue  = @ (1.0 )  Scale.tovalue  = @ (2.0 )   

Now that our JNWSpringAnimation object knows its start and end values, and the exact properties of the springs we want to imitate, we can now add it to the move we want to make CALayer . In our case, we're going to add it to the RedBall.

Jnwspringanimation *scale = [Jnwspringanimation animationwithkeypath:@"Transform.scale"];Scale. Damping=9;Scale. Stiffness= -;Scale. Mass=2;Scale. Fromvalue= @(1.0);Scale. Tovalue= @(2.0);[RedBall. LayerAddanimation:scale Forkey:scale. KeyPath];

The animation named scale is now added to the Redball.layer based on the given critical path (also known as the value we want to change on the layer). We can pass "Transform.scale" into the Forkey: parameter, but we can also just pass in the exact key path of the animation we created, so that we don't confuse JNWSpringAnimation the critical path and the key path we use to coordinate the animation. In this example, we create an animation using the critical path "Transform.scale" can be written directly into Scale.keypath.

If we create and run our code, this is the resulting animation.

Now if you want to use in Swift engineering JNWSpringAnimation , since you are using a objective-c framework, you need to use something called "Bridge Street" to let Xcode know that you want to use a non-swift framework in your swift code. So first, I drag the JNWSpringAnimation . h and. m files called Jnwswift into my Swift project in Xcode (included in the Xcode project file). Xcode will ask if you want to create a bridge on the street, I choose to, this is the content of which special file.

// This is within the JNWSwift-Bridging-Header.h file// that was automatically created for me#import "JNWSpringAnimation.h"#import "NSValue+JNWAdditions.h"

Any objective-c header files added to this special Bridge Street file will be set to swift visible, so you can use Swift to interact with their objective-c functions. The cool part is that when you want to use them in your swift code, you don't need to have any import instructions, and Xcode will handle it.

When the bridge is set up, you can go into your swift code and start processing the object you want to manipulate, in this case, the jnwspringanimation . Here is the code I wrote with Swift to create the same animation as the example above, still using jnwspringanimation .

scale"transform.scale")scale9.0scale100scale2scale.fromValue = NSNumber(float1.0)scale.toValue = NSNumber(float2.0)redBall.layer.addAnimation(scalescale.keyPath)

It looks very close to the OBJECTIVE-C code above, but of course it doesn't contain the square brackets that call the method, and if you write JavaScript, it looks very similar.

This is the same animation that the swift code and the OBJECTIVE-C code will create.

Finished viewing the integration set: Https://github.com/Cloudox/Motion-Design-for-iOS
All rights reserved: Http://blog.csdn.net/cloudox_

Motion Design for IOS (21)

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.