[英]How to get the coordinates of UIImage from a UITapGestureRecognizer in a zoomed image?
我有一個UIScrollView,里面有一個帶圖像的UIImageView。 我希望能夠縮放並仍然能夠獲得UIImage的坐標。 我的圖像的坐標大於iPhone屏幕600x800,並且使用當前的方法,我只獲得iPhone屏幕上的坐標。 如何獲取已被點擊的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];
}
我只是想通了uitapgesture識別器應該添加到UIImageView中,當調用方法tapDetected時,獲取位置的正確方法是給它正確的視圖,如下所示
CGPoint point = [recognizer locationInView:self.imageV];
然后可以從圖像中獲得協調。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.