iOS:验证点是否在矩形内


Answers:


303

斯威夫特4

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

目标C

用途CGRectContainsPoint()

bool CGRectContainsPoint(CGRect rect, CGPoint point);

参量

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

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



是否可以检查CGPoint是否在在线上下文(CGContext)中?
阿维吉特·纳加尔(Avijit Nagare)

6
在Swift 3.0中使用,例如:rect =视图的框架,然后isContain = rect.contains(point)
nfinfu

38

在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)

链接到文档。请记住,如果两者都在同一个坐标系中,请检查是否包含该对象,否则需要进行转换(某些示例


12

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];

10

您可以像这样迅速地做到这一点:

let isPointInFrame = frame.contains(point)

“框架”是CGRect,“点”是CGPoint


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{

}}

快速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{

    }

}

3

非常简单,您可以使用以下方法来进行此类工作:-

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

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

您还可以在下面的UITouch方法中使用此方法:

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

0

我开始学习如何使用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")
}

在里面打印


0
- (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");
    }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.