I've been trying to animate a UIView with a spring animation using swift. I am able to achieve it when I use objective C, however I get an error in swift. This is the animation:
UIView.animateWithDuration(3,
usingSpringWithDamping: 0.3,
initialSpringVelocity: 3.0,
animations:{
viewToAnimate.frame.offset(dx: 0, dy: 100.0)},
completion: nil)
The compiler gives me an error saying
Could not find an overload for 'animateWithDuration' that accepts supplied arguments.
If I delete the "usingSpringWithDamping: 0.3, initialSpringVelocity: 3.0," it compiles and animated fine. How can I make the spring animation in swift?
To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.
The UIView class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content.
it queries the views objects for changed state and gets back the initial and target values and hence knows what properties to change and in what range to perform the changes. it calculates the intermediate frames, based on the duration and initial/target values, and fires the animation.
You're missing a parameter. The method also takes a delay as input.
UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 3.0, options: UIView.AnimationOptions.curveEaseInOut, animations: ({
// do stuff
}), completion: nil)
Try this:
UIView.animateWithDuration(0.7, delay: 0.0, usingSpringWithDamping: 0.5,
initialSpringVelocity: 0.5, options: [], animations:
{
self.yourView.transform = CGAffineTransformMakeScale(1, 1)
}, completion: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With