Estoy usando una animación de rotación creada con CABasicAnimation
. Gira un UIView
durante 2 segundos. Pero necesito poder detenerlo cuando se toca el UIView
. Si elimino la animación, la vista está en la misma posición que antes de que comenzara la animación.Detener CABasicAnimation en el punto específico
Aquí está mi código de animación:
float duration = 2.0;
float rotationAngle = rotationDirection * ang * speed * duration;
//rotationAngle = 3*(2*M_PI);//(double)rotationAngle % (double)(2*M_PI) ;
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: rotationAngle ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.delegate = self;
[self.view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
¿Cómo puedo dejar de derecho de giro del UIView
's donde está, cuando se ha tocado? Sé cómo administrar la parte táctil, pero no puedo imaginar cómo detener la vista en el ángulo actual de la animación.
Solución: Resolví el problema obteniendo el ángulo de la capa de presentación, eliminando la animación y configurando la transformación de la vista. Aquí está el código:
[self.view.layer removeAllAnimations];
CALayer* presentLayer = self.view.layer.presentationLayer;
float currentAngle = [(NSNumber *)[presentLayer valueForKeyPath:@"transform.rotation.z"] floatValue];
self.view.transform = CGAffineTransformMakeRotation(currentAngle);
dónde se pone la solución anterior ?? Siempre obtengo el ángulo 0.0000 – Hisenberg