【问题标题】:How do i find out if a touch is over the frame of another shape?我如何确定触摸是否在另一个形状的框架上?
【发布时间】:2011-05-19 22:30:40
【问题描述】:

我知道如何触摸位置,所以现在我有一个触摸位置的 CGPoint。找出触摸是否在 UIView 上的最佳方法是什么?我知道的方法:

if touchpoint.x > frame.origin.x && touchpoint.x < frame.size.width + frame.origin.x

等等,但这是最好的方法吗?

【问题讨论】:

    标签: iphone objective-c view cgpoint


    【解决方案1】:

    如果您只是想知道一个点是否在视图的边界内,您可以使用pointInside:withEvent: 方法。

    CGPoint touchPoint = [theTouch locationInView:theView];
    // If the point was retrieved for a different view, it must be converted to the coordinate space of the destination view using convertPoint:fromView:
    if([theView pointInside:touchPoint withEvent:nil]) {
        NSLog(@"point inside");
    }
    

    【讨论】:

      【解决方案2】:

      Ugh 的回答涵盖了视图,我相信这是您想要的;如果你有一个任意的CGRect,你可以使用CGRectContainsPoint

      BOOL isInside = CGRectContainsPoint(myRect, touchPoint);
      

      【讨论】:

        【解决方案3】:

        是的,CGRectContainsPoint 会让你免于编写如此多的比较方程。

        【讨论】:

          猜你喜欢
          • 2014-12-16
          • 2015-06-29
          • 2016-04-24
          • 1970-01-01
          • 1970-01-01
          • 2021-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多