gpt4 book ai didi

ios - Swift - 在 CABasicAnimation 完成时更新 UIButtton

转载 作者:行者123 更新时间:2023-11-28 13:51:38 27 4
gpt4 key购买 nike

当 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”是问题所在,但我什至不确定这是否是解决问题的最佳方法。任何帮助表示赞赏。

谢谢

最佳答案

使用 setValue:forKey: 方法将你的 Button tag 添加到 CABasicAnimation 中,并从代表。

@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
}
}

}

关于ios - Swift - 在 CABasicAnimation 完成时更新 UIButtton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54510373/

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