前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS16适配指南之UIImage

iOS16适配指南之UIImage

作者头像
YungFan
发布2022-06-23 14:37:05
1K0
发布2022-06-23 14:37:05
举报
文章被收录于专栏:学海无涯学海无涯
  • SF Symbols 中增加了新的类别 Variable,其中的图标支持可变渲染。
  • UIImage 相应地增加了新的构造函数支持可变渲染。
代码语言:javascript
复制
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)
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-06-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档