2010-09-09 8 views

risposta

2
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { 
    return ([self hitTest:point withEvent:nil] == yourSubclass) 
} 

Il metodo - (UIView *) hitTest: (CGPoint) il punto withEvent: (UIEvent *) evento restituisce il più lontano discendente del ricevitore nella gerarchia vista (compreso se stesso) che contiene un punto specificato. Quello che ho fatto è restituire il risultato del confronto della vista più lontana con la sottoview. Se la sottoview ha anche sottoview, questo potrebbe non funzionare per te. Quindi, ciò che si vorrebbe fare in questo caso è:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { 
    return ([[self hitTest:point withEvent:nil] isDescendantOfView:yourSubclass]) 
} 
+0

Ma questo metodo è una sottoclasse di UIView, quindi questo si blocca: – sol

+0

dispiace, io che ho bisogno di aggiungere una risposta per spiegare - vedi sotto – sol

0

Ma questo metodo è in una sottoclasse di UIView, quindi questo si blocca:

return ([[self hitTest:point withEvent:nil] isDescendantOfView:self]) 

Presumibilmente a causa di ricorsione infinita.

ho provato questo:

NSSet* touches = [event allTouches]; 
UITouch* touch = [touches anyObject]; 

return ([touch.view isDescendantOfView:self] || touch.view == self); 

ma restituisce sempre NO

+0

Questo lavoro ha fatto per me in una sottoclasse UIView: - (void) touchesBegan: (NSSet *) tocca withEvent: (UIEvent *) evento { \t \t UITouch * touch = [tocca anyObject]; \t if ([touch.view isDescendantOfView: mySubView]) { \t \t NSLog (@ "YUP"); \t} \t else { \t \t NSLog (@ "NOPE"); \t} } –

+0

questo non rileva i tocchi al di fuori del frame della vista - questa è la parte più importante che sto cercando. – sol

12

Prova che:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    return CGRectContainsPoint(subview.frame, point); 
} 

Se si desidera tornare YES se il tocco è all'interno della vista in cui si implementa questo metodo, utilizzare questo codice: (nel caso in cui si desidera aggiungere riconoscitori di gesti a una sottoview che si trova all'esterno dei limiti del contenitore)

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    if ([super pointInside:point withEvent:event]) 
    { 
     return YES; 
    } 
    else 
    { 
     return CGRectContainsPoint(subview.frame, point); 
    } 
} 
1

provate questo:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    if([touch.view isKindOfClass:[self class]]) { 
    return YES; 
    } 
    return NO; 
} 
0

versione Swift:

var yourSubview: UIView! 

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { 
    return subviewAtPoint(point) == yourSubview 
    } 

    private func subviewAtPoint(point: CGPoint) -> UIView? { 
    for subview in subviews { 
     let view = subview.hitTest(point, withEvent: nil) 
     if view != nil { 
     return view 
     } 
    } 
    return nil 
    } 
Problemi correlati