Fix animation layer stripping. Issue #1668

This commit is contained in:
Maurice Parker 2020-01-20 13:16:03 -07:00
parent fe75fb1aa4
commit 61c854b031

View File

@ -17,21 +17,23 @@ enum ArticleExtractorButtonState {
class ArticleExtractorButton: UIButton { class ArticleExtractorButton: UIButton {
private var animatedLayer: CALayer?
var buttonState: ArticleExtractorButtonState = .off { var buttonState: ArticleExtractorButtonState = .off {
didSet { didSet {
if buttonState != oldValue { if buttonState != oldValue {
switch buttonState { switch buttonState {
case .error: case .error:
stripSublayer() stripAnimatedSublayer()
setImage(AppAssets.articleExtractorError, for: .normal) setImage(AppAssets.articleExtractorError, for: .normal)
case .animated: case .animated:
setImage(nil, for: .normal) setImage(nil, for: .normal)
setNeedsLayout() setNeedsLayout()
case .on: case .on:
stripSublayer() stripAnimatedSublayer()
setImage(AppAssets.articleExtractorOn, for: .normal) setImage(AppAssets.articleExtractorOn, for: .normal)
case .off: case .off:
stripSublayer() stripAnimatedSublayer()
setImage(AppAssets.articleExtractorOff, for: .normal) setImage(AppAssets.articleExtractorOff, for: .normal)
} }
} }
@ -61,14 +63,12 @@ class ArticleExtractorButton: UIButton {
guard case .animated = buttonState else { guard case .animated = buttonState else {
return return
} }
stripSublayer() stripAnimatedSublayer()
addAnimatedSublayer(to: layer) addAnimatedSublayer(to: layer)
} }
private func stripSublayer() { private func stripAnimatedSublayer() {
if layer.sublayers?.count ?? 0 > 1 { animatedLayer?.removeFromSuperlayer()
layer.sublayers?.last?.removeFromSuperlayer()
}
} }
private func addAnimatedSublayer(to hostedLayer: CALayer) { private func addAnimatedSublayer(to hostedLayer: CALayer) {
@ -76,12 +76,12 @@ class ArticleExtractorButton: UIButton {
let image2 = AppAssets.articleExtractorOnTinted.cgImage! let image2 = AppAssets.articleExtractorOnTinted.cgImage!
let images = [image1, image2, image1] let images = [image1, image2, image1]
let imageLayer = CALayer() animatedLayer = CALayer()
let imageSize = AppAssets.articleExtractorOff.size let imageSize = AppAssets.articleExtractorOff.size
imageLayer.bounds = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height) animatedLayer!.bounds = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height)
imageLayer.position = CGPoint(x: bounds.midX, y: bounds.midY) animatedLayer!.position = CGPoint(x: bounds.midX, y: bounds.midY)
hostedLayer.addSublayer(imageLayer) hostedLayer.addSublayer(animatedLayer!)
let animation = CAKeyframeAnimation(keyPath: "contents") let animation = CAKeyframeAnimation(keyPath: "contents")
animation.calculationMode = CAAnimationCalculationMode.linear animation.calculationMode = CAAnimationCalculationMode.linear
@ -90,7 +90,7 @@ class ArticleExtractorButton: UIButton {
animation.values = images as [Any] animation.values = images as [Any]
animation.repeatCount = HUGE animation.repeatCount = HUGE
imageLayer.add(animation, forKey: "contents") animatedLayer!.add(animation, forKey: "contents")
} }
} }