斯威夫特 - 更新UIButtton CABasicAnimation完成时

问题描述 投票:0回答:1

我想无论是否选择一个按钮完成CAbasicanimation时说。当按下某些按钮的动画开始。

当按钮被点击我的按钮标记值存储到一个整数“currentSelectedMarker”。这并不工作,并提供所需的结果,但如果另一个按钮动画之前按未完成,它会更新点击按钮代替了原来的按钮,有动画的。

我知道这是因为“currentSelectedMarker”的值当按下任何按钮,但更新的会是什么,当动画完成更新正确的按钮的方式。下面是我使用动画的代码。

func AutoUpRadial(button: UIButton, height: Int, value: Int){

    let trackLayer = CAShapeLayer()

    let radius = height / 3
    let circularPath = UIBezierPath(arcCenter: button.center, radius: CGFloat(radius), startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
    trackLayer.path = circularPath.cgPath

    trackLayer.strokeColor = UIColor.black.cgColor
    trackLayer.opacity = 0.3
    trackLayer.fillColor = UIColor.clear.cgColor
    trackLayer.lineWidth = 5
    trackLayer.strokeEnd = 0

    mainScrollView.layer.addSublayer(trackLayer)

    autoUpFillRadial(value: value, tmpBtn: button, shape: trackLayer)
}

@objc private func autoUpFillRadial(value: Int, tmpBtn: UIButton, shape: CAShapeLayer){

    let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
    basicAnimation.toValue = 1
    basicAnimation.duration = CFTimeInterval(value)
    basicAnimation.fillMode = .forwards
    basicAnimation.isRemovedOnCompletion = true
    basicAnimation.delegate = self

    shape.add(basicAnimation, forKey: "basic")
}

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {

    if let tmpButton = self.view.viewWithTag(currentSelectedMarker) as? UIButton {
        tmpButton.isSelected = false
    }
}

该“currentSelectedMarker”是其中的问题是,据我所知,但我甚至不知道这是去了解这个问题的最好办法。任何帮助表示赞赏。

谢谢

ios swift uibutton cashapelayer cabasicanimation
1个回答
1
投票

添加您Button tag使用CABasicAnimation方法像下面setValue:forKey:和委托得到它。

@objc private func autoUpFillRadial(value: Int, tmpBtn: UIButton, shape: CAShapeLayer){

            let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
            basicAnimation.toValue = 1
            basicAnimation.duration = CFTimeInterval(value)
            basicAnimation.fillMode = .forwards
            basicAnimation.isRemovedOnCompletion = true
            basicAnimation.setValue(tmpBtn.tag, forKey: "animationID")
            basicAnimation.delegate = self

            shape.add(basicAnimation, forKey: "basic")
        }

        func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
            if let tag = anim.value(forKey: "animationID") as? Int {
                if let tmpButton = self.view.viewWithTag(tag) as? UIButton {
                    tmpButton.isSelected = false
                }
            }

        }
© www.soinside.com 2019 - 2024. All rights reserved.