3

我有一个UIScrollView,里面有一个UIImageView与图像。我想能够缩放,仍然能够获得UIImage的坐标。我的图像具有比iPhone屏幕600x800大的坐标,并且使用当前的方法,我只获取iPhone屏幕上的坐标。我怎样才能得到被挖掘的UIImage上的实际位置的坐标?如何从缩放图像中的UITapGestureRecognizer获取UIImage的坐标?

这是我到目前为止有:

- (void) loadView { 

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)]; 
tapGesture.numberOfTapsRequired = 1; 
tapGesture.numberOfTouchesRequired = 1; 


UIImage *mapImage = [UIImage imageNamed:@"Map1.png"]; 
imageV = [[UIImageView alloc] initWithImage:mapImage]; 
//imageV.userInteractionEnabled = YES; 

//[imageV addGestureRecognizer:tapGesture]; 

//CGRect appFrame = [[UIScreen mainScreen] applicationFrame]; 
scrollV = [[UIScrollView alloc] initWithFrame:imageV.frame]; 
scrollV.contentSize = CGSizeMake(imageV.frame.size.width, imageV.frame.size.height); 

[scrollV addGestureRecognizer:tapGesture]; 

[scrollV addSubview:imageV]; 
scrollV.userInteractionEnabled = YES; 
scrollV.maximumZoomScale = 5; 
scrollV.minimumZoomScale = 0.5; 
scrollV.bounces = NO; 
scrollV.bouncesZoom = NO; 
scrollV.delegate = self; 

self.view = scrollV; 

} 



-(void)tapDetected:(UIGestureRecognizer*)recognizer{ 
NSLog(@"tap detected."); 
CGPoint point = [recognizer locationInView:nil]; 

NSLog(@"x = %f y = %f", point.x, point.y); 
} 

-(UIView*) viewForZoomingInScrollView:(UIScrollView *)scrollView { 
return [[self.view subviews] objectAtIndex:0]; 
} 

回答

11

我想通了,在uitapgesture识别器应该被添加到的UIImageView并在方法tapDetected被调用,以正确的方式来获得位置是给它正确的看法如下

CGPoint point = [recognizer locationInView:self.imageV]; 

然后就可以从图像中获得协调。

+0

感谢您为此转贴。您的更正帮助我快速从UITapGesture中获得我的观点。 – Nungster 2011-10-10 13:03:42

+0

多数民众赞成在正确的方式...我给你投票 – Sabby 2011-10-29 10:28:59

+0

P.sami如何处理在android中相同的问题? – 2012-01-12 14:31:40