iOS: Adding pinch-zoom to UIImageView

491 views
Skip to first unread message

Jon Sigman

unread,
Jan 16, 2012, 4:57:13 PM1/16/12
to Cocoa- Dev List
I have an iOS iPad app that uses a full screen UIImageView to display some png images. However, this arrangement doesn't allow the user to pinch-zoom the image. It seems to ignore gestures. I've tried embedding the UIImageView within a UIScrollView from IB (using Embed Object in Scroll View), and checking 'Multiple Touch' and 'User Interaction Enabled' for both views, but now I just get a white screen. What's the typical way to add pinch capability to a UIImageView?
_______________________________________________

Cocoa-dev mailing list (Coco...@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/cocoa-dev-garchive-98506%40googlegroups.com

This email sent to cocoa-dev-ga...@googlegroups.com

Fritz Anderson

unread,
Jan 17, 2012, 11:11:03 AM1/17/12
to Jon Sigman, Cocoa- Dev List
On 16 Jan 2012, at 3:57 PM, Jon Sigman wrote:

> What's the typical way to add pinch capability to a UIImageView?

Put it in a UIScrollView.

— F

Jon Sigman

unread,
Jan 17, 2012, 2:11:23 PM1/17/12
to Fritz Anderson, Cocoa- Dev List
Here's what I've tried:

IBOutlet UIScrollView *scrollView;  // created & placed on GUI with IB

@synthesize scrollView;             // hooked up from w/in IB

- (void)viewDidLoad {
    [super viewDidLoad];
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default.png"]];
    [scrollView addSubview:imageView];
    return;
}

The UIScrollView & UIImageView show up on the GUI, with the correct image, but gestures are ignored. What am I overlooking?


________________________________
From: Fritz Anderson <fri...@manoverboard.org>
To: Jon Sigman <rf_...@yahoo.com>
Cc: Cocoa- Dev List <coco...@lists.apple.com>
Sent: Tuesday, January 17, 2012 8:11 AM
Subject: Re: iOS: Adding pinch-zoom to UIImageView

Eric E. Dolecki

unread,
Jan 17, 2012, 2:18:50 PM1/17/12
to Jon Sigman, Cocoa- Dev List
Did you set the contentSize for the scroll view?

Google Voice: (508) 656-0622
Twitter: eric_dolecki XBoxLive: edolecki PSN: eric_dolecki
http://blog.ericd.net

> http://lists.apple.com/mailman/options/cocoa-dev/edolecki%40gmail.com
>
> This email sent to edol...@gmail.com

Jon Sigman

unread,
Jan 17, 2012, 2:30:20 PM1/17/12
to Eric E. Dolecki, Cocoa- Dev List
I just added this:
    [scrollView setContentSize:CGSizeMake(128.0,96.0)];
Now I can move the image about within the UIScrollView, but I can't pinch/zoom it.

________________________________
From: Eric E. Dolecki <edol...@gmail.com>
To: Jon Sigman <rf_...@yahoo.com>
Cc: Fritz Anderson <fri...@manoverboard.org>; Cocoa- Dev List <coco...@lists.apple.com>
Sent: Tuesday, January 17, 2012 11:18 AM

David Duncan

unread,
Jan 17, 2012, 2:34:15 PM1/17/12
to Jon Sigman, Cocoa- Dev List
You need to set the min/max magnification properties as well. You may also need to implement the delegate protocol to tell the scroll view which view to zoom as well (I'm almost certain this is necessary, but I haven't done this in a little bit).

> http://lists.apple.com/mailman/options/cocoa-dev/david.duncan%40apple.com
>
> This email sent to david....@apple.com

--
David Duncan

Fritz Anderson

unread,
Jan 17, 2012, 2:33:21 PM1/17/12
to Jon Sigman, Cocoa- Dev List
On 17 Jan 2012, at 1:30 PM, Jon Sigman wrote:

> I just added this:
> [scrollView setContentSize:CGSizeMake(128.0,96.0)];
> Now I can move the image about within the UIScrollView, but I can't pinch/zoom it.

There are excellent tutorials about this, and you would profit from a skimming of the UIScrollView class reference.

The properties you are looking for are .minimumZoomScale and .maximumZoomScale.

— F

_______________________________________________

Cocoa-dev mailing list (Coco...@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

Julius Oklamcak

unread,
Jan 17, 2012, 2:37:57 PM1/17/12
to Jon Sigman, Cocoa- Dev List
You need to implement the -viewForZoomingInScrollView: UIScrollViewDelegate
method and return the image view.

Please have a look at Apple's ScrollViewSuite sample code:

http://developer.apple.com/library/ios/#samplecode/ScrollViewSuite/Introduct
ion/Intro.html

> I just added this:
>    [scrollView setContentSize:CGSizeMake(128.0,96.0)];
> Now I can move the image about within the UIScrollView, but I can't
pinch/zoom it.

_______________________________________________

Jon Sigman

unread,
Jan 17, 2012, 2:45:51 PM1/17/12
to Julius Oklamcak, Cocoa- Dev List
Yep, specifying a delegate and implementing -viewForZoomingInScrollView: did the trick! (As a newb, I thought this functionality was innate in UIImageView, but...)

Thanks to everyone for your help!

________________________________
From: Julius Oklamcak <jul...@icodemonks.com>


To: 'Jon Sigman' <rf_...@yahoo.com>
Cc: 'Cocoa- Dev List' <coco...@lists.apple.com>

Sent: Tuesday, January 17, 2012 11:37 AM
Subject: RE: iOS: Adding pinch-zoom to UIImageView

Reply all
Reply to author
Forward
0 new messages