1

我正在尝试为 CALayer 的边界设置动画,但它不起作用。这是代码:

class CircleView < UIIVew

  def initWithFrame(frame)
    super

    # determine the dimensions
    radius = (bounds.size.width < bounds.size.height ? bounds.size.width : bounds.size.height) / 2

    # create the circle layer
    @circle = CAShapeLayer.layer
    @circle.bounds = bounds
    @circle.path = UIBezierPath.bezierPathWithRoundedRect(bounds, cornerRadius: radius).CGPath

    @circle.fillColor = UIColor.blueColor.CGColor

    # add the circle to the view
    self.layer.addSublayer(@circle)

    shrink

    self
  end

  private

  # Applies the shrink animation to the provided circle layer.
  def shrink

    # determine the bounds
    old_bounds = @circle.bounds
    new_bounds = CGRectMake(old_bounds.size.width / 2, old_bounds.size.height / 2, 0, 0)

    # animate the shrinking
    animation = CABasicAnimation.animationWithKeyPath("bounds")
    animation.fromValue = NSValue.valueWithCGRect(old_bounds)
    animation.toValue = NSValue.valueWithCGRect(new_bounds)
    animation.duration = 3.0
    @circle.addAnimation(animation, forKey:"bounds")

    # set the bounds so the animation doesn't bounce back when it's complete
    @circle.bounds = new_bounds
  end

  # Applies the shrink animation to the provided circle layer.
  def disappear

    # animate the shirnk
    animation = CABasicAnimation.animationWithKeyPath("opacity")
    animation.fromValue = NSNumber.numberWithFloat(1.0)
    animation.toValue = NSNumber.numberWithFloat(0.0)
    animation.duration = 3.0
    @circle.addAnimation(animation, forKey:"opacity")

    # set the value so the animation doesn't bound back when it's completed
    @circle.opacity = 0.0
  end
end

如果我从方法中调用消失initWithFrame,则动画效果很好。然而,调用收缩什么也不做。我究竟做错了什么?

4

2 回答 2

6

那是因为您使用的是CAShapeLayer. path独立于,因此bounds您必须单独对其进行动画处理。

我可以使用大致翻译为目标 C 的代码版本来重现您的问题。当我将shrink方法更改为此时,它起作用了:

-(void)shrink
{
  CGRect old_bounds = self.circle.bounds;
  CGRect new_bounds = CGRectMake(old_bounds.size.width / 2, old_bounds.size.height / 2, 0, 0);

  // bounds
  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"bounds"];
  animation.fromValue = [NSValue valueWithCGRect: old_bounds];
  animation.toValue = [NSValue valueWithCGRect: new_bounds];
  animation.duration = 3.0;
  [self.circle addAnimation: animation forKey: @"bounds"];

  // path
  CGPathRef old_path = self.circle.path;
  CGPathRef new_path = [UIBezierPath bezierPathWithRoundedRect:new_bounds cornerRadius:0].CGPath;

  CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath: @"path"];
  pathAnimation.fromValue = (__bridge id)(old_path);
  pathAnimation.toValue = (__bridge id)(new_path);
  pathAnimation.duration = 3.0;
  [self.circle addAnimation: pathAnimation forKey: @"path"];

  //set the value so the animation doesn't bound back when it's completed
  self.circle.bounds = new_bounds;
  self.circle.path = new_path;
}

从技术上讲,您甚至可能不必为边界设置动画。如果您backgroundColorcircle图层上设置了 distinct ,您可以更轻松地进行一些实验,以了解如何或多或少独立地设置和动画(例如,在您的原始代码中,您可以看到边界实际上是动画的,但bounds路径path保持不变)。

于 2013-08-03T09:27:17.980 回答
-2

不了解 Ruby,我建议调整框架而不是边界。边界在 CALayers 自己的坐标系中,与图层本身在父级中的显示方式无关。

于 2013-07-31T21:35:53.207 回答