I'm really close to being able to scale a picture of a map programmatically with UIPinchGestureRecognizer
and a scale method. I've tried a scroll view like this: [mapScrollView addGestureRecognizer:pinchRecognizer];
, but of course that won't work because that will scale the entire view and the bounds. If I do [mapImageView addGestureRecognizer:pinchRecognizer]
nothing will happen.
How do I scale the image inside the scroll view? Even more, how do I set bounds of max and min scaling? Thanks
The code:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];
map_List = [[NSMutableArray alloc] init];
[map_List addObject:@"Pacific_Map_8bit.png"];
[map_List addObject:@"Atlantic_Map_8bit.png"];
CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);
mapScrollView = [[UIScrollView alloc] initWithFrame:mapScrollViewFrame];
mapScrollView.contentSize = CGSizeMake(2437, 1536);
UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
mapImageView = [[UIImageView alloc] initWithImage: mapImage];
mapScrollView.bounces = NO;
[mapImage release];
[mapScrollView addSubview:mapImageView];
[self addSubview:mapScrollView];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[mapImageView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
UIImage *footerMapIMG = [UIImage imageNamed:@"footer_map_alternate.png"];
UIImageView *footerMapView = [[UIImageView alloc] initWithImage:(UIImage *)footerMapIMG];
CGRect footerMapFrame = CGRectMake(0, 686, 213, 82);
footerMapView.frame = footerMapFrame;
[self addSubview:footerMapView];
footerMapView.image = footerMapIMG;
[footerMapView release];
CGRect backBTNFrame = CGRectMake(20, 714, 140, 52);
UIButton *MAP_backButton = [[UIButton alloc] init];
MAP_backButton.frame = backBTNFrame;
UIImage *MAP_backButtonIMG = [UIImage imageNamed:@"button_back.png"];
[MAP_backButton setImage:MAP_backButtonIMG forState:UIControlStateNormal];
MAP_backButton.backgroundColor = [UIColor clearColor];
[self addSubview:MAP_backButton];
[MAP_backButton release];
[MAP_backButton addTarget:del.switchVC
action:@selector(gotoMapAndListChooser)
forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void)scale:(id)sender {
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
/*
if(scale > 1){
开发者_StackOverflow中文版 scale = 1;
}
if(scale < .5){
scale = .5;
}
*/
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
I don't know for sure, and I don't want to test but try to add
mapImageView.userInteractionEnabled = YES;
Gestures don't work on imageviews without this setting.
EDIT: Maybe I've interpreted your question wrong and your scaling method gets called but doesn't work.
Btw, have you read about CATiledLayer?
There is also a great introduction in the WWDC10 Session Videos (Session 104 - Designing Apps with Scroll Views). You should get to that video through developer.apple.com
You should watch it, because a 2437x1536 image is quite big for the iphone. You will run into performance and/or memory problems sooner or later.
精彩评论