Andy Lebowitz
Andy Lebowitz

Reputation: 1501

Issue flipping UIImageView

I have an image in my storyboard, and I want to flip it horizontally. By that, I mean I want to flip it over the X axis. I used the following code in a different action that seemed to flip the image, but without calling that action, but using the same code, I'm for some reason not able to flip the image. By the way, imageView (in the code below) is the name of the outlet for the UIImageView that I'm flipping, and there also aren't any error when I run the code. Idk why it won't flip. Any ideas? Is flipping a UIImageView, different from flipping a UIImage?? I don't know if theres even a difference there. Im kinda clueless.

Here's the code that I added into a button action:

self.imageView.transform = CGAffineTransformMakeScale(-1, 1);

I'm not sure if I need to switch around the numbers in that at all, I HAVE tried switching around the numbers, which ones are negative, or positive, but it is having no effect on my image.

Upvotes: 2

Views: 1081

Answers (3)

Caleb
Caleb

Reputation: 124997

I'm for some reason not able to flip the image. By the way, imageView (in the code below) is the name of the outlet for the UIImageView that I'm flipping, and there also aren't any error when I run the code. Idk why it won't flip.

Make sure that the view in your storyboard is actually connected to the imageView outlet of your view controller. If the outlet isn't connected, or is connected to the wrong view, then obviously the view will never be flipped, and no error will be generated -- it's perfectly fine to message nil in Objective-C.

Upvotes: 1

matt
matt

Reputation: 534925

Your code is perfectly right if what you want to do is flip horizontally. I used your code without any change, and as you can see, the image is reversed horizontally when I tap the button:

enter image description here

Upvotes: 0

Abhinav
Abhinav

Reputation: 38142

Is flipping a UIImageView, different from flipping a UIImage?

UIImageView is the container for the UIImage, so you flip container data and not the container.

Your code looks perfect to me. You may want to add an animation like this:

UIView.animateWithDuration(1.0, animations: { () -> Void in
    self.imageView.transform = CGAffineTransformMakeScale(-1, 1)
}, completion: nil)

Upvotes: 1

Related Questions