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
1 changed files with 13 additions and 13 deletions

View File

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