【问题标题】:iOS: verify if a point is inside a rectiOS:验证点是否在矩形内
【发布时间】:2011-12-23 16:12:09
【问题描述】:

有没有办法验证CGPoint 是否在特定的CGRect 内?

一个例子是: 我正在拖动一个UIImageView,我想验证它的中心点CGPoint 是否在另一个UIImageView 内。

【问题讨论】:

    标签: ios uiview collision-detection cgrect cgpoint


    【解决方案1】:

    斯威夫特 4

    let view = ...
    let point = ...
    view.bounds.contains(point)
    

    目标-C

    使用CGRectContainsPoint():

    bool CGRectContainsPoint(CGRect rect, CGPoint point);

    参数

    • rect 要检查的矩形。
    • point 要检查的点。 返回值 如果矩形不为空或不为空并且该点位于矩形内,则为 true;否则为假。

    如果一个点的坐标位于矩形内或在最小 X 或最小 Y 边上,则认为该点位于矩形内。

    【讨论】:

    【解决方案2】:

    在 Swift 中看起来像这样:

    let point = CGPointMake(20,20)
    let someFrame = CGRectMake(10,10,100,100)
    let isPointInFrame = CGRectContainsPoint(someFrame, point)
    

    Swift 3 版本:

    let point = CGPointMake(20,20)
    let someFrame = CGRectMake(10,10,100,100)
    let isPointInFrame = someFrame.contains(point)
    

    Link to documentation 。如果两者在同一个坐标系中,请记住检查是否包含,否则需要转换 (some example)

    【讨论】:

      【解决方案3】:

      在swift中你可以这样做:

      let isPointInFrame = frame.contains(point)
      

      “frame”是一个CGRect,“point”是一个CGPoint

      【讨论】:

        【解决方案4】:

        UIView 的 pointInside:withEvent: 可能是一个很好的解决方案。 将返回一个布尔值,指示给定的 CGPoint 是否在您正在使用的 UIView 实例中。 示例:

        UIView *aView = [UIView alloc]initWithFrame:CGRectMake(0,0,100,100);
        CGPoint aPoint = CGPointMake(5,5);
        BOOL isPointInsideView = [aView pointInside:aPoint withEvent:nil];
        

        【讨论】:

          【解决方案5】:

          在目标 c 中,您可以使用 CGRectContainsPoint(yourview.frame, touchpoint)

          -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
          UITouch* touch = [touches anyObject];
          CGPoint touchpoint = [touch locationInView:self.view];
          if( CGRectContainsPoint(yourview.frame, touchpoint) ) {
          
          }else{
          
          }}
          

          在 swift 3 yourview.frame.contains(touchpoint)

           override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
              let touch:UITouch = touches.first!
              let touchpoint:CGPoint = touch.location(in: self.view)
              if wheel.frame.contains(touchpoint)  {
          
              }else{
          
              }
          
          }
          

          【讨论】:

            【解决方案6】:

            就这么简单,你可以用下面的方法来做这种工作:-

            -(BOOL)isPoint:(CGPoint)point insideOfRect:(CGRect)rect
            {
                if ( CGRectContainsPoint(rect,point))
                    return  YES;// inside
                else
                    return  NO;// outside
            }
            

            在您的情况下,您可以在 about 方法中将 imagView.center 作为点传递,将另一个 imagView.frame 作为矩形传递。

            你也可以在下面的UITouch方法中使用这个方法:

            -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
            {
            }
            

            【讨论】:

              【解决方案7】:

              我开始学习如何使用 Swift 编写代码,并且也在尝试解决这个问题,这就是我在 Swift 的操场上想出的:

              // Code
              var x = 1
              var y = 2
              var lowX = 1
              var lowY = 1
              var highX = 3
              var highY = 3
              
              
              if (x, y) >= (lowX, lowY) && (x, y) <= (highX, highY ) {
                  print("inside")
              } else {
                  print("not inside")
              }
              

              它在里面打印

              【讨论】:

                【解决方案8】:
                - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
                            UITouch *touch = [[event allTouches] anyObject];
                            CGPoint touchLocation = [touch locationInView:self.view];
                            CGRect rect1 = CGRectMake(vwTable.frame.origin.x, 
                            vwTable.frame.origin.y, vwTable.frame.size.width, 
                            vwTable.frame.size.height);
                            if (CGRectContainsPoint(rect1,touchLocation))
                            NSLog(@"Inside");
                            else
                            NSLog(@"Outside");
                    }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-11-05
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-06-12
                  • 1970-01-01
                  • 2015-01-07
                  相关资源
                  最近更新 更多