2012-04-20 10 views
11

Ho un UIView con un'ombra e una sottoview UIImageView.Ruota uniformemente e modifica la dimensione di UIView con l'ombra

Desidero ridimensionare la vista quando l'iPad viene ruotato e sto cercando di farlo nel callback willRotateToInterfaceOrientation.

Se si imposta l'ombra sul UIView nel modo base la rotazione è molto discontinua; quindi mi piacerebbe qualche suggerimento da parte di altri su come impostare l'ombra shadow layer.shadowPath.

Ho provato ad animare la modifica della dimensione del fotogramma utilizzando [UIView animateWithDuration:animations] e ho impostato il nuovo shadowPath nello stesso blocco, ma il percorso dell'ombra si aggancia alla nuova dimensione.

E se non cambio il shadowPath del livello nel blocco di animazioni, non cambia.

Da alcune ricerche che ho fatto, l'animazione delle modifiche alle proprietà dei livelli deve essere eseguita con un CABasicAnimation.

Quindi penso che la domanda potrebbe essere "come faccio ad animare le dimensioni di un fotogramma di UIView e il cambiamento di livello contemporaneamente?"

risposta

8

C'è un po 'più di codice di quello che si spera, ma qualcosa del genere dovrebbe funzionare.

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; 
Problemi correlati