Swift - 当 CABasicAnimation 完成时更新 UIButtton
Swift - Update a UIButtton when CABasicAnimation is complete
我想说的是当 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
}
}
}
我想说的是当 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
}
}
}