0

I have code to create animation:

var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() imageView = UIImageView(frame:CGRect(x: 10, y: 10, width: 10, height: 10)) imageView.image = UIImage(named:"2.png") UIView.animate(withDuration: 5) { self.imageView = UIImageView(frame:CGRect(x: 10, y: 10, width: 500, height: 500)) self.view.addSubview(self.imageView) self.view.layoutIfNeeded() } } 

But animation doesn't work. And imageView not showing.

8
  • so you want from size(100, 100) to go size(10,10) and then size(500,500) correct? first decrease size and then increase size Commented Jul 2, 2018 at 12:10
  • I edit my answer if you try the code with first duration 0 it will work Commented Jul 2, 2018 at 12:18
  • Call also self.view.layoutIfNeeded() before the UIView.animate() if you want a brutal change from 100 to 10, and then an animation from 10 to 500. Commented Jul 2, 2018 at 12:55
  • Updated my question. Commented Jul 2, 2018 at 17:56
  • Where you put those code? I mean did you put code on viewDidLoad method or anywhere else? Commented Jul 2, 2018 at 18:29

5 Answers 5

3

You should set the constraint's constant before the animation block and inside just call layoutIfNeeded

self.imageViewWidth.constant = 500 self.imageViewHeight.constant = 500 UIView.animate(withDuration: 10) { self.layoutIfNeeded() } 
Sign up to request clarification or add additional context in comments.

3 Comments

This is also a solution
Updated my question.
Updated my question.
2

Try this..

UIView.animate(withDuration: 10) { self.imageViewWidth.constant = 500 self.imageViewHeight.constant = 500 self.view.updateConstraintsIfNeeded() self.view.layoutIfNeeded() } 

To decrease and increase size you have to use the on completion handler

UIView.animate(withDuration: 2, animations: { self.imageViewWidth.constant = 10 self.imageViewHeight.constant = 10 self.view.layoutIfNeeded() }) { (true) in UIView.animate(withDuration: 10, animations: { self.imageViewWidth.constant = 500 self.imageViewHeight.constant = 500 self.view.layoutIfNeeded() }) } 

if you want the decrease to start immediately change the first withDuration to 0

2 Comments

Updated my question.
Updated my question.
1

There are things that makes sense within an animation block (here: UIView.animate). Creating a newer image view is not one of them. Neither is addSubView.

The problem is that you are completely reassigning to your imageview variable - creating a new one within animation block.

You should create your image view and add it to your main view completely before you execute UIView.animate.

Animation is changing the visible properties of your objects, not recreating them. If you want frame change, simply do the following within animation block:

self.imageView.frame = CGRect(x: 10, y: 10, width: 500, height: 500) 

You can also call layoutSubviews() or layoutIfNeeded() depending upon your constraint requirements, within UIView.animate.

Comments

1

Where is your problem

Problem 1

imageView = UIImageView(frame:CGRect(x: 10, y: 10, width: 10, height: 10)) imageView.image = UIImage(named:"2.png") 

Here, you created new instance of an image view which never has been added into viewcontroller's view stack. You should add this view into stack. These line of code doesn't have any effect on animation performance.

Problem 2

UIView.animate(withDuration: 5) { self.imageView = UIImageView(frame:CGRect(x: 10, y: 10, width: 500, height: 500)) self.view.addSubview(self.imageView) self.view.layoutIfNeeded() } } 

You initialized another imageview and added as a subview. This image view doesn't have any image or background color and as a result you do not see anything happending in the device screen. You should not do these stuff here. View.animate block does not need these stuff. Few things you should learn about animation. How animation works

Solution:
Try these lines of code instead.

 // initial frame var frame:CGRect = CGRect(x: 10, y: 10, width: 10, height: 10) // create imageview and add this into view stack self.imageView = UIImageView(frame:frame) self.imageView.image = UIImage(named:"2.png") self.view.addSubview(self.imageView) // set new height & width frame.size.width = 500; frame.size.height = 500; // animate view UIView.animate(withDuration: 5) { self.imageView.frame = frame; self.view.layoutIfNeeded() } 

Hope, it will work!

Comments

-1

if you want to animate constraints than you can use this

self.imageViewWidth.constant = 500 self.imageViewHeight.constant = 500; UIView.animate(withDuration: 10) { // if you are doing this in View controller then self.view.layoutSubviews() // if you are doing this in View then self.layoutSubviews() } 

2 Comments

Updated my question.
Updated my question.