【问题标题】:Smoothly rotate and change size of UIView with shadow用阴影平滑旋转和改变 UIView 的大小
【发布时间】:2012-05-01 15:26:00
【问题描述】:

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

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

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

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

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

根据我所做的一些搜索,需要使用CABasicAnimation 对图层属性进行动画更改。

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

【问题讨论】:

    标签: ios uiview core-animation calayer shadow


    【解决方案1】:

    代码比人们希望的要多,但这样的东西应该可以工作。

      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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      相关资源
      最近更新 更多