How to customize animation attributes in CoreAnimation

Source: Internet
Author: User

CoreAnimation can animation CALayer in UIView. The principle is very simple. The user provides an initial value, the end value and the animation duration, and CoreAnimation automatically performs

Interpolation (Median value is generated) between initial values and end values based on the animation duration, generating intermediate frames, and playing them in a separate thread, programmers no longer need to draw intermediate frames themselves.

When CoreAnimation is animated to CALayer, You need to specify which attribute of CALayer is interpolated, that is, the variable @ property must be declared, use the animationWithKeyPath method of the virtual super class CAPropertyAnimation of each animation to specify it. For example, the following code describes how to perform Interpolation on the postion attribute of CALayer.

CABasicAnimation *aBasicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];

By default, the animation attributes specified by CALayer are fixed. For example, bounds and postion can be found in XCode development documents. However, sometimes, we need to animation the Custom Attributes. This requires subclass CALayer and reload its needsDisplayForKey method.

+ (BOOL)needsDisplayForKey:(NSString *)aKey {
if ([aKey isEqualToString:@"myProperty"]) {
return (YES);
} else {
return ([super needsDisplayForKey:aKey]);
}
}

Here, tell CoreAnimation that my CALayer subclass has
The myProperty property. Perform animation Interpolation on it. Next, you can reload


The drawInContext method adds the key frame painting code.

Note that when subclass CALayer is used, because CoreAnimation generates a large number of intermediate frames using the Copy operation, when copying CALayer data, it can only copy the original attribute members of CALayer. It does not add objects such as object references after copying. This requires the programmer to reload

 

- (id)initWithLayer:(id)layer
{
self = [super initWithLayer:layer];
if(self != nil) {
MyLayer *myLayer = (MyLayer*)layer;
self.aUIImage = cl.aUIImage;
}

return (self);
}

To vividly copy resources that cannot be automatically copied.

 

Write some tips, such

In drawing methods such as drawInContex, try to avoid calling meta-drawing such as CGContextDrawImageInRect, because these meta-drawing operations are time-consuming and cannot help with hardware acceleration. Try to pass CGImageRef to CALayer. the contents attribute method transmits the content to CALayer in advance, and then performs animation transformation through the affine or 3D transform method,

Because the affine or 3D transform is completely hardware accelerated, it is much faster than writing the drawing code.

 

 

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.