Smoothly rotate and change size of UIView with shadow

拟墨画扇 提交于 2021-02-05 20:28:28

问题


I have a UIView with a shadow and a UIImageView subview.

I want to resize the view when the iPad is rotated and I'm trying to do this in the willRotateToInterfaceOrientation callback.

If I set the shadow on the UIView in the basic way the rotation is very choppy; so I'd like some suggestions from others on how to set the shadow setting layer.shadowPath.

I have tried animating the frame size change using [UIView animateWithDuration:animations] and setting the new shadowPath in the same block, but the shadow path snaps to the new size.

And if I don't change the layer's shadowPath in the animations block, it doesn't change.

From a few searches I've done, animating changes to layer properties need to be done with a CABasicAnimation.

So I think the question may be "how do I animate a UIView's frame size and layer change simultaneously?"


回答1:


There's a bit more code than one would hope but something like this should work.

  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;


来源:https://stackoverflow.com/questions/10239520/smoothly-rotate-and-change-size-of-uiview-with-shadow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!