2019-09-24 23:34:11 +02: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
|
|
|
|
}
|
|
|
|
|
2024-04-20 07:30:37 +02:00
|
|
|
@MainActor final class ArticleExtractorButton: UIButton {
|
2019-09-27 19:12:12 +02:00
|
|
|
|
2020-01-20 21:16:03 +01:00
|
|
|
private var animatedLayer: CALayer?
|
|
|
|
|
2019-09-24 23:34:11 +02:00
|
|
|
var buttonState: ArticleExtractorButtonState = .off {
|
|
|
|
didSet {
|
|
|
|
if buttonState != oldValue {
|
|
|
|
switch buttonState {
|
|
|
|
case .error:
|
2020-01-20 21:16:03 +01:00
|
|
|
stripAnimatedSublayer()
|
2019-09-24 23:34:11 +02:00
|
|
|
setImage(AppAssets.articleExtractorError, for: .normal)
|
|
|
|
case .animated:
|
|
|
|
setImage(nil, for: .normal)
|
|
|
|
setNeedsLayout()
|
|
|
|
case .on:
|
2020-01-20 21:16:03 +01:00
|
|
|
stripAnimatedSublayer()
|
2019-09-24 23:34:11 +02:00
|
|
|
setImage(AppAssets.articleExtractorOn, for: .normal)
|
|
|
|
case .off:
|
2020-01-20 21:16:03 +01:00
|
|
|
stripAnimatedSublayer()
|
2019-09-24 23:34:11 +02:00
|
|
|
setImage(AppAssets.articleExtractorOff, for: .normal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-09 22:38:25 +01: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 23:34:11 +02:00
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
|
|
|
guard case .animated = buttonState else {
|
|
|
|
return
|
|
|
|
}
|
2020-01-20 21:16:03 +01:00
|
|
|
stripAnimatedSublayer()
|
2019-09-24 23:34:11 +02:00
|
|
|
addAnimatedSublayer(to: layer)
|
|
|
|
}
|
|
|
|
|
2020-01-20 21:16:03 +01:00
|
|
|
private func stripAnimatedSublayer() {
|
|
|
|
animatedLayer?.removeFromSuperlayer()
|
2019-09-24 23:34:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func addAnimatedSublayer(to hostedLayer: CALayer) {
|
|
|
|
let image1 = AppAssets.articleExtractorOffTinted.cgImage!
|
|
|
|
let image2 = AppAssets.articleExtractorOnTinted.cgImage!
|
|
|
|
let images = [image1, image2, image1]
|
|
|
|
|
2020-01-20 21:16:03 +01:00
|
|
|
animatedLayer = CALayer()
|
2019-09-24 23:34:11 +02:00
|
|
|
let imageSize = AppAssets.articleExtractorOff.size
|
2020-01-20 21:16:03 +01:00
|
|
|
animatedLayer!.bounds = CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height)
|
|
|
|
animatedLayer!.position = CGPoint(x: bounds.midX, y: bounds.midY)
|
2019-09-24 23:34:11 +02:00
|
|
|
|
2020-01-20 21:16:03 +01:00
|
|
|
hostedLayer.addSublayer(animatedLayer!)
|
2019-09-24 23:34:11 +02: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
|
|
|
|
|
2020-01-20 21:16:03 +01:00
|
|
|
animatedLayer!.add(animation, forKey: "contents")
|
2019-09-24 23:34:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|