ios - 从图像获取触摸坐标,同时保持平移/缩放功能

标签 ios uiimage touch uitapgesturerecognizer

我正在开发一个小应用程序,其中有一个 UIScrollView - 里面有一个图像,宽度是两倍,显示 map 。 UIScrollView是在Storyboard Editor中制作的,图像是动态加载的。

它可以部分工作 - 我可以平移、缩放和检测点击,但我需要它返回 x/y 坐标,反射(reflect)触摸位置。

以下是我的代码的相关部分:

    UIImage *worldMapImage = [UIImage imageNamed:@"worldmapBig.png"];
    imageView = [[UIImageView alloc] initWithImage:worldmapImage];
    imageView.userInteractionEnabled = YES;
    imageView.multipleTouchEnabled = YES;

    [_worldmap addSubview:imageView];
    _worldmap.userInteractionEnabled = YES;
    [_worldmap setContentSize:[worldMapImage size]];
    [_worldmap setMinimumZoomScale:.5];
    [_worldmap setMaximumZoomScale:1.0];
    [self performSelectorOnMainThread:@selector(didLoadImageInBackground:) withObject:imageView waitUntilDone:YES]; // After loading, initially zoom out

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

-(void)tapDetected:(UIGestureRecognizer*)recognizer{
    UIView *myView = [recognizer view]; <--
CGPoint touch = [recognizer locationInView:myView]; <--
NSLog(@"%f", touch.x);
}

我搜索了很多网页,解决了类似的问题 - 并且还尝试用按钮替换图像,使用 map 作为背景。它返回坐标,但我无法再平移/缩放;无法在同一代码中同时获得坐标和平移/缩放功能。

你能帮我一下吗?我已经被困了好几天了...

更新:事件处理程序已更改 - 现在可以工作了:-)

最佳答案

也许可以试试这个。

-[UIGestureRecognizer locationInView:]方法计算手势中所有触摸点的中心点。

关于ios - 从图像获取触摸坐标,同时保持平移/缩放功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19083942/

相关文章:

react-native - 如何在 React Native 中制作交互式绘图应用程序?

cocoa-touch - 关于如何学习 Cocoa Touch API 有什么建议吗?

ios - 未调用 Firebase 奖励视频委托(delegate)方法

ios - Swift - 动态更改 UIImageView 的图像

iphone - Monotouch 在 UIImage 上画线

wpf - WindowStyle None 和触摸输入

Android 如何使用 te LinearLayout 消耗所有的 touchevent

ios - NSURLSession 委托(delegate)与 completionHandler

javascript - 将对应用内购买的支持构建到 react native 代码中

iOS通过文件或nsdata将UIImage保存在plist中?