2012-04-20 11 views
11

私はUIViewに影とUIImageViewのサブビューがあります。スムーズに回転し、影付きのUIViewのサイズを変更します。

iPadを回転させて、私がwillRotateToInterfaceOrientationコールバックでこれをしようとしているときに、ビューのサイズを変更したいとします。

UIViewのシャドウを基本的な方法で設定すると、回転が非常に不安定になります。ですから、shadow.shadowPathを設定する方法については、他の人からの提案が必要です。

[UIView animateWithDuration:animations]を使用してフレームサイズの変更をアニメートして、同じブロック内に新しいshadowPathを設定しようとしましたが、シャドウパスが新しいサイズにスナップします。

アニメーションブロック内のレイヤーのshadowPathを変更しないと、変更されません。

いくつかの検索から、レイヤプロパティの変更をアニメーション化するには、CABasicAnimationを使用する必要があります。

私は質問が "どのようにUIViewのフレームサイズとレイヤーの変更を同時にアニメーション化するのでしょうか?

答えて

8

希望よりも少しコードがありますが、これがうまくいくはずです。

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; 
関連する問題