gpt4 book ai didi

ios - CABasicAnimation 不围绕中心缩放

转载 作者:可可西里 更新时间:2023-11-01 00:57:25 25 4
gpt4 key购买 nike

我想同时执行不透明度和缩放效果我的动画工作完美但它的位置不合适。我想在中心执行动画。这是我的代码。

    btn.backgroundColor = UIColor.yellowColor()
let stroke = UIColor(red:236.0/255, green:0.0/255, blue:140.0/255, alpha:0.8)
let pathFrame = CGRectMake(24, 13, btn.bounds.size.height/2, btn.bounds.size.height/2)

let circleShape1 = CAShapeLayer()
circleShape1.path = UIBezierPath(roundedRect: pathFrame, cornerRadius: btn.bounds.size.height/2).CGPath
circleShape1.position = CGPoint(x: 2, y: 2)
circleShape1.fillColor = stroke.CGColor
circleShape1.opacity = 0

btn.layer.addSublayer(circleShape1)

circleShape1.anchorPoint = CGPoint(x: 0.5, y: 0.5)

let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.fromValue = NSValue(CATransform3D: CATransform3DIdentity)
scaleAnimation.toValue = NSValue(CATransform3D: CATransform3DMakeScale(2.0, 2.0, 1))

let alphaAnimation = CABasicAnimation(keyPath: "opacity")
alphaAnimation.fromValue = 1
alphaAnimation.toValue = 0

CATransaction.begin()
let animation = CAAnimationGroup()
animation.animations = [scaleAnimation, alphaAnimation]
animation.duration = 1.5
animation.repeatCount = .infinity
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
circleShape1.addAnimation(animation, forKey:"Ripple")
CATransaction.commit()

enter image description here

最佳答案

问题是您没有正确处理框架。你说:

 let circleShape1 = CAShapeLayer()

但是你忘了给circleShape1一个框架!因此,它的大小为零,当你为它设置动画时会发生非常奇怪的事情。您在下一行的工作应该是为 circleShape1 分配一个框架。示例:

circleShape1.frame = pathFrame

这可能是也可能不是正确的框架;可能不是。但你需要弄清楚这一点。

然后,您需要根据形状层的边界修复贝塞尔路径的框架:

circleShape1.path = UIBezierPath(roundedRect: circleShape1.bounds // ...

关于ios - CABasicAnimation 不围绕中心缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42787285/

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