CABasicAnimation:自己只有三个property fromValue toValue ByValue
当你创建一个 CABasicAnimation 时,你需要通过-setFromValue 和-setToValue 来指定一个开始值和结束值。 当你增加基础动画到层中的时候,它开始运行。当用属性做动画完成时,例如用位置属性做动画,层就会立刻 返回到它的初始位置
设定动画的属性。以下是属性及其对应的说明:{
duration 动画时长(秒为单位)
repeatCount 重复次数。永久重复的话设置为HUGE_VALF。
beginTime 指定动画开始时间。从开始指定延迟几秒执行的话,请设置为「CACurrentMediaTime() + 秒数」的形式。
timingFunction 设定动画的速度变化
autoreverses 动画结束时是否执行逆动画
}
设定动画的开始帧和结束帧
设定动画开始和结束帧时的状态。设定的值会变为KeyPath所指定的属性的值。
属性 说明
fromValue 开始值、 toValue 终了值(絶対値)、byValue 终了值(相对值)
我们可以通过animationWithKeyPath键值对的方式来改变动画
animationWithKeyPath的值{transform.scale = 比例轉換、transform.scale.x = 闊的比例轉換、transform.scale.y = 高的比例轉換、transform.rotation.z = 平面圖的旋轉、opacity = 透明度、margin、zPosition、backgroundColor 背景颜色、cornerRadius 圆角、borderWidth、bounds、contents、contentsRect、cornerRadius、frame、hidden、mask、masksToBounds、opacity、position、shadowColor、shadowOffset 、shadowOpacity 、shadowRadius}
实例:
#import "JumpToBaseController.h"
@interface JumpToBaseController ()
@property (nonatomic, strong) UIView *viewRect;
@end
@implementation JumpToBaseController
#pragma mark - LfileCycle
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *moveViewButton = [UIButton createButton:@"移动" target:self action:@selector(changeViewOne)];
moveViewButton.frame = CGRectMake(80, 60, 100, 40);
[self.view addSubview:moveViewButton];
UIButton *rotateButton = [UIButton createButton:@"旋转" target:self action:@selector(rotateButtonClick)];
rotateButton.frame = CGRectMake(180, 60, 100, 40);
[self.view addSubview:rotateButton];
UIButton *zoomButton = [UIButton createButton:@"缩放" target:self action:@selector(zoomButtonClick)];
zoomButton.frame = CGRectMake(80, 100, 100, 40);
[self.view addSubview:zoomButton];
UIButton *combinationButton = [UIButton createButton:@"组合动画" target:self action:@selector(combinationButtonClick)];
combinationButton.frame = CGRectMake(180, 100, 100, 40);
[self.view addSubview:combinationButton];
self.viewRect = [[UIView alloc] initWithFrame:CGRectMake(40, 250, 100, 100)];
self.viewRect.backgroundColor = [UIColor redColor];
[self.view addSubview:_viewRect];
}
#pragma mark - Touch Event
- (void)changeViewOne
{
// position:动画的移动效果keyPath
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
// 动画选项的设定
animation.duration = 3.f; // 动画持续时间
animation.repeatCount = 2.f; // 动画重复次数
// 起始帧和终了帧的设定
animation.fromValue = [NSValue valueWithCGPoint:self.viewRect.layer.position]; //起始帧5
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(320, 250)];// 终了帧
animation.autoreverses = YES;//动画结束时执行逆动画
[self.viewRect.layer addAnimation:animation forKey:nil];
}
- (void)rotateButtonClick {
// 对Y轴进行旋转(指定Z轴的话,就和UIView的动画一样绕中心旋转)
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
animation.duration = 3.f;
animation.repeatCount = 2.f;
animation.fromValue = [NSNumber numberWithFloat:0.0];// 开始角度
animation.toValue = [NSNumber numberWithInteger:2 * M_PI/3];// 结束角度
animation.autoreverses = YES;
[self.viewRect.layer addAnimation:animation forKey:nil];
// // 动画终了后不返回初始状态
// animation.removedOnCompletion = NO;
// animation.fillMode = kCAFillModeForwards;
}
- (void)zoomButtonClick {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 3.f;// 动画时间
animation.repeatCount = 3.f;// 动画次数
animation.fromValue = [NSNumber numberWithFloat:1.f]; // 开始时的倍率
animation.toValue = [NSNumber numberWithFloat:2.f]; // 结束时的倍率
animation.autoreverses = YES;// 动画结束时执行逆动画
[self.viewRect.layer addAnimation:animation forKey:nil];
}
- (void)combinationButtonClick {
/* 动画1(在X轴方向移动) */
CABasicAnimation *animationX = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
animationX.toValue = [NSNumber numberWithInteger:100];// 终点
/* 动画2(绕Z轴中心旋转) */
CABasicAnimation *animationZ = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animationZ.fromValue = [NSNumber numberWithFloat:0.0]; // 开始时的角度
animationZ.toValue = [NSNumber numberWithFloat:2 * M_PI]; // 结束时的角度
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];//动画数组
animationGroup.duration = 3.f;// 动画时间
animationGroup.repeatCount = 3;// 动画执行次数
animationGroup.autoreverses = YES;
animationGroup.animations = [NSArray arrayWithObjects:animationX,animationZ, nil];
[self.viewRect.layer addAnimation:animationGroup forKey:nil];
}