3

I have a UIImageView contained within a UIScrollView. The image is (usually) big, so the user can zoom it out in order to see the whole thing.

Upon zooming out, though, UIScrollView snaps the ImageView to the top-left of the scrollview. I want this to be positionable by the user, and haven't found a way to "turn it off" yet.

It's kinda like always allowing scrolling, rather then only allowing scrolling when the image is zoomed in. Maybe it's too major of a change?

Anyone know of a way? Originally, I was just going to create this functionality manually. But UIImageViews don't like to adjust to new sizes (I've tried about everything and can't get UIImageView to resize UNLESS I remove the picture from the imageView, change the frame, and re-add it).

etolstoy
  • 1,798
  • 21
  • 33
Chewmieser
  • 131
  • 1
  • 7

2 Answers2

5

I ended up disabling the UIScrollView's panGestureRecognizer and subbing in a custom one.

Here's a quick snippet on how to disable it:

// Disable our GestureRecognizer
for (UIGestureRecognizer *gesture in scrollView.gestureRecognizers){
    if ([[NSString stringWithFormat:@"%@",[gesture class]] isEqualToString:@"UIScrollViewPanGestureRecognizer"]){
        [gesture setEnabled:NO];
        break;
    }
}

A bit of a hack-job there, but it's due to the fact that UIScrollViews have changed the class of the GestureRecognizer to "UIScrollViewPanGestureRecognizer." The compiler will yell at you if you try to use that class (there's probably a better solution out there).

TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
Chewmieser
  • 131
  • 1
  • 7
  • 1
    `if ([gestureRecogniser isMemberOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")])` – Max May 24 '12 at 23:19
  • `if ([gestureRecogniser isKindOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")])` – nacho4d Jun 08 '12 at 04:02
  • 1
    There is a `panGestureRecognizer` property on `UIScrollView` as of iOS 5 that avoids this hack. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html#//apple_ref/occ/instp/UIScrollView/panGestureRecognizer – Robert Nov 07 '13 at 15:32
  • Any hints on the custom method you subbed in after disabling the gesture recognizer? I have no problems centering the image after zooming out. But I don't want to center it. I want to let the user position the image whether it's zoomed in or zoomed out. Any help appreciated. – crgt Nov 30 '13 at 04:40
1

If you locate the UIImageView at location 0,0 in the UIScrollView, then that's always going to be the upper-left. If you want it to be centered in the scrollview when it's smaller than the view, you need to position it there. Check whether its -size is bigger or smaller than the scrollview's. If it's smaller than the scrollview, set its -center to be the same as the scrollview's.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • This works perfectly for keeping the UIImageView at the center of the scrollView, but I was wondering if you knew a way to keep the UIImageView at the position it's dragged to, rather then snapping back to the center? – Chewmieser Mar 15 '11 at 20:50