My goal is to animate UIImageView transition from top left corner to the center of the view. It also has to change it width and height to screen width and change corner radius to zero.
How to modify following code so UIImageView changes its width and height:
private func imageAnimates() {
UIView.animate(
withDuration: 0.5,
delay: 0.0,
options: .curveLinear
) {
self.userImage.layer.cornerRadius = 0
self.userImage.center = CGPoint(x: self.view.center.x, y: self.view.center.y)
} completion: { finished in
print("Image animated")
}
}
UPD: Following an answer by vaibhav sharma, I updated my code to:
UIView.animate(
withDuration: 0.5,
delay: 0.0,
options: .curveLinear
) {
self.userImage.alpha = 1.0
self.userImage.layer.cornerRadius = 0
self.userImage.frame = CGRect(
x: (screenWidth - finalWidth) / 2,
y: (self.view.frame.size.height - finalHeight) / 2,
width: finalWidth,
height: finalHeight
)
} completion: { finished in
print("Image animated")
}
But the result I'm getting is not the one I expected. Here's a screen recording: https://sendvid.com/i1fxbf0y. It seems like the image now goes from tiny picture in the top left corner to its original frame and position. It's not even moving to the center.
You need to start out *simple -- work on only this image view animation until you get it working right. Then add it to your complex controller.
You cannot "mix-and-match" constraints and direct frame setting. UIKit will automatically reset the frame to match the constraints.
Instead, use only frame setting for your
userImageview ... you can use auto-layout constraints for everything else.So, take a look at this - tap anywhere to show-and-grow or shrink-and-hide the image view: