iOS Zooming max/min

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
I need to write a simple if statement to prevent my image from zooming in/out too much. At the moment, my code is as follows.

Code:
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;}
I want the image to behave like it does the in standard iPhone photos app, whereby the image stays within the limits of the iPhone screen when zooming out and doesn't zoom in a ridiculous amount.

Please help

Thanks
Rob
 

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
At the moment, it is zooming out too far. The image gets really small and a black background dominates the interface.
 

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
What kind of help are you expecting?

What is your code doing now? What are you expecting it to do?
I'm expecting someone kind to point out where I'm going wrong, as typical.

My code is not giving any errors. It's running fine.

I'm expecting the image to stay within the boundaries of the iPhone screen, and not zoom out too far, but it doesn't.
 
Last edited:

dejo

Moderator
Staff member
Sep 2, 2004
15,981
450
The Centennial State
I'm expecting the image to stay within the boundaries of the iPhone screen, and not zoom out too far, but it doesn't.
And what is your code doing to prevent this from happening? That is, what are you doing to restrict the zoom factor, or scale, as it were?
 

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
Adding zoom scale makes absolutely no difference at all. Even if I do it within attribute inspector of story board.

Code:
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1.0;
    self.scrollView.maximumZoomScale = 2.0;
    self.scrollView.minimumZoomScale = 0.5;
}
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
I assume that recognizer.view is not the same thing as self.scrollView. Setting the min/max zoom on the scroll view only affects how much you can zoom via the scroll view, which is not what you are doing.

You are telling recognizer.view exactly how much to zoom every time you set its transform scale to recognizer.transform. Why not just examine recognizer.scale before doing the transform to make sure that it is within your desired range?
 

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
It's funny you should post this now admanamal after I just found a lecture on scroll view.

I was under the impression that the only way to zoom, was to drag in a pinch gesture recogniser object in storyboard, onto my UIImageView and connect some code to it.

However, I've learned that there is no need to add gesture recognisers at all, which is counter intuitive! The iPhone will recognise a pinch gesture within scroll view!

I simply set my image view frame and scroll view content size ilke this
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageView.image = _currentContent.Image1;
    self.scrollView.contentSize = self.imageView.image.size;
    self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
}
and then I adjusted the min and max zoom scale in storyboard, and it works.

However, now I have a different problem, that is too related to this thread to be in a new thread.

When the image view presents itself, the content size of the scroll view dominates the whole screen and the image maintains its aspect but is partially off screen. Of course, I could pinch to get the whole image on screen but

1) I want the whole image on screen when the view controller loads
2) I want the image in the centre of the view and not at the upper left corner
3) I don't want the background white

I only know how to take the following action

1)?
2)?
3)set scrollview background to black in storyboard inspector

----------

I assume that recognizer.view is not the same thing as self.scrollView. Setting the min/max zoom on the scroll view only affects how much you can zoom via the scroll view, which is not what you are doing.

You are telling recognizer.view exactly how much to zoom every time you set its transform scale to recognizer.transform. Why not just examine recognizer.scale before doing the transform to make sure that it is within your desired range?
out of interest, how would you modify the following to incorporate the examination of recogniser.scale?

Code:
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1.0;
    self.scrollView.maximumZoomScale = 2.0;
    self.scrollView.minimumZoomScale = 0.5;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.