2010-11-30 11 views
7

Tengo una clase arrastrable que hereda UIImageView. El arrastre funciona bien cuando la vista no está animando. Pero al animarlo no responderá a los toques. Una vez que se completa la animación, el toque funciona nuevamente. Pero necesito pausar la animación en toques y reanudarla cuando termine el toque. Pasé todo el día investigando pero no pude descubrir el motivo.UIView touchesbegan no responde durante la animación

Aquí está mi código de animación.

[UIView animateWithDuration:5.0f 
    delay:0 
    options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) 
    animations:^{ 
    self.center = CGPointMake(160,240); 
    self.transform = CGAffineTransformIdentity; 
    } 
    completion:nil 
]; 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    NSLog(@"touch"); 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    startLocation = pt; 
    [self.layer removeAllAnimations]; 
    [[self superview] bringSubviewToFront:self]; 
} 

Respuesta

9

Eso es porque ios coloca su punto de vista que anima a la posición de destino, cuando la animación se inicia, pero lo dibuja en el camino. Entonces, si toca la vista mientras se mueve, en realidad toca en algún lugar fuera de su marco.

En el init de su vista de animación, establezca userInteractionEnabled en NO. Entonces los eventos táctiles son manejados por la supervista.

self.userInteractionEnabled = NO; 

En el método touchesBegan de su supervista, comprobación de la posición capa de presentación de su vista animación. Si coinciden con la posición táctil, redirija el mensaje toukesBegan a esa vista.

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint point = [[touches anyObject] locationInView:self.view]; 
    CGPoint presentationPosition = [[animatingView.layer presentationLayer] position]; 

    if (point.x > presentationPosition.x - 10 && point.x < presentationPosition.x + 10 
     && point.y > presentationPosition.y - 10 && point.y < presentationPosition.y + 10) { 
     [animatingView touchesBegan:touches withEvent:event]; 
    } 
} 
+0

Brilliant. ¡Gracias! – bzlm

+0

btw He tenido problemas con esto incluso cuando la posición no está animando, pero otra propiedad ('frame' en mi caso) es ... todavía obtienes toques que simplemente se tragan, aunque la vista sea' hitTest' y Los métodos 'pointInside' se llaman y regresan correctamente. Además, solo estoy haciendo 'if ([view pointInside: point withEvent: event])' - no hay necesidad de 10 puntos de slop – AlexChaffee

Cuestiones relacionadas