Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating a View in Swift

Tags:

swift

xcode6

ios8

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?

like image 307
user3724253 Avatar asked Jun 11 '14 00:06

user3724253


People also ask

How do you animate a view in IOS 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.

What is UIView in Swift?

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.

How does UIView animate work?

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.


2 Answers

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)
like image 121
Mick MacCallum Avatar answered Oct 20 '22 17:10

Mick MacCallum


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)
like image 43
Massimo Polimeni Avatar answered Oct 20 '22 16:10

Massimo Polimeni