IOS: verify if a point is inside a rect

前端 未结 8 1443
夕颜
夕颜 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:15

    In objective c you can use CGRectContainsPoint(yourview.frame, touchpoint)

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

    In swift 3 yourview.frame.contains(touchpoint)

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

提交回复
热议问题