iOS16适配指南之UIImage

  • SF Symbols 中增加了新的类别 Variable,其中的图标支持可变渲染。
  • UIImage 相应地增加了新的构造函数支持可变渲染。
import Combine
import UIKit

class ViewController: UIViewController {
    // 可变色度
    lazy var variable: Double = 0 {
        didSet {
            // 新的构造函数,支持可变渲染
            let image = UIImage(systemName: "touchid", variableValue: variable, configuration: symbolConfig)
            imageView.image = image
        }
    }
    // 配置图标的渲染颜色
    let symbolConfig = UIImage.SymbolConfiguration(paletteColors: [.systemTeal, .systemGreen])
    var cancellable: [AnyCancellable] = []
    // Combine定时器
    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?) {
        // 动态调整Variable图标
        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)
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • iOS 16 真机调试时需要在设备的设置 —> 隐私与安全 —> 开发者模式 中打开开发者模式。 新增 UICal...
    YungFan阅读 13,422评论 17 23
  • 开发小知识(一)[https://www.jianshu.com/p/5a4ba3c165b9] 开发小知识(二)...
    ZhengYaWei阅读 13,422评论 11 140
  • UIKit 1.UIView 和 CALayer 是什么关系? UIView 继承 UIResponder,而 U...
    Sephiroth_Ma阅读 6,912评论 0 25
  • 做这个的初心是希望能巩固自己的基础知识,也通过这种方式检查自己知识的缺失点。目前还不完善,后续会根据时间不断更正和...
    lp_lp阅读 36,878评论 7 147
  • 1.网络 1.网络七层协议有哪些? 物理层:主要功能:传输比特流;典型设备:集线器、中继器;典型协议标准和应用:V...
    _我和你一样阅读 9,160评论 1 38

友情链接更多精彩内容