平滑旋转并使用阴影更改UIView的大小 [英] Smoothly rotate and change size of UIView with shadow

查看:76
本文介绍了平滑旋转并使用阴影更改UIView的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想在旋转iPad时调整视图的大小,并且尝试在 willRotateToInterfaceOrientation 回调中做到这一点.

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

如果我以基本方式在 UIView 上设置阴影,则旋转非常不稳定;所以我想从别人那里得到一些关于如何设置阴影设置layer.shadowPath的建议.

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.

我尝试使用 [UIView animateWithDuration:animations] 为帧大小变化设置动画效果,并在同一块中设置新的 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.

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

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

根据我所做的几次搜索,需要使用 CABasicAnimation 完成对图层属性的动画更改.

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

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

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

推荐答案

代码比人们希望的多得多,但是这样的代码应该可以工作.

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;

这篇关于平滑旋转并使用阴影更改UIView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆