【问题标题】:Swift - Update a UIButtton when CABasicAnimation is completeSwift - CABasicAnimation 完成时更新 UIButton
【发布时间】:2019-02-04 05:03:16
【问题描述】:

我想说的是当 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】:

    使用下面的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
                    }
                }
    
            }
    

    【讨论】:

    • 谢谢Torongo,我试过了,但是没用。动画像以前一样播放,animationDidStop 被调用,但其中的其余代码没有通过。
    • @STerrier 我已经更新了我的答案。请立即检查。别忘了给你的按钮添加标签。
    • 感谢托龙戈!使用 forKey 的好主意:“animationID”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多