我正在开发一个小应用程序,其中有一个 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/