如何设置uiimage的特定区域来处理触摸手势?

How to set specific areas of uiimage to process touch gestures?

我正在制作一个照片搜索风格的应用程序。我有许多 X 射线,我需要设置 uiimage 的特定区域以将触摸事件处理为正确的,而其他的则为不正确的。

我知道我可以使用下面的代码获取图像视图中的点击位置,但我如何声明图像视图上的区域是正确的并将其与点击位置值进行比较?

 CGPoint tapLocation = [gesture locationInView:self.imagePlateA];

非常感谢任何帮助!

如果图像的正确位置是 CGRect 而不是点,您可以使用 CGRectContainsPoint()

CGGeometry Reference

因此您必须以编程方式创建 "regions" 并在获得该点后进行测试以查看它们是否在该区域中。例如:

//Get the tap location
CGPoint tapLocation = [gesture locationInView:self.imagePlateA];
if ([self checkIfTap:tapLocation inRegionWithCenter:CGPointMake(someX, someY) radius:radius]) {
    //YAY WE'RE WITHIN THE BOUNDS OF A CIRCLE AT POINT (someX, someY)
    //THAT HAS A RADIUS OF radius
}

和checkIfTap的方法:inRegionWithCenter:radius:可以这样定义:

- (BOOL)checkIfTap:(CGPoint)tapLocation inRegionWithCenter:(CGPoint)center radius:(CGFloat)radius {
    CGFloat dx = tapLocation.x - center.x;
    CGFloat dy = tapLocation.y - center.y;
    //Pythagorean theorem
    if (sqrt(dx * dx + dy * dy) < radius) {
        return YES;
    } else {
        return NO;
    }
}