Swift - 按下按钮时,如果动画已经在播放,则将其重置为开始
Posted
技术标签:
【中文标题】Swift - 按下按钮时,如果动画已经在播放,则将其重置为开始【英文标题】:Swift - When button pressed, reset the animation to beginning if it is already playing 【发布时间】:2019-02-07 03:44:42 【问题描述】:我在以编程方式创建的按钮上有一个径向动画,该动画在按下按钮时出现。
如果在动画运行时按下按钮,我希望动画在开始时重置并再次自动播放。
在“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 函数,我希望能够重置动画。
我尝试删除尽可能多的与代码无关的内容以尽可能地压缩它。希望这是有道理的。感谢您的任何建议
【问题讨论】:
【参考方案1】:经过几个小时的坚持,我想出了这个。我在自定义 CAshapelayer 中添加了一个重置布尔值。当按下按钮并且动画已经运行时,将调用 reset bool。
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")
【讨论】:
以上是关于Swift - 按下按钮时,如果动画已经在播放,则将其重置为开始的主要内容,如果未能解决你的问题,请参考以下文章