0

I'm animating show and hide of my FAB button, however I want to prevent showing FAB if it is already shown. How to read current layer scale?

func hideFab(){
    let materialCurve = MDCAnimationTimingFunction.deceleration
    let timingFunction = CAMediaTimingFunction.mdc_function(withType: materialCurve)
    let animation = CABasicAnimation(keyPath:"transform.scale.xy")
    animation.timingFunction = timingFunction
    animation.fromValue = 1
    animation.toValue = 0
    animation.duration = 0.5
    animation.isRemovedOnCompletion = false
    animation.fillMode = .forwards
    fab.layer.playAnimation({ (layer) -> CAAnimation in
        animation
    }, key: animation.keyPath!)
}

func showFab(){
    let materialCurve = MDCAnimationTimingFunction.deceleration
    let timingFunction = CAMediaTimingFunction.mdc_function(withType: materialCurve)
    let animation = CABasicAnimation(keyPath:"transform.scale.xy")
    animation.timingFunction = timingFunction
    animation.fromValue = 0
    animation.toValue = 1
    animation.duration = 0.5
    animation.isRemovedOnCompletion = false
    animation.fillMode = .forwards
    fab.layer.playAnimation({ (layer) -> CAAnimation in
        animation
    }, key: animation.keyPath!)
}

1 Answers1

0

With this let currentScale = fab.layer.presentation()?.value(forKeyPath: "transform.scale.xy") ?? 0.0 you should be able to get the presentation scale value.

But I'd try to instead check if I showed or not the FAB with a tag:

let hiddenFABTag = 99
let shownFABTag = 100
func hideFAB(){
   if fab.tag == shownFABTag {
     ...
     ...
     fab.tag = hiddenFABTag
   }
}

func showFAB(){
   if fab.tag == hiddenFABTag {
     ...
     ...
     fab.tag = shownFABTag
   }
}

The first time you call it, either in viewDidLoad() or somewhere, you might want to give it a default tag value, if you know if it's going to be shown or hidden the first time by saying:

override func viewDidLoad() {
   super.viewDidLoad()
   fab.tag = hiddenFABTag
}
denis_lor
  • 6,212
  • 4
  • 31
  • 55