2011-11-07 24 views

Respuesta

239

Ver CGRectContainsPoint() en la documentación.

bool CGRectContainsPoint(CGRect rect, CGPoint point);

Parámetros

  • rect El rectángulo de examinar.
  • point El punto a examinar. Valor de retorno cierto si el rectángulo no es nulo o vacío y el punto se encuentra dentro del rectángulo; de lo contrario, falso.

Se considera un punto dentro del rectángulo si sus coordenadas se encuentran dentro del rectángulo o sobre el mínimo X o el mínimo del borde Y.

+13

El eslabón perdido;) https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html – ezekielDFM

+0

me ahorrar tiempo. Gracias – HamasN

+0

Gracias una tonelada ... –

10

de UIView pointInside: withEvent: podría ser una buena solución. Devolverá un valor booleano que indica si el CGPoint dado está en la instancia de UIView que está utilizando. Ejemplo:

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

es tan simple, puede utilizar siguiente método para hacer este tipo de trabajo: -

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

En su caso, puede pasar imagView.center como apuntar y hacer otra imagView.frame como rect en el método.

También puede utilizar este método en abajo UITouch Método:

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

En Swift que se vería así:

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

Swift 3 Versión:

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

Link to documentation. Por favor, recuerde que debe comprobar la contención si ambos están en el mismo sistema de coordenadas si no, las conversiones se requieren (some example)

+0

Gracias muy claro –

7

en Swift puede hacerlo de esta manera:

let isPointInFrame = frame.contains(point) 

"marco" es un CGRect y " punto" es un CGPoint

5

En Objective C puede utilizar CGRectContainsPoint (yourview.frame, punto de contacto)

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

}else{ 

}} 

En swift 3 yourview.frame.contiene (punto de contacto)

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{ 

    } 

} 
Cuestiones relacionadas