Files
NetNewsWire/iOS/Article/ArticleExtractorButton.swift
T

96 lines
2.5 KiB
Swift
Raw Normal View History

2019-09-24 16:34:11 -05:00
//
// ArticleExtractorButton.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 9/24/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
enum ArticleExtractorButtonState {
case error
case animated
case on
case off
}
final class ArticleExtractorButton: UIButton {
2025-11-05 11:22:10 -08:00
2020-01-20 13:16:03 -07:00
private var animatedLayer: CALayer?
2025-11-05 11:22:10 -08:00
2019-09-24 16:34:11 -05:00
var buttonState: ArticleExtractorButtonState = .off {
didSet {
if buttonState != oldValue {
switch buttonState {
case .error:
2020-01-20 13:16:03 -07:00
stripAnimatedSublayer()
setImage(Assets.Images.articleExtractorError, for: .normal)
2019-09-24 16:34:11 -05:00
case .animated:
setImage(nil, for: .normal)
setNeedsLayout()
case .on:
2020-01-20 13:16:03 -07:00
stripAnimatedSublayer()
setImage(Assets.Images.articleExtractorOn, for: .normal)
2019-09-24 16:34:11 -05:00
case .off:
2020-01-20 13:16:03 -07:00
stripAnimatedSublayer()
setImage(Assets.Images.articleExtractorOff, for: .normal)
2019-09-24 16:34:11 -05:00
}
}
}
}
2025-11-05 11:22:10 -08:00
override var accessibilityLabel: String? {
get {
switch buttonState {
case .error:
return NSLocalizedString("Error - Reader View", comment: "Error - Reader View")
case .animated:
return NSLocalizedString("Processing - Reader View", comment: "Processing - Reader View")
case .on:
return NSLocalizedString("Selected - Reader View", comment: "Selected - Reader View")
case .off:
return NSLocalizedString("Reader View", comment: "Reader View")
}
}
set {
super.accessibilityLabel = newValue
}
}
2025-11-05 11:22:10 -08:00
2019-09-24 16:34:11 -05:00
override func layoutSubviews() {
super.layoutSubviews()
guard case .animated = buttonState else {
return
}
2020-01-20 13:16:03 -07:00
stripAnimatedSublayer()
2019-09-24 16:34:11 -05:00
addAnimatedSublayer(to: layer)
}
2025-11-05 11:22:10 -08:00
2020-01-20 13:16:03 -07:00
private func stripAnimatedSublayer() {
animatedLayer?.removeFromSuperlayer()
2019-09-24 16:34:11 -05:00
}
2025-11-05 11:22:10 -08:00
2019-09-24 16:34:11 -05:00
private func addAnimatedSublayer(to hostedLayer: CALayer) {
2025-11-19 10:38:18 -08:00
let image1 = Assets.Images.articleExtractorOffTinted.cgImage!
let image2 = Assets.Images.articleExtractorOnTinted.cgImage!
2019-09-24 16:34:11 -05:00
let images = [image1, image2, image1]
2025-11-05 11:22:10 -08:00
2020-01-20 13:16:03 -07:00
animatedLayer = CALayer()
let imageSize = Assets.Images.articleExtractorOff.size
2020-01-20 13:16:03 -07:00
animatedLayer!.bounds = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height)
animatedLayer!.position = CGPoint(x: bounds.midX, y: bounds.midY)
2025-11-05 11:22:10 -08:00
2020-01-20 13:16:03 -07:00
hostedLayer.addSublayer(animatedLayer!)
2025-11-05 11:22:10 -08:00
2019-09-24 16:34:11 -05:00
let animation = CAKeyframeAnimation(keyPath: "contents")
animation.calculationMode = CAAnimationCalculationMode.linear
animation.keyTimes = [0, 0.5, 1]
animation.duration = 2
animation.values = images as [Any]
animation.repeatCount = HUGE
2025-11-05 11:22:10 -08:00
2020-01-20 13:16:03 -07:00
animatedLayer!.add(animation, forKey: "contents")
2019-09-24 16:34:11 -05:00
}
2025-11-05 11:22:10 -08:00
2019-09-24 16:34:11 -05:00
}