An implicit animation
-
Almost all CALayer properties can be animated implicitly
-
CATransaction can control implicit animation (execution time or turn implicit animation off)
-
UIView turns this feature off for its associated layer without animation
-
Two important approaches:
- (instancetype)presentationLayer
- (instancetype)modelLayer
Copy the code
Explicit animation
Attribute animation
CAAnimation implements the KVC protocol. Properties can be accessed using -setValue:forKey: and -valueForkey: methods. But CAAnimation, like NSDictionary, can set keys arbitrarily. So, it is especially convenient when multiple animations need to be distinguished in proxy methods:
. CABasicAnimation *animation = [CABasicAnimation animation]; [self updateHandsAnimated:NO]; animation.keyPath = @"transform"; animation.toValue = [NSValue valueWithCATransform3D:transform]; Animation. Duration = 0.5; animation.delegate = self; [animationsetValue:handView forKey:@"handView"];
[handView.layer addAnimation:animation forKey:nil]; . - (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag { //set final position for hand view
UIView *handView = [anim valueForKey:@"handView"];
handView.layer.transform = [anim.toValue CATransform3DValue];
}
Copy the code
Keyframe animation
Since CAKeyframeAnimation has a path property, so~ can be used with Bezier curves to achieve complex animations.
Virtual properties
CALayer doesn’t explicitly provide attributes like Angle or direction. To rotate the layer, we can apply an animation to the transform.rotation critical path instead of the transform itself.
- (void)viewDidLoad
{
[super viewDidLoad];
//add the ship
CALayer *shipLayer = [CALayer layer];
shipLayer.frame = CGRectMake(0, 0, 128, 128);
shipLayer.position = CGPointMake(150, 150);
shipLayer.contents = (__bridge id)[UIImage imageNamed: @"Ship.png"].CGImage;
[self.containerView.layer addSublayer:shipLayer];
//animate the ship rotation
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"transform.rotation"; Animation. Duration = 2.0; animation.byValue = @(M_PI * 2); [shipLayer addAnimation:animationforKey:nil];
}
Copy the code
Animation group
CABasicAnimation and CAKeyframeAnimation only work on individual properties, whereas CAAnimationGroup can group these animations together
excessive
CATransition
CATransition is another subclass of CAAnimation. Different from other subclasses, CATransition has a Type and subtype to identify the transformation effect.
- (IBAction)switchImage
{
//set up crossfade transition
CATransition *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];
}
Copy the code
Ps: You might use it a lot when you’re doing transitions.
Unanimate during animation
- (void)removeAnimationForKey:(NSString *)key;
- (void)removeAllAnimations;
Copy the code
summary
Both basicAnimation and keyAnimation work with a single property (after all, both are subclasses of propertyAnimation), and an animationGroup can be composed of multiple animations.
Transitions are different from property animations and apply to scenarios such as view removal add and controller transitions.
Refer to the link
- IOS Core Animation