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.