Reputation: 269
I have a long press gesture recognizer on an image. When someone clicks the image, I want a small animation to occur where the picture becomes a bit smaller for 0.1 seconds, and then it grows back to its original size. I am doing this to add the effect that they're pushing something down.
This is the function for my long press gesture
@objc func handleLongPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
pushBackImage()
}
}
I call the pushBackImage function which handles animating the image. This is the function down below.
func pushBackImage() {
UIView.animate(withDuration: 0.1, animations: {
self.contentImage.frame = CGRect(x: self.contentImage.frame.origin.x, y: self.contentImage.frame.origin.y, width: self.contentImage.frame.width / 1.25, height: self.contentImage.frame.height / 1.25)
}) { (completed) in
UIView.animate(withDuration: 0.1, animations: {
self.contentImage.frame = CGRect(x: self.contentImage.frame.origin.x, y: self.contentImage.frame.origin.y, width: self.contentImage.frame.width * 1.25, height: self.contentImage.frame.height * 1.25)
})
}
}
So this is what the code above does. It makes the picture smaller and then grows back to its original size. The problem is that it doesn't become smaller from the center of the image. I would like the image to scale smaller from the center of the image so that it can stay in the same position.
Upvotes: 0
Views: 755
Reputation: 1312
For scaling down something use , change x,y value based on your needs.
self.myView.transform = CGAffineTransform(scaleX: 0.25, y: 0.25)
scaling up use bigger than 1.
self.myView.transform = CGAffineTransform(scaleX: 1.25, y: 1.25)
Upvotes: 2