8

Is it possible to make a UIView animation by changing constraints?

Basically, I want to animate myv (UIView) which has x, y, height and width constraints, using: UIView.animateWithDuration(1.5) {} by changing the old constraints.

Wyetro
  • 8,439
  • 9
  • 46
  • 64
ramsserio
  • 144
  • 1
  • 2
  • 11

1 Answers1

27

Yes this is possible. You can do something like this:

func AnimateHeight() {
    UIView.animateWithDuration(1.5, animations: {
         self.heightCons.constant = 250 // Some value
         self.view.layoutIfNeeded()    
    })
}

Where self.heightCons is an IBOutlet to the height constraint on the UIView.

If you don't use storyboards you can check out how to create layout constraints programmatically.

For info on Core Animation and Auto layout: Combining autolayout with core animation

Community
  • 1
  • 1
Wyetro
  • 8,439
  • 9
  • 46
  • 64
  • I don't use StoryBoard I prefer doing everything programmatically ! So I should create a var for constraints ? – ramsserio Jul 25 '16 at 19:05
  • Yeah check out this link http://stackoverflow.com/questions/31651022/how-to-create-layout-constraints-programmatically for how to do that. – Wyetro Jul 25 '16 at 19:07
  • What about Core animation can I Use CABasicAnimation with constraints ? – ramsserio Jul 25 '16 at 19:38
  • Seems like it: http://stackoverflow.com/questions/22761622/combining-autolayout-with-core-animation – Wyetro Jul 25 '16 at 19:39
  • there is no answer :/ I want the same thing – ramsserio Jul 25 '16 at 19:41
  • I'd recommend searching around for core animation and auto layout. I found this: https://www.invasivecode.com/weblog/auto-layout-and-core-animation-auto-layout-was/ – Wyetro Jul 25 '16 at 19:42
  • @ramsserio, you asked "can I Use CABasicAnimation with constraints?" The answer to that is no. CABasicAnimation and other subclasses of CAAnimation all operate on layers, and constraints operate on views. – Duncan C Jul 26 '16 at 00:34
  • So even if the view has some constraints I can use CAAnimation to animate layers ? – ramsserio Jul 26 '16 at 00:44