Zooming max/min

Discussion in 'iOS Programming' started by nashyo, Jan 25, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    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
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What kind of help are you expecting?

    What is your code doing now? What are you expecting it to do?
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    At the moment, it is zooming out too far. The image gets really small and a black background dominates the interface.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Well, that answers one of my three questions.
     
  5. nashyo, Jan 26, 2012
    Last edited: Jan 26, 2012

    nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #5
    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.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    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?
     
  7. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #7
    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;
    }
     
  8. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #8
    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?
     
  9. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #9
    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

    ----------

    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;
    }
     

Share This Page