Ios CALayer不透明动画工作,但边界动画不工作';T
我正在尝试为CALayer的边界设置动画,但它不起作用。代码如下:Ios CALayer不透明动画工作,但边界动画不工作';T,ios,ruby,cocoa-touch,core-animation,rubymotion,Ios,Ruby,Cocoa Touch,Core Animation,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 t
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
class CircleView
如果我从
initWithFrame
方法调用“消失”,动画工作正常。然而,打电话给心理医生没有任何作用。我做错了什么?不了解Ruby,我建议调整帧而不是边界。边界位于CALayers自己的坐标系中,与层本身在父层中的显示方式无关。不知道Ruby,我建议调整框架而不是边界。边界位于CALayers自己的坐标系中,与层本身在父层中的显示方式无关。这是因为您使用的是CAShapeLayer
。路径
独立于边界
,因此必须单独设置动画
我可以用大致翻译成目标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;
}
从技术上讲,您甚至不必设置边界的动画。如果您在
圆圈
层上设置了一个独特的背景色
,您可以进行一些实验,更轻松地了解如何或多或少独立地设置和设置边界
和路径
的动画(例如,在原始代码中,您可以看到边界实际上已设置动画,但路径保持不变).这是因为您使用的是CAShapeLayer
。路径
独立于边界
,因此必须单独设置动画
我可以用大致翻译成目标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;
}
从技术上讲,您甚至不必设置边界的动画。如果您在
圆圈
层上设置了一个独特的背景色
,您可以进行一些实验,更轻松地了解如何或多或少独立地设置和设置边界
和路径
的动画(例如,在原始代码中,您可以看到边界实际上已设置动画,但路径保持不变).Apple Docs:frame:此属性不可设置动画。通过设置边界和位置属性的动画,可以获得相同的结果。抱歉,您当前正在使用边界设置层相对于其父层的原点。这只会影响它的内容,而不会影响图层本身——而框架可以满足您的需要。您链接的苹果文档表示该位置可设置动画。该位置相对于锚点,默认情况下,锚点是边界的中心。要设置层大小的动画,请使用边界。Apple Docs:frame:此属性不可设置动画。通过设置边界和位置属性的动画,可以获得相同的结果。抱歉,您当前正在使用边界设置层相对于其父层的原点。这只会影响它的内容,而不会影响图层本身——而框架可以满足您的需要。您链接的苹果文档表示该位置可设置动画。该位置相对于锚点,默认情况下,锚点是边界的中心。要设置层大小的动画,请使用边界。那很有效!谢谢你的帮助!我可以让边界动画退出。这非常有效!谢谢你的帮助!我可以让边界动画离开。