1

Looking at this question: Prevent UIScrollView from moving contents to top-left, i'm having the exact issue.

I'm using this tutorial: http://cocoadevblog.heroku.com/iphone-tutorial-uiimage-with-zooming-tapping-rotation

Back to the similar question, if i disable the UIScrollViewPanGestureRecognizer, i'm not able to pan the zoomed image anymore.

I have a UIImageView within a UIScrollView, and i want to be able to zoom and pan the image as well.

How can i do tho disable the contents moving to the top left corner when zooming in?

Community
  • 1
  • 1
Phillip
  • 4,276
  • 7
  • 42
  • 74

4 Answers4

2

Seems i solved tweaking my UiScrollView Autosizing and Origin in the Size inspector \ Attributes inspector. I unchecked Paging Enabled and the magic happened.

Phillip
  • 4,276
  • 7
  • 42
  • 74
1

Just in case anyone else comes here and none of the other answers seem to work ( which was my case ), what did the trick for me was setting the contentSize of the scrollView. Just set it to the size whatever subview you are zooming in on and it should work.

kensanwa
  • 11
  • 2
0

If I understand you right, you want to allowing scrolling only when theImageView is zoomed in, then a scrollView.zoomScale > 1. For my app requirement I am using this.

Add UIScrollView's delegate method as follows and check.

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    CGFloat offsetY = 0;
    if (aScrollView.zoomScale > 1)
        offsetY = aScrollView.contentOffset.y;

    [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, offsetY)];
}
  • Hi Tanya, you are answering a pretty old question that has already been correctly answered. Can you explain in your post why your answer is better or in some cases preferable over the others? – Noel Widmer May 29 '17 at 14:38
0

Make a subclass of UIScrollView, and add this method to it:

- (void)layoutSubviews {
    [super layoutSubviews];

    // center the image as it becomes smaller than the size of the screen
    CGSize boundsSize = self.bounds.size;

    //get the subView that is being zoomed
    UIView *subView = [self.delegate viewForZoomingInScrollView:self];

    if(subView)
    {
    CGRect frameToCenter = subView.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    subView.frame = frameToCenter;
    }

    else
        NSLog(@"No subView set for zooming in delegate");
}
GWed
  • 15,167
  • 5
  • 62
  • 99
  • Do you notice any changes? If you don't, then you have done something wrong as this should defo work. – GWed Feb 05 '12 at 16:58
  • Let's do it step by step. subView is another UIScrollView right? layoutSubviews i'm missing to understand what is it. – Phillip Feb 05 '12 at 17:04
  • So you have a scrollView. Replace that scrollView with a custom subclass of UIScrollView. In the implementation (.m file) of the custom class, add the above method. And no, subView isn't another scrollView, its the view you are applying the zooming to. You should already have implemented viewForZoomingInScrollView somewhere in your code, otherwise you would not be able to zoom anything! – GWed Feb 05 '12 at 17:44
  • 1
    Thanks for explaning. I'm new to this and i need to understand! By the way yes, i implemented that method! I will try – Phillip Feb 05 '12 at 18:08
  • No. You have a scrollview - get rid of it. Replace it with a class youve created that is a sub class of uiscrollview. Add the method above. . – GWed Feb 05 '12 at 20:23