Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-18-2010, 12:30 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 13
Missing.Matter is on a distinguished road
Default UIScrollView scale subviews on zoom

Hi all,

I'm making a board game using a UIScrollView to display the board, and I'm trying to figure out how to zoom the game pieces when I zoom in on the board with a pinch gesture.

Currently, zooming the board works, and moving the pieces works. However when I zoom in on the board, the pieces maintain their frame position and size when I zoom the board.

Is there an easy way to implement this behavior, or am I going to have to manually write a method to transform each piece's frame on a zoom gesture? If that's the case, where do I call this method from?

Here is my code as it currently stands:

Code:
#import "RootViewController.h"

#define ZOOM_VIEW_TAG 100

@implementation RootViewController

@synthesize imageScrollView;

- (void)viewDidLoad {
    [super viewDidLoad];
	
	CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];

	//Initialize scrollview
	imageScrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
	[imageScrollView setDelegate:self];
	
	//Initialize Board
	UIImageView *imageBoardView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Board.png"]];
	[imageBoardView setTag:ZOOM_VIEW_TAG];
	[imageScrollView setContentSize:[imageBoardView frame].size];
	[imageScrollView addSubview:imageBoardView];
	[imageBoardView release];
	
	//Initialize Tokens
	TokenImageView *tokenImageView = [[TokenImageView alloc] initWithImage:[UIImage imageNamed:@"Token.png"]];
	[tokenImageView setDelegate:self];
	[imageScrollView addSubview:token];
	[tokenImageView release];
	
	[[self view] addSubview:imageScrollView];
	
	//calculate minimum scale to perfectly fit image width, and begin at that scale
        float minimumScale = [imageScrollView frame].size.width  / [imageBoardView frame].size.width;
        [imageScrollView setMinimumZoomScale:minimumScale];
        [imageScrollView setZoomScale:minimumScale];
	
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [imageScrollView viewWithTag:ZOOM_VIEW_TAG];
}
The code initializes a scrollview, adds the board, adds a token, and zooms out to fit the board. Is there some property I'm overlooking or some other trick which would accomplish my goal?

Any help is appreciated. Thanks.

Last edited by Missing.Matter; 06-18-2010 at 09:54 AM.
Missing.Matter is offline   Reply With Quote
Old 06-18-2010, 01:15 AM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 651
kapps11 is on a distinguished road
Default

hmmm. well the problem is that at the end, you are only returning the background view as the zoom view, so that is why the tokens dont change at all. But im not sure if it lets you return multiple views...
kapps11 is offline   Reply With Quote
Old 06-18-2010, 09:27 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 13
Missing.Matter is on a distinguished road
Default

Quote:
Originally Posted by kapps11 View Post
hmmm. well the problem is that at the end, you are only returning the background view as the zoom view, so that is why the tokens dont change at all. But im not sure if it lets you return multiple views...
I figured there might be something I could do here, like setting both views to have the same tag, but that doesn't do anything.

I've found though, that adding tokenImageView to boardImageView instead of imageScrollView produces the desired effect; however, it breaks dragging the board, so if I can fix that I'm good to go.

In another application I'm using both of these while preserving zooming, but I'm not using any gesture recognizers (I'm using them here to recognize tap to zoom). Could that be the problem?

Last edited by Missing.Matter; 06-18-2010 at 10:18 AM.
Missing.Matter is offline   Reply With Quote
Old 06-18-2010, 10:16 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 13
Missing.Matter is on a distinguished road
Default

Okay, in viewDidLoad I've added the following lines:

Code:
	[imageScrollView setDelaysContentTouches:NO];
	[imageScrollView setCanCancelContentTouches: NO];

	

	[imageBoardView addSubview:imageTokenView];
The last one of course replacing [imageScrollView addSubView:imageTokenView];

Adding setCanCancelContentTouches and setDelaysContentTouches fixes some problems with dragging tokens. Adding one or the other is fine, but adding them both breaks scrolling (but not zooming).

Any ideas? setDelaysContentTouches:NO is not necessary, but it gets rid of the delay between touching a token, and recognizing the touch. Is there a way to set that delay to be shorter? Or am I going to have to handle scrolling with a pan gesture recognizer?

Edit: I found the combination that breaks scrolling

Code:
[imageScrollView setDelaysContentTouches:NO];
[imageScrollView setCanCancelContentTouches:NO];
[boardImageView setUserInteractionEnabled:YES];
So the entire board is responding to touches and the scroll view can't cancel out of them. setUserInteractionEnabled is necessary to interact with the tokenImageView subviews. But I don't care about touches on the board itself, just touches on the tokens. So now I need to figure out how to ignore touches on the board but not on a token.

Last edited by Missing.Matter; 06-18-2010 at 10:42 AM.
Missing.Matter is offline   Reply With Quote
Old 06-19-2010, 08:01 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 13
Missing.Matter is on a distinguished road
Default

Any thoughts on this?
Missing.Matter is offline   Reply With Quote
Old 07-19-2010, 12:32 PM   #6 (permalink)
Registered Member
 
Join Date: Apr 2010
Location: Belo Horizonte, MG
Posts: 8
Paulo Khouri is on a distinguished road
Default

I have the same problem when I zooming the scroll's of the scrollView stops, but I'm not scrolling an image, I'm scrolling and zooming a PDF file.
Paulo Khouri is offline   Reply With Quote
Reply

Bookmarks

Tags
scale, uiscrollview, zoom

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 334
9 members and 325 guests
Absentia, Chickenrig, Domele, fiftysixty, givensur, heshiming, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:16 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0