【问题标题】:How to get the coordinates of UIImage from a UITapGestureRecognizer in a zoomed image?如何从缩放图像中的 UITapGestureRecognizer 获取 UIImage 的坐标?
【发布时间】:2011-07-18 18:38:00
【问题描述】:

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

【问题讨论】:

    标签: iphone uiscrollview uiimageview uiimage uigesturerecognizer


    【解决方案1】:

    我刚刚发现应该将 uitapgesture 识别器添加到 UIImageView 中,并且在调用方法 tapDetected 时,获取位置的正确方法是为其提供正确的视图,如下所示

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

    那么就可以从图像中获取坐标了。

    【讨论】:

    • 感谢转发。您的更正帮助我快速从 UITapGesture 中获得了我的观点。
    • P.sami 如何在 android 中处理同样的问题?
    • 对不起,我这么晚才回复 Arunkumar.p...我真的不知道 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多