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!
self.view.layoutIfNeeded()before theUIView.animate()if you want a brutal change from 100 to 10, and then an animation from 10 to 500.