Swift - 按下按钮时,如果动画已经在播放,则将其重置为开始

Swift - When button pressed, reset the animation to beginning if it is already playing

我在以编程方式创建的按钮上有一个径向动画,按下按钮时会出现该动画。

如果在动画已经 运行 时按下按钮,我希望动画在开头重置并自动再次播放。

在 "animationDidStop" 函数中,我有在动画完成时应用的代码。我遇到的问题是按下按钮时动画已经 运行。我无法删除动画 运行 并开始一个新动画,因为它调用 "animationDidStop"。我还不想调用它。

我的想法是调整附加到径向形状的动画值,但这没有按计划进行。我知道可以访问动画值,但我会更改哪个,因为 "fromValue" 没有显示。我什至也不知道这是否真的有效或更好的方法。我现在被困了一段时间。

 var shapeLayerArray: [CustomShapeLayer] = []

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

    let trackLayer = CustomShapeLayer()

    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)


        shapeLayerArray.append(trackLayer)  
}

@objc private func autoUpFillRadial(value: Int, tmpBtn: CustomBtn, shape: CustomShapeLayer){

    let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
    basicAnimation.fromValue = 0
    basicAnimation.toValue = 1
    basicAnimation.duration = CFTimeInterval(value)
    basicAnimation.fillMode = .forwards
    basicAnimation.isRemovedOnCompletion = true
    basicAnimation.setValue(tmpBtn.UUIDtag, 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? UUID {
        if let tmpButton: CustomBtn = tmpButtonPicked(uuid: tag) {
                    tmpButton.isSelected = false
                    actionInput(sender: tmpButton, act: "up")
        }
    }
}

下面是按钮脚本

@IBAction func clipButton(sender: CustomBtn){

    currentSelectedMarkerUUID = sender.UUIDtag

    if let marker = markUpPlist.arrayObjects.filter({[=12=].UUIDpic == currentSelectedMarkerUUID}).first {

        if recording {
            if sender.isSelected {

                if !shapeLayerArray.isEmpty {
                    for shape in shapeLayerArray {
                        if shape.uuidTag == marker.UUIDpic {

                            print(shape.animation(forKey: "basic"))
                            // The line aboves shows the animation data

                            sender.isSelected = true
                        } else {
                            sender.isSelected = false
                            endClip(sender: sender)
                        }
                    }
                } else if shapeLayerArray.isEmpty {
                    sender.isSelected = false
                    endClip(sender: sender)
                }

            } else if !sender.isSelected {

                sender.isSelected = true
                startClip(sender: sender)


                if marker.timeAutoUp > 0 {

                    if !shapeLayerArray.isEmpty {
                        for shape in shapeLayerArray {
                            if shape.uuidTag == marker.UUIDpic {

                                //DO SOMETHING

                            } else {
                                self.AutoUpRadial(button: sender, height: marker.height, value: marker.timeAutoUp)
                            }
                        }
                    } else if shapeLayerArray.isEmpty {

                        self.AutoUpRadial(button: sender, height: marker.height, value: marker.timeAutoUp)
                    }
                }
            } else {
                sender.isSelected = false
            }
        }
    }
}

class CustomShapeLayer: CAShapeLayer {
var uuidTag: UUID?
}

所以回顾一下,如果按下按钮时动画已经在播放但不调用 animationDidStop 函数,我希望能够重置动画。

我试图删除尽可能多的与代码无关的东西,以尽可能多地压缩它。希望这是有道理的。感谢您的任何建议

经过几个小时的坚持,我想到了这个。我在自定义 CAshapelayer 中添加了一个重置​​布尔值。当按下按钮并且动画已经是 运行.

时,会调用重置布尔值
class CustomShapeLayer: CAShapeLayer {
var uuidTag: UUID?
vart reset = false
}

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

    if let tag = anim.value(forKey: "animationID") as? UUID {
        if let tmpButton: CustomBtn = tmpButtonPicked(uuid: tag) {

            if !shapeLayerArray.isEmpty {
                for shape in shapeLayerArray {
                    if shape.uuidTag == tag{
                        if shape.reset {
                            shape.reset = false
                        } else {
                            tmpButton.isSelected = false
                            actionInput(sender: tmpButton, act: "up")
                        }
                    } else {
                            tmpButton.isSelected = false
                            actionInput(sender: tmpButton, act: "up")
                    }
                }
            } else if shapeLayerArray.isEmpty {

                    tmpButton.isSelected = false
                    actionInput(sender: tmpButton, act: "up")
            }
        }
    }
}