我要说的是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”是问题所在,但我什至不确定这是否是解决问题的最佳方法。任何帮助表示赞赏。
谢谢
答案 0 :(得分: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
}
}
}