1.通過(guò)CAKeyframeAnimation(關(guān)鍵幀動(dòng)畫(huà))來(lái)實(shí)現(xiàn)沿著沿著路徑移動(dòng)的動(dòng)畫(huà)
2.通過(guò)CABaseAnimation實(shí)現(xiàn)旋轉(zhuǎn)
3.在CAAnimationGroup中添加這兩個(gè)動(dòng)畫(huà)
如何使用CAKeyframeAnimation來(lái)實(shí)現(xiàn)沿著路徑的移動(dòng)
使用CGMutablePathRef來(lái)繪制路徑利用關(guān)鍵幀動(dòng)畫(huà)實(shí)現(xiàn)移動(dòng)也可以使用UIBezierPath來(lái)繪制貝塞爾曲線路徑(code)
CGPoint startPoint = self.leafImg.center;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, startPoint.x, startPoint.y);
CGPathAddQuadCurveToPoint(path, nil, 50, self.view.frame.size.height-100, startPoint.x + 360, startPoint.y + 550);///添加一個(gè)控制點(diǎn)和結(jié)束點(diǎn)
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path;
使用CABaseAnimation實(shí)現(xiàn)旋轉(zhuǎn)操作(code)
CABasicAnimation *aniBase = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
aniBase.fromValue = @0;
aniBase.toValue = @M_PI;
添加到CAAnimationGroup中牲平,然后把動(dòng)畫(huà)組加到layer中
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 4.f;
group.animations = @[aniBase,animation];
[self.leafImg.layer addAnimation:group forKey:nil];
二.使用CAKeyframeAnimation實(shí)現(xiàn)
[UIView animateKeyframesWithDuration:4 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{
CGPoint center = self.leafImg.center;
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.1 animations:^{
self.leafImg.center = (CGPoint){(center.x + 15), (center.y + 80)};
}];
///相對(duì)于4s的時(shí)間 0.1*4=0.4s的時(shí)候開(kāi)始
[UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.15 animations:^{
self.leafImg.center = (CGPoint){center.x + 45, center.y + 185};
}];
[UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.3 animations:^{
self.leafImg.center = (CGPoint){center.x + 90, center.y + 295};
}];
[UIView addKeyframeWithRelativeStartTime:0.55 relativeDuration:.3 animations:^{
self.leafImg.center = (CGPoint){center.x + 180, center.y + 375};
}];
[UIView addKeyframeWithRelativeStartTime:0.85 relativeDuration:.15 animations:^{
self.leafImg.center = (CGPoint){center.x + 360, center.y + 435};
}];
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:1 animations:^{
self.leafImg.transform = CGAffineTransformMakeRotation(M_PI);
}];
} completion:^(BOOL finished) {
/// if (self.timer) {
/// [self.timer invalidate];
///self.timer = nil;
}
}];