可能重复:
How can I animate the movement of a view or image along a curved path?
我有一个iOS应用程序,我想动画落叶(或几个)。我的叶子图像在ImageView
中,我从文档中找到了一个简单的动画:
[UIView animateWithDuration:4.0f
delay:0
options:UIViewAnimationTransitionFlipFromLeft
animations:^(void)
{
leaf1ImageView.frame = CGRectMake(320, 480, leaf1ImageView.frame.size.width,leaf1ImageView.frame.size.height);
}
completion:NULL];
这将使叶子从其起始位置直线进入右下角。我如何设置动画来跟随路径或曲线如抛物线或正弦曲线,甚至可能旋转图像或视图?这会在动画块中完成吗?提前致谢!
答案 0 :(得分:10)
您可以使用CAKeyframeAnimation和CGPath执行此操作。代码看起来像这样:
CAKeyframeAnimation *leafAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
leafAnimation.duration = 10.0;
leafAnimation.path = /* your CGPathRef */;
[leaf1ImageView.layer addAnimation:leafAnimation forKey:@"leafAnimation"];
答案 1 :(得分:1)
你正在寻找的是沿着Bezier CGPath制作动画。有关详细信息,请参阅此问题:
How can I animate the movement of a view or image along a curved path?
在代码中创建bezier路径相当困难,为了更简单地创建贝塞尔曲线来制作动画,请查看问题的答案: