diff --git a/VDS/Components/Checkbox/Checkbox.swift b/VDS/Components/Checkbox/Checkbox.swift index d16c12f4..5048f4e5 100644 --- a/VDS/Components/Checkbox/Checkbox.swift +++ b/VDS/Components/Checkbox/Checkbox.swift @@ -98,7 +98,8 @@ open class CheckboxBase: Control, Accessable, DataTrackable, Errorable { $0.translatesAutoresizingMaskIntoConstraints = false } - //can't bind to @Proxy + open var isAnimated: Bool = true { didSet { didChange() }} + open override var isSelected: Bool { didSet { didChange() }} open var labelText: String? { didSet { didChange() }} @@ -369,8 +370,6 @@ open class CheckboxBase: Control, Accessable, DataTrackable, Errorable { self.shapeLayer = nil } - selectorView.backgroundColor = backgroundColor - selectorView.layer.borderColor = borderColor.cgColor selectorView.layer.cornerRadius = VDSFormControls.borderradius selectorView.layer.borderWidth = VDSFormControls.widthBorder @@ -407,5 +406,30 @@ open class CheckboxBase: Control, Accessable, DataTrackable, Errorable { shapeLayer.strokeEnd = isSelected ? 1 : 0 } } + + shapeLayer?.removeAllAnimations() + + if isAnimated { + let animateStrokeEnd = CABasicAnimation(keyPath: "strokeEnd") + animateStrokeEnd.timingFunction = CAMediaTimingFunction(name: .linear) + animateStrokeEnd.duration = 0.3 + animateStrokeEnd.fillMode = .both + animateStrokeEnd.isRemovedOnCompletion = false + animateStrokeEnd.fromValue = !isSelected ? 1 : 0 + animateStrokeEnd.toValue = isSelected ? 1 : 0 + self.shapeLayer?.add(animateStrokeEnd, forKey: "strokeEnd") + + UIView.animate(withDuration: 0.2, delay: 0.1, options: .curveEaseOut, animations: { + self.selectorView.backgroundColor = backgroundColor + self.selectorView.layer.borderColor = borderColor.cgColor + }) + } else { + CATransaction.withDisabledAnimations { + self.shapeLayer?.strokeEnd = isSelected ? 1 : 0 + } + + selectorView.backgroundColor = backgroundColor + selectorView.layer.borderColor = borderColor.cgColor + } } }