Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如果视图变得可设置动画,则有效调整CAShapeLayer的大小_Swift_Uibutton_Cashapelayer - Fatal编程技术网

Swift 如果视图变得可设置动画,则有效调整CAShapeLayer的大小

Swift 如果视图变得可设置动画,则有效调整CAShapeLayer的大小,swift,uibutton,cashapelayer,Swift,Uibutton,Cashapelayer,我有一个ui按钮,里面有一个CAShapeLayer。这是子类代码的一部分: private func addBeneathFill(){ didAddedBeneathFill = true let halfBeneathFill = UIBezierPath() halfBeneathFill.move(to: CGPoint(x: 0, y: frame.height / 2)) halfBeneathFill.addCurve(to: CGPoint(x:

我有一个
ui按钮
,里面有一个
CAShapeLayer
。这是子类代码的一部分:

private func addBeneathFill(){
    didAddedBeneathFill = true
    let halfBeneathFill = UIBezierPath()
    halfBeneathFill.move(to: CGPoint(x: 0, y: frame.height / 2))
    halfBeneathFill.addCurve(to: CGPoint(x: frame.width, y: frame.height / 2), controlPoint1: CGPoint(x: frame.width / 10, y: frame.height / 2 + frame.height / 10), controlPoint2: CGPoint(x: frame.width,      y: frame.height / 2 + frame.height / 10))
    halfBeneathFill.addLine(to: CGPoint(x: frame.width, y: frame.height))
    halfBeneathFill.addLine(to: CGPoint(x: 0, y: frame.height))
    halfBeneathFill.addLine(to: CGPoint(x: 0, y: frame.height / 2))
    let path = halfBeneathFill.cgPath
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path
    let fillColor = halfBeneathFillColor.cgColor
    shapeLayer.fillColor = fillColor
    shapeLayer.opacity = 0.3
    layer.insertSublayer(shapeLayer, at: 2)
}
只要
ui按钮
的大小不变,它就可以正常工作。当它的大小改变时,
CAShapeLayer
只会保持在原来的位置。当然,当
ui按钮的帧被更改时,我可以再次运行该方法,但在动画中它看起来很糟糕


如何正确地将
CAShapeLayer
的大小设置为始终为
UIButton
的当前大小?

您需要在按钮帧更改时重新计算并设置路径动画

private func animateShapeLayer() {
    let animation = CABasicAnimation(keyPath: "path")
    animation.fromValue = getBezierPath(forFrame: oldFrame)
    animation.toValue = getBezierPath(forFrame: newFrame)
    animation.duration = 1.0
    animation.fillMode = kCAFillModeForwards
    animation.isRemovedOnCompletion = false
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

    shapeLayer.add(animation, forKey: "path")
}
我已经测试过了,这里你有一个关于操场的要点和取得的成果: