objective-c - 通过触摸检测 View 内的更近点

标签 objective-c ios

有一种方法,当用户在 View 外触摸时,应用程序会检测到该 View 内更近的点?我想像下图一样检测。

Example

编辑:

CGPoint touchPoint = [[touches anyObject] locationInView:self.view];

if (CGRectContainsPoint([_grayView frame], touchPoint)) {
    // The touch was inside the gray view
} else {
    // The touch was outside the view. Detects where's the closer CGPoint inside the gray view.
    // The detection must be related to the whole view (In the image example, the CGPoint returned would be related to the whole screen)
}

最佳答案

static float bound(float pt, float min, float max)
{
    if(pt < min) return min;
    if(pt > max) return max;
    return pt;
}

static CGPoint boundPoint(CGPoint touch, CGRect bounds)
{
    touch.x = bound(touch.x, bounds.origin.x, bounds.origin.x + bounds.size.width;
    touch.y = bound(touch.y, bounds.origin.y, bounds.origin.y + bounds.size.height;
    return touch;
}

关于objective-c - 通过触摸检测 View 内的更近点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13210670/

相关文章:

ios - 如何使用 Typhoon 为集成测试注入(inject)假的、 stub 的或模拟的依赖项

ios - 立即创建 iPhone 应用程序,并计划稍后支持 iPad

ios - UIbutton 自动调整大小

ios - UIPageViewController 防止在滚动时隐藏键盘

iphone - 将数据 UITableViewCell 传递到 UITableViewCell

objective-c - 如何在 ARC 下将 objective c 对象的地址获取到 void * volatile * 中?

ios - 基于ARC的项目是否可以分析?

ios - 防止一个闭包运行,直到另一个闭包完成

objective-c - 如何在 Objective-C 中使用类别访问@private 实例变量?

iphone - 有没有办法在一段时间后自动关闭警报 View ?