如何从缩放图像中的UITapGestureRecognizer获取UIImage的坐标?

时间:2011-03-19 14:33:00

标签: iphone uiscrollview uiimageview uiimage uigesturerecognizer

我有一个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];
}

1 个答案:

答案 0 :(得分:11)

我只是想通知uitapgesture识别器应该添加到UIImageView中,当调用方法tapDetected时,获取位置的正确方法是给它正确的视图如下

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

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