- SF Symbols 中增加了新的類(lèi)別 Variable,其中的圖標(biāo)支持可變渲染。
- UIImage 相應(yīng)地增加了新的構(gòu)造函數(shù)支持可變渲染。
import Combine
import UIKit
class ViewController: UIViewController {
// 可變色度
lazy var variable: Double = 0 {
didSet {
// 新的構(gòu)造函數(shù),支持可變渲染
let image = UIImage(systemName: "touchid", variableValue: variable, configuration: symbolConfig)
imageView.image = image
}
}
// 配置圖標(biāo)的渲染顏色
let symbolConfig = UIImage.SymbolConfiguration(paletteColors: [.systemTeal, .systemGreen])
var cancellable: [AnyCancellable] = []
// Combine定時(shí)器
let timer = Timer.publish(every: 0.2, on: .main, in: .common)
lazy var imageView: UIImageView = {
let imageView = UIImageView(image: UIImage(systemName: "touchid", variableValue: 0, configuration: symbolConfig))
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
imageView.contentMode = .scaleAspectFit
imageView.center = view.center
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(imageView)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 動(dòng)態(tài)調(diào)整Variable圖標(biāo)
timer
.autoconnect()
.sink { [weak self] _ in
guard let self = self else { return }
switch self.variable {
case 0:
self.variable = 0.2
case 0.2:
self.variable = 0.4
case 0.4:
self.variable = 0.6
case 0.6:
self.variable = 0.8
case 0.8:
self.variable = 1.0
case 1.0:
self.variable = 0.9
case 0.9:
self.variable = 0.7
case 0.7:
self.variable = 0.5
case 0.5:
self.variable = 0.3
case 0.3:
self.variable = 0.1
default:
self.variable = 0
}
}
.store(in: &cancellable)
}
}