IOS: verify if a point is inside a rect

前端 未结 8 1441
夕颜
夕颜 2021-01-30 04:55

Is there a way to verify if a CGPoint is inside a specific CGRect.

An example would be: I\'m dragging a UIImageView and I want t

8条回答
  •  面向向阳花
    2021-01-30 05:20

    It is so simple,you can use following method to do this kind of work:-

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

    In your case,you can pass imagView.center as point and another imagView.frame as rect in about method.

    You can also use this method in bellow UITouch Method :

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

提交回复
热议问题