Throwing paper/ball effect animation in Swift UIKit

喜欢而已 提交于 2021-02-11 12:28:48

问题


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

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.

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
        }
    }
    )
}


回答1:


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.

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.)




回答2:


Here is the final code after implementing the three-point with the cubic interpolation animation that matt suggested.

@IBAction func startAnimation(_ sender: Any) {
    UIView.animateKeyframes(withDuration: 3.0, delay: 0.0, options: [.calculationModeCubic], animations: {
        // start
        UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1.0/5.0, animations: {
            self.myView.center =  CGPoint(x: self.pointA.center.x, y: self.pointA.center.y)
        })
        // apex
        UIView.addKeyframe(withRelativeStartTime: 1.0/5.0, relativeDuration: 1.0/5.0, animations: {
            self.myView.center =  CGPoint(x: self.pointB.center.x - 100, y: self.pointB.center.y - 100)
        })
        // end
        UIView.addKeyframe(withRelativeStartTime: 2.0/5.0, relativeDuration: 1.0/5.0, animations: {
            self.myView.center =  CGPoint(x: self.pointB.center.x, y: self.pointB.center.y)
        })
    }
    )
}


来源:https://stackoverflow.com/questions/64972363/throwing-paper-ball-effect-animation-in-swift-uikit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!