基本动画(CABaseAnimation)
#import "ViewController.h"
@interface ViewController ()
{
UIImageView *_imgView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_imgView= [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
_imgView.backgroundColor = [UIColor lightGrayColor];
_imgView.image = [UIImage imageNamed:@"Appicon@3x.png"];
[self.view addSubview:_imgView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 获取动画对象
CABasicAnimation *animation = (CABasicAnimation *)[_imgView.layer animationForKey:@"rotationAnimation"];
if (animation == nil)
{
//旋转
[self rotationAnimation];
}else if(_imgView.layer.speed == 1)
{
//暂停
[self pauseAnimation];
}else if (_imgView.layer.speed == 0)
{
//开始
[self startAnimation];
}
}
- (void)rotationAnimation
{
//创建基本动画
CABasicAnimation *imganimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
imganimation.duration = 3;//设置时间
imganimation.repeatCount = MAXFLOAT;//设置重复次数
imganimation.fromValue = 0;//设置开始值
imganimation.toValue = @(M_PI*2);//设置结束值
[_imgView.layer addAnimation:imganimation forKey:@"rotationAnimation"];//将动画添加到图层上去
}
- (void)pauseAnimation
{
//获取当前的便宜时间
_imgView.layer.timeOffset = [_imgView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
_imgView.layer.speed = 0;
}
- (void)startAnimation
{
_imgView.layer.beginTime = CACurrentMediaTime() - (_imgView.layer.timeOffset);
_imgView.layer.timeOffset = 0;
_imgView.layer.speed = 1;
}
- (void)scaleAnimation
{
//获取动画对象
CABasicAnimation *imgAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
imgAnimation.duration = 1;
//设置初始值
imgAnimation.fromValue = @1;
//设置结束值
imgAnimation.toValue = @2;
//使动画结束在以动画的形式恢复
imgAnimation.autoreverses = YES;
//动画结束后停留在结束的状态
imgAnimation.removedOnCompletion = NO;
imgAnimation.fillMode = @"forwards";
[_imgView.layer addAnimation:imgAnimation forKey:@"animation"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
关键帧动画(CAKeyframeAnimation)
//让动画围绕触摸的地方画圆
- (void)movieWithArc:(CGPoint)touchPoint {
//以手指点击的地方作为圆心,150作为半径的圆运动
//创建动画对象
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置属性
keyFrameAnimation.duration = 2;
keyFrameAnimation.repeatCount = MAXFLOAT;
//设置运动的路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, touchPoint.x, touchPoint.y, 150, 0, M_PI*2, 1);
keyFrameAnimation.path = path;
//释放路径
CGPathRelease(path);
//添加动画
[_imgView.layer addAnimation:keyFrameAnimation forKey:nil];
}
动画组:(CAAnimationGroup)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self.view];
//创建动画组对象
CAAnimationGroup *group = [CAAnimationGroup animation];
//设置属性
CAAnimation *animation1 = [self movieWithArc:p];
CAAnimation *animation2 = [self getAnimation];
group.animations = @[animation1,animation2];
// group.repeatCount = MAXFLOAT;
group.duration = 3;
group.delegate = self;
[_imgView.layer addAnimation:group forKey:nil];
}
//圆周运动
- (CAKeyframeAnimation *)movieWithArc:(CGPoint)touchPoint {
//以手指点击的地方作为圆心,150作为半径的圆运动
//创建动画对象
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置属性
keyFrameAnimation.duration = 2;
keyFrameAnimation.repeatCount = MAXFLOAT;
//设置运动的路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, touchPoint.x, touchPoint.y, 150, 0, M_PI*2, 1);
keyFrameAnimation.path = path;
//释放路径
CGPathRelease(path);
return keyFrameAnimation;
}
//摇晃
- (CAKeyframeAnimation *)getAnimation {
//创建动画化对象
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
//设置属性
CGFloat num1 = M_PI_4/5.0;
CGFloat num2 = -M_PI_4/5.0;
animation.values = @[@(num1),@(num2),@(num1)];
animation.duration = 0.5;
animation.repeatCount = MAXFLOAT;
//加速方式
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
return animation;
}
CATransition转场动画
//创建动画对象
CATransition *transition = [[CATransition alloc] init];
//设置属性
transition.duration = 1;
//设置动画类型
提供了:
kCATransitionFade 渐变
kCATransitionMoveIn 进入
kCATransitionPush 推入
kCATransitionReveal 移除
5. rippleEffect 水滴效果 cameraIrisHollowClose相机关闭的效果
transition.type = @"cameraIrisHollowOpen";
//设置动画子类型
transition.subtype = kCATransitionFromRight;
//添加
[_imgView.layer addAnimation:transition forKey:nil];