- SF Symbols 中增加了新的類別 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)
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。