
coreAnimation.png
- 基本動(dòng)畫CABasicAnimation,確定keyPath,animation的duration、fromValue以及toValue。
// KeyPath確定為position.x,
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
//動(dòng)畫執(zhí)行的duration
animation.duration = 0.8f;
//動(dòng)畫執(zhí)行的fromValue到tovalue
animation.fromValue = @(self.redView.center.x);
animation.toValue = @(self.redView.center.x+50);
//一個(gè)時(shí)間函數(shù),表示它是以怎么樣的時(shí)間運(yùn)行
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];
- 關(guān)鍵幀動(dòng)畫,動(dòng)畫可以按照一個(gè)指定的path執(zhí)行,或者按照一組特定的values去執(zhí)行。
//關(guān)鍵幀動(dòng)畫
-(void)keyFrameAnimation{
//動(dòng)畫執(zhí)行的路徑
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;
//動(dòng)畫執(zhí)行的keyPath
keyFrameAnmiation.keyPath = @"position";
keyFrameAnmiation.duration = 5;
keyFrameAnmiation.repeatCount = MAXFLOAT;
[self.redView.layer addAnimation:keyFrameAnmiation forKey:nil];
}
- 動(dòng)畫組,
//動(dòng)畫組
-(void)animationGroup{
//1.創(chuàng)建一個(gè)基礎(chǔ)動(dòng)畫
CABasicAnimation *basicAnimation = [CABasicAnimation animation];
basicAnimation.keyPath = @"transform.scale";
basicAnimation.fromValue = @1.0;
basicAnimation.toValue = @0.2;
//2.創(chuàng)建一個(gè)幀動(dòng)畫
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.創(chuàng)建一個(gè)動(dòng)畫組
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 5.0f;
group.autoreverses = YES;
group.animations = @[basicAnimation,keyFrameAnimation];
[self.redView.layer addAnimation:group forKey:nil];
}
-
轉(zhuǎn)場(chǎng)動(dòng)畫
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];
}
