平滑旋转并用阴影改变UIView的大小

Ben*_*mas 12 core-animation shadow calayer uiview ios

我有一个UIView阴影和UIImageView子视图.

我希望在iPad旋转时调整视图大小,我试图在willRotateToInterfaceOrientation回调中执行此操作.

如果我UIView在基本方式上设置阴影,则旋转非常不稳定; 所以我希望其他人提供一些关于如何设置阴影设置layer.shadowPath的建议.

我尝试使用相同的块并在同一个块中[UIView animateWithDuration:animations]设置new 来设置帧大小更改的动画shadowPath,但阴影路径会捕捉到新的大小.

如果我不在动画块中更改图层的shadowPath,它就不会改变.

从我完成的一些搜索中,需要使用a来完成对图层属性的更改动画CABasicAnimation.

所以我认为问题可能是"如何同时为UIView的帧大小和图层变化设置动画?"

sam*_*age 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;
Run Code Online (Sandbox Code Playgroud)