本文介绍了在 Swift UIKit 中投掷纸/球效果动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 UIKit 中创建投掷纸/球效果动画的最佳方法是什么?

What would be the best way to create a throwing paper/ball effect animation in UIKit?

我需要的基本上是一个从point A开始到Point B结束的投掷效果.我不需要任何弹跳效果,这更像是让用户知道从 point APoint B 添加了一些东西的效果;类似于我们在照片中将照片添加到相册时获得的效果.

What I need is basically a throwing effect starting from point A and ending at Point B. I don't need any bouncing effects, this is more of an effect to let the user know that something was added from point A to Point B; similar to the effect we get when we add a photo to an album in Photos.

我一直在玩 animateKeyframes 但我不能完全得到我想要的效果,我没有得到曲线路径,我不能改变速度,所以效果看起来不真实.

I have been playing around with animateKeyframes but I cannot quite get the effect I want, I'm not getting the curve path and I cannot change the speed so the effect doesn't look realistic.

@IBAction func startAnimation(_ sender: Any) {
    UIView.animateKeyframes(withDuration: 1.0, delay: 0.0, options: [], animations: {
        var startTime = 0.0
        var viewScale:CGFloat = 1.0
        
        for i in 0..<5{
            UIView.addKeyframe(withRelativeStartTime: startTime, relativeDuration: 0.25, animations: {
                self.myView.center =  CGPoint(x: self.myView.center.x + 10, y: self.myView.center.y - 20)
                self.myView.transform = CGAffineTransform(scaleX: viewScale, y: viewScale)
            })
            startTime += 0.1
            viewScale += 0.01
        }
        
        for i in 0..<5{
            UIView.addKeyframe(withRelativeStartTime: startTime, relativeDuration: 0.25, animations: {
                self.myView.center =  CGPoint(x: self.myView.center.x + 10, y: self.myView.center.y + 5)
                self.myView.transform = CGAffineTransform(scaleX: viewScale, y: viewScale)
            })
            startTime += 0.1
            viewScale -= 0.01
        }
    }
    )
}

推荐答案

我不认为这个结果很糟糕:

I don't think this result is totally terrible:

这是一个关键帧动画,以三个点(起点、顶点、终点)和三次插值作为其计算模式.玩弄数字可能会有所改善.

That's a keyframe animation with three points (start, apex, end) and a cubic interpolation as its calculation mode. Playing around with the numbers might improve it a little.

如果您不喜欢那样,您可能更喜欢使用 UIKit 动态来获得更多基于物理的东西.(但我也用 UIKit 动态尝试过,但效果并不好.)

If you don't like that, you might prefer to use UIKit dynamics to get something more physics-based. (But I also tried it with UIKit dynamics and it wasn't better.)

这篇关于在 Swift UIKit 中投掷纸/球效果动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 23:56