gpt4 book ai didi

ios - CALayer 不透明度动画有效但边界动画无效

转载 作者:太空宇宙 更新时间:2023-11-03 17:54:05 25 4
gpt4 key购买 nike

我正在尝试为 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 方法中调用 disappear,动画效果很好。但是,调用 shrink 什么也不做。我做错了什么?

最佳答案

那是因为您正在使用 CAShapeLayerpath 独立于 bounds,因此您必须单独为其设置动画。

我可以使用粗略翻译成 Objective C 的代码版本重现您的问题。当我将 shr​​ink 方法更改为此时,它起作用了:

-(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;
}

从技术上讲,您甚至不必为边界设置动画。如果您在 circle 层上设置了不同的 backgroundColor,您可以进行一些试验,更容易地了解 boundspath 可以或多或少地独立设置和动画(例如,在您的原始代码中,您可以看到边界实际上是动画的,但路径保持不变)。

关于ios - CALayer 不透明度动画有效但边界动画无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17869180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com