CALayer不透明度动画可以工作,但边界动画不起作用

Lan*_*opp 1 ruby cocoa-touch core-animation ios rubymotion

我试图动画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
Run Code Online (Sandbox Code Playgroud)

如果我从initWithFrame方法中调用消失,则动画可以正常工作.但是,调用shrink无效.我究竟做错了什么?

Jör*_*ich 6

那是因为你正在使用CAShapeLayer.该path是独立的bounds,所以你必须单独制作动画.

我可以使用大致翻译为Objective 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;
}
Run Code Online (Sandbox Code Playgroud)

从技术上讲,您甚至可能不需要为边界设置动画.如果设置了不同backgroundColor的上circle一层,你可以尝试一点点,更容易看到bounds并path可以设置和动画或多或少独立(例如,在原始的代码,你可以看到,边界实际上动画,但是路径保持不变).