http://www.devqinwei.com/2015/10/15/ios实现动画的几种常见方式/
1. 使用CALayer的基本关键帧动画CABasicAnimation和CAKeyframeAnimation
特点:可做3D动画
详细介绍可参看两个帖子:
http://blog.csdn.net/iosevanhuang/article/details/14488239
http://blog.csdn.net/wscqqlucy/article/details/8669636
注:( CGAffineTransform 和 CATransform3D 的比较 )
CGAffineTransform is used for 2-D manipulation of NSViews, UIViews, and other 2-D Core Graphics elements.
CATransform3D is a Core Animation structure that can do more complex 3-D manipulations of CALayers.( 搬运from stackOverFlow)
如:(CABasicAnimation)
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
animation.fromValue = @(0);
animation.toValue = @(-M_PI);
animation.repeatCount = 0;
animation.duration = 0.4;
[aView.layer addAnimation:animation forKey:@"rotation"];
CATransform3D transform = CATransform3DIdentity;
transform.m34 = 1.0 / 500.0;
aView.layer.transform = transform;
如:(CAKeyframeAnimation)
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"bounds"];
keyFrameAnimation.delegate = self;
keyFrameAnimation.duration = 1.0f;
keyFrameAnimation.beginTime = CACurrentMediaTime() + 1.0f;
NSValue *initialBounds = [NSValue valueWithCGRect: self.navigationController.view.layer.mask.bounds];
NSValue *secondBounds = [NSValue valueWithCGRect:CGRectMake(0.0f, 0.0f, self.navigationController.view.layer.mask.bounds.size.width - 40.0f , self.navigationController.view.layer.mask.bounds.size.height - 40.0f)];
NSValue *finalBounds = [NSValue valueWithCGRect:CGRectMake(0.0f, 0.0f, 2000.0f, 2000.0f)];
keyFrameAnimation.values = @[initialBounds,secondBounds,finalBounds];
keyFrameAnimation.keyTimes = @[@0, @0.5, @1];
keyFrameAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut] ];
keyFrameAnimation.removedOnCompletion = NO;
keyFrameAnimation.fillMode = kCAFillModeForwards;
[self.navigationController.view.layer.mask addAnimation:keyFrameAnimation forKey:@"maskAnimation"];
2. 使用CALayer的事务类CATransaction来执行
CATransaction动画包含显示动画和隐式动画两种。(显示能控制动画中更多方面)
隐式动画:
//设置变化动画过程是否显示,默认为YES不显示
[CATransaction setDisableActions:NO];
//设置圆角
self.layer.cornerRadius = (self.layer.cornerRadius == 0.0f) ? 30.0f : 0.0f;
//设置透明度
self.layer.opacity = (self.layer.opacity == 1.0f) ? 0.5f : 1.0f;
显示动画:
//修改执行时间
[CATransaction begin];
//显式事务默认开启动画效果,kCFBooleanTrue关闭
[CATransaction setValue:(id)kCFBooleanFalse
forKey:kCATransactionDisableActions];
//动画执行时间
[CATransaction setValue:[NSNumber numberWithFloat:5.0f] forKey:kCATransactionAnimationDuration];
//[CATransaction setAnimationDuration:[NSNumber numberWithFloat:5.0f]];
self.layer.cornerRadius = (self.layer.cornerRadius == 0.0f) ? 30.0f : 0.0f;
self.layer.opacity = (self.layer.opacity == 1.0f) ? 0.5f : 1.0f;
[CATransaction commit];
另外,CATransaction来可以用来设置嵌套动画,即先执行A动画,再执行B动画,再执行C动画…(这里不做举例)
(例子来源于:http://www.cnblogs.com/bandy/archive/2012/03/26/2418165.html)
3. 使用UIView关键帧动画animateKeyframesWithDuration
如:
[UIView animateKeyframesWithDuration:durationTime delay:0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0*frameDuration relativeDuration:1*frameDuration animations:^{
self.pickGradeView.transform = CGAffineTransformMakeScale(0.01, 0.01);
}];
[UIView addKeyframeWithRelativeStartTime:1*frameDuration relativeDuration:1*frameDuration animations:^{
self.pickGradeView.alpha = 1.0;
}];} completion:^(BOOL finished) {
}];
4.使用UIView animateWithDuration
如:
[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:0.5 options:0 animations:^{
self.shortNameLabel.transform = CGAffineTransformMakeScale(0.8, 0.8);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
self.shortNameLabel.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:nil];
}];
网友评论