21

Since upgrading my project to swift 3 my autolayout constraint animations aren't working; to be more specific, they're snapping to the new position rather than animating.

UIView.animate(withDuration: 0.1,
               delay: 0.1,
               options: UIViewAnimationOptions.curveEaseIn,
               animations: { () -> Void in
                   constraint.constant = ButtonAnimationValues.YPosition.DefaultOut()
                   self.layoutIfNeeded()
    }, completion: { (finished) -> Void in
    // ....
})

I know they added the UIViewPropertyAnimator class but am yet to try it.

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Alex Brown
  • 1,613
  • 1
  • 13
  • 23
  • 1
    I've been searching for a solution about this lately. Many are having the same issue, and I couldn't make it work even with the new UIViewPropertyAnimator. Maybe it is an unresolved bug in iOS 10. – diegotrevisan Sep 14 '16 at 13:39
  • Did you try setting the constant before the animate call? – lkraider Sep 15 '16 at 22:10
  • @lkraider Yes already tried that. – Alex Brown Sep 16 '16 at 07:48
  • Still no definite answer as to why this isn't working, the code works fine in the iOS9 sim. For now i've set `self.translatesAutoresizingMaskIntoConstraints = true` on my view and am animating the origin. – Alex Brown Sep 16 '16 at 08:48

6 Answers6

34

I had this problem too with the newest update to swift 3.

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.curveEaseIn,
           animations: { () -> Void in
               constraint.constant = ButtonAnimationValues.YPosition.DefaultOut()
               self.superview?.layoutIfNeeded()
}, completion: { (finished) -> Void in
// ....
})

It seems in the past they've been lenient about being able to just relayout the view you want to animate. But its in the documentation that you should really be calling layoutIfNeeded in the superview.

Enoch Ng
  • 364
  • 3
  • 3
  • Position is changing but its not animating. – Satyam Dec 11 '16 at 04:08
  • 3
    Please try this code, its work for me. UIView.animate(withDuration: 1, delay: 0, options: .curveEaseInOut , animations: { self.bottomViewTopConstraint.constant = 0.0 self.bottomView.superview?.layoutIfNeeded() }, completion: nil) – jbchitaliya Dec 30 '16 at 06:34
10

First set the new value for your constraint and then call animate.

self.YOUR_CONSTRAINT.constant = NEW_VALUE
UIView.animate(withDuration: 1.0) {
    self.view.layoutIfNeeded()
}
abdullahselek
  • 7,893
  • 3
  • 50
  • 40
9

Upgrade to swift 3.0

View right to left animation like apple default push animation

//intially set x = SCREEN_WIDTH
view.frame = CGRect(x: ScreenSize.SCREEN_WIDTH, y: ScreenSize.SCREEN_HEIGHT - heightTabBar , width: ScreenSize.SCREEN_WIDTH, height: heightTabBar)

UIView.animate(withDuration: 0.50, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: {
 //Set x position what ever you want
                        view.frame = CGRect(x: 0, y: ScreenSize.SCREEN_HEIGHT - heightTabBar , width: ScreenSize.SCREEN_WIDTH, height: heightTabBar)

                    }, completion: nil)
Saumil Shah
  • 2,299
  • 1
  • 22
  • 27
5

Swift 3.1 , Xcode 8.3.2

This code works well for me. Any changes on your view will animate in slow motion.

UIView.animate(withDuration: 3.0, animations: { // 3.0 are the seconds

// Write your code here for e.g. Increasing any Subviews height.

self.view.layoutIfNeeded()

})
Prashant Gaikwad
  • 3,493
  • 1
  • 24
  • 26
1

Swift 4, Xcode 10

Right to Left animation for search

//intially set x = Your_View_Width

    viewOfSearch.frame = CGRect(x: viewOfSearch.frame.size.width, y: 0 , width: viewOfSearch.frame.size.width, height: 50)

    UIView.animate(withDuration: 0.50, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0, options: [], animations: {
        //Set x position what ever you want
        self.viewOfSearch.frame = CGRect(x: 0, y: 0 , width: self.viewOfSearch.frame.size.width, height: 50)

    }, completion: nil)
Shamshad
  • 129
  • 1
  • 6
-1
func setView(view: UIView) {
        UIView.transition(with: view, duration: 1.0, options: .transitionFlipFromTop, animations: {
        })
    }
  • change the option part to apply new animations on that particular UIView.
Raghib Arshi
  • 717
  • 8
  • 12