2012-08-14 8 views
5

estoy seguro de que este problema se resuelve fácilmente sin embargo yo soy relativamente nuevo en el desarrollo de iOS. Estoy tratando de manejar los eventos táctiles que pasan a los niños que están más abajo en el orden de sorteo en un UIView. Por ejemplo -táctil para el próximo respondedor u otro sub-vista iOS

puedo crear extendió UIImageView para crear mi clase MoveableImage. Esta clase es básicamente UIImageView que implementa el touchesBegan, touchesEnded y touchesMoved-

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


[self showFrame]; 

//if multitouch dont move 
if([[event allTouches]count] > 1) 
{ 
    return; 
} 



    UITouch *touch = [[event touchesForView:self] anyObject ]; 

    // Animate the first touch 
    CGPoint colorPoint = [touch locationInView:self]; 

    CGPoint touchPoint = [touch locationInView:self.superview]; 


    //if color is alpha of 0 , they are touching the frame and bubble to next responder 
    UIColor *color = [self colorOfPoint:colorPoint]; 
    [color getRed:NULL green:NULL blue:NULL alpha:&touchBeganAlpha]; 
    NSLog(@"alpha : %f",touchBeganAlpha); 

    if(touchBeganAlpha > 0) 
    { 
      [self animateFirstTouchAtPoint:touchPoint]; 
    } 
    else { 
     [super.nextResponder touchesBegan:touches withEvent:event]; 
    } 



} 

Así, el resultado final es básicamente esto: Si ellos están tocando el marco de la imageView y no la imagen dentro de otra imagen que será debajo puede posiblemente responder. Vea esta imagen para un ejemplo.

Passing touch to lower view

Hasta ahora he intentado próxima respondedor sin embargo, que no resuelve el problema. ¡Cualquier ayuda sería muy apreciada!

Resolved- que se detuvo el control de la alfa en el touchesBegan y touchesMoved. Ovveriding pointInside permitió a UIView manejar eso por mí.

-(BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *) event 
{ 
    BOOL superResult = [super pointInside:point withEvent:event]; 
    if(!superResult) 
    { 
    return superResult; 
    } 

    if(CGPointEqualToPoint(point, self.previousTouchPoint)) 
    { 
    return self.previousTouchHitTestResponse; 
    }else{ 
    self.previousTouchPoint = point; 
    } 

    BOOL response = NO; 

    //if image is nil then return yes and fall back to super 
    if(self.image == nil) 
    { 
    response = YES; 
    } 

    response = [self isAlphaVisibleAtPoint:point]; 
    self.previousTouchHitTestResponse = response; 
    return response; 





} 

Respuesta

7

Puede anular lugar - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event método para su subclase de UIImageView. (Es un método de visualización que cada subclase puede anular)

La UIView utiliza este método en hitTest:withEvent: para determinar qué subvista debe recibir un evento táctil. Si pointInside: withEvent: devuelve SÍ, se recorre la jerarquía de la subvista; de lo contrario, se ignora su rama de la jerarquía de vistas.

Comprobar el código fuente en GitHub de OBShapedButton. Manejan el evento Tap solo para la parte opaca de un botón.

+0

Funcionó muy bien gracias hombre! – thebringking

Cuestiones relacionadas