2012-04-20 96 views
11

我有一个UIView阴影和UIImageView子视图。平滑地旋转和改变阴影UIView的大小

我想在iPad旋转时调整视图大小,并且我正在尝试在willRotateToInterfaceOrientation回调中执行此操作。

如果我以基本方式在UIView上设置阴影,旋转非常不连贯;所以我想从其他人那里得到一些关于如何设置阴影设置layer.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;