- 基本动画CABasicAnimation,确定keyPath,animation的duration、fromValue以及toValue。
// KeyPath确定为position.x,
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
//动画执行的duration
animation.duration = 0.8f;
//动画执行的fromValue到tovalue
animation.fromValue = @(self.redView.center.x);
animation.toValue = @(self.redView.center.x+50);
//一个时间函数,表示它是以怎么样的时间运行
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
animation.repeatCount = HUGE_VALF;
animation.repeatDuration = 2;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[self.redView.layer addAnimation:animation forKey:nil];
- 关键帧动画,动画可以按照一个指定的path执行,或者按照一组特定的values去执行。
//关键帧动画
-(void)keyFrameAnimation{
//动画执行的路径
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.redView.center radius:100 startAngle:0 endAngle:M_PI*2 clockwise:YES];
CAKeyframeAnimation *keyFrameAnmiation = [CAKeyframeAnimation animation];
keyFrameAnmiation.path = path.CGPath;
//动画执行的keyPath
keyFrameAnmiation.keyPath = @"position";
keyFrameAnmiation.duration = 5;
keyFrameAnmiation.repeatCount = MAXFLOAT;
[self.redView.layer addAnimation:keyFrameAnmiation forKey:nil];
}
- 动画组,
//动画组
-(void)animationGroup{
//1.创建一个基础动画
CABasicAnimation *basicAnimation = [CABasicAnimation animation];
basicAnimation.keyPath = @"transform.scale";
basicAnimation.fromValue = @1.0;
basicAnimation.toValue = @0.2;
//2.创建一个帧动画
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animation];
keyFrameAnimation.keyPath = @"position";
keyFrameAnimation.path = [UIBezierPath bezierPathWithArcCenter:self.redView.center radius:100 startAngle:0 endAngle:M_PI * 2 clockwise:YES].CGPath;
//3.创建一个动画组
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 5.0f;
group.autoreverses = YES;
group.animations = @[basicAnimation,keyFrameAnimation];
[self.redView.layer addAnimation:group forKey:nil];
}
-
转场动画
May-14-2020 18-52-28.gif
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100,100,200, 200)];
self.imgView.backgroundColor = UIColor.redColor;
self.imgView.image = [UIImage imageNamed:@"1.jpg"];
[self.view addSubview:self.imgView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
static int index = 1;
index++;
NSString *imgName = [NSString stringWithFormat:@"%d.jpg",index];
self.imgView.image = [UIImage imageNamed:imgName];
if (index == 4) {
index = 1;
}
CATransition *transition = [CATransition animation];
transition.type = @"pageCurl";
transition.subtype = kCATransitionFromLeft;
transition.duration = 1.0f;
[self.imgView.layer addAnimation:transition forKey:nil];
}
网友评论