2011-03-19 122 views
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