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

    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()

ios - CABasicAnimation不在中心缩放-LMLPHP

最佳答案

问题是您没有正确地抓取框架。你说:

 let circleShape1 = CAShapeLayer()

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

那可能是正确的框架,也可能不是正确的框架。可能不是。但是您需要弄清楚。

然后,您需要根据形状图层的边界来固定Bezier路径的框架:
circleShape1.path = UIBezierPath(roundedRect: circleShape1.bounds // ...

10-04 16:20