斯威夫特 - 当按下按钮,重置动画开始,如果它已经打

问题描述 投票:0回答:1

我有该按钮被按下时出现在编程方式创建的按钮径向动画。

如果按下按钮,而动画已经在运行,我希望动画在开始复位,再自动播放。

在“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({$0.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功能按钮时动画已经在播放。

我试图删除尽可能多的东西无关的代码,就像我能凝结成液体。希望这是有道理的。感谢您的任何意见

swift cashapelayer cabasicanimation caanimation
1个回答
0
投票

后粘贴了小时,我想出了这个。我在定制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")
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.