2011-11-07 17 views

risposta

239

Vedere CGRectContainsPoint() nella documentazione.

bool CGRectContainsPoint(CGRect rect, CGPoint point);

Parametri

  • rect Il rettangolo di esaminare.
  • point Il punto da esaminare. Valore restituito true se il rettangolo non è nullo o vuoto e il punto si trova all'interno del rettangolo; altrimenti, falso.

Un punto è considerato all'interno del rettangolo se le sue coordinate giacciono all'interno del rettangolo o sul X minimo o sul bordo Y minimo.

+13

L'anello mancante;) https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html – ezekielDFM

+0

mi risparmiare tempo. Grazie – HamasN

+0

Grazie mille ... –

10

UIView's pointInside: withEvent: potrebbe essere una buona soluzione. Restituisce un valore booleano che indica che il dato CGPoint è presente o meno nell'istanza di UIView che si sta utilizzando. Esempio:

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

è così semplice, è possibile utilizzare il metodo seguente per fare questo tipo di lavoro: -

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

Nel tuo caso, è possibile passare imagView.center come punto e altro imagView.frame come metodo perfetto.

È inoltre possibile utilizzare questo metodo in muggito UITouch Metodo:

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

In Swift che sarebbe simile a questa:

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

Swift 3 Versione:

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

Link to documentation. Ricordatevi di controllare il contenimento se entrambi sono nello stesso sistema di coordinate, se non allora conversioni sono tenuti (some example)

+0

Grazie molto chiaro –

7

a Swift si può fare in questo modo:

let isPointInFrame = frame.contains(point) 

"frame" è una CGRect e " punto" è una CGPoint

5

in Objective C è possibile utilizzare 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{ 

}} 

In swift 3 yourview.frame.contiene (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{ 

    } 

} 
Problemi correlati