2012-04-20 13 views
11

Tengo un UIView con una sombra y una subvista UIImageView.Gire con suavidad y cambie el tamaño de UIView con la sombra

Quiero cambiar el tamaño de la vista cuando se gira el iPad y estoy tratando de hacer esto en la devolución de llamada willRotateToInterfaceOrientation.

Si configuro la sombra en el UIView de la manera básica, la rotación es muy agitada; entonces me gustaría algunas sugerencias de otros sobre cómo establecer la configuración de sombra layer.shadowPath.

He intentado animar el cambio de tamaño de fotograma usando [UIView animateWithDuration:animations] y estableciendo el nuevo shadowPath en el mismo bloque, pero la ruta de sombra se ajusta al nuevo tamaño.

Y si no cambio el shadowPath de la capa en el bloque de animaciones, no cambia.

De algunas de las búsquedas que he hecho, la animación de los cambios a las propiedades de la capa se debe hacer con un CABasicAnimation.

Así que creo que la pregunta puede ser "¿cómo puedo animar el tamaño de fotograma y el cambio de capa de UIView simultáneamente?"

Respuesta

8

Hay un poco más de código que uno esperaría, pero algo así debería funcionar.

CGFloat animationDuration = 5.0; 

    // Create the CABasicAnimation for the shadow 
    CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"]; 
    shadowAnimation.duration = animationDuration; 
    shadowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; // Match the easing of the UIView block animation 
    shadowAnimation.fromValue = (id)self.shadowedView.layer.shadowPath; 

    // Animate the frame change on the view 
    [UIView animateWithDuration:animationDuration 
         delay:0.0f 
         options:UIViewAnimationCurveEaseInOut 
        animations:^{ 
        self.shadowedView.frame = CGRectMake(self.shadowedView.frame.origin.x, 
                  self.shadowedView.frame.origin.y, 
                  self.shadowedView.frame.size.width * 2., 
                  self.shadowedView.frame.size.height * 2); 
        } completion:nil]; 

    // Set the toValue for the animation to the new frame of the view 
    shadowAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:self.shadowedView.bounds].CGPath; 

    // Add the shadow path animation 
    [self.shadowedView.layer addAnimation:shadowAnimation forKey:@"shadowPath"]; 

    // Set the new shadow path 
    self.shadowedView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.shadowedView.bounds].CGPath; 
Cuestiones relacionadas