iOS17适配指南

iOS17适配指南之UIImageView、UIButton

2023-06-14  本文已影响0人  YungFan

UIImageView

import UIKit

class ViewController: UIViewController {
    lazy var imageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "hdr.png"))
        imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        imageView.contentMode = .scaleAspectFit
        imageView.center = view.center
        // 支持HDR图片
        imageView.preferredImageDynamicRange = .constrainedHigh
        return imageView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(imageView)
    }
}
import UIKit

class ViewController: UIViewController {
    lazy var imageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(systemName: "touchid"))
        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)
        imageView.addSymbolEffect(.variableColor.reversing, options: .repeat(3), animated: true) { context in
            if context.isFinished {
                print("动画完成")
            }
        }
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        imageView.removeSymbolEffect(ofType: .variableColor.reversing)
        // imageView.removeAllSymbolEffects()
    }
}

UIButton

Symbol Animations 新特性使得按钮中使用的 SF Symbols 图标也可以呈现动画。

import UIKit

class ViewController: UIViewController {
    lazy var button1: UIButton = {
        let button = UIButton(frame: CGRect(x: 100, y: 0, width: 100, height: 60))
        button.setImage(UIImage(systemName: "heart"), for: .normal)
        // 开启Symbol Animations
        button.isSymbolAnimationEnabled = true
        return button
    }()
    lazy var button2: UIButton = {
        let actionHandler = UIAction { _ in
        }
        actionHandler.image = UIImage(systemName: "plus")
        let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 60), primaryAction: actionHandler)
        button.isSymbolAnimationEnabled = true
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(button1)
        view.addSubview(button2)
    }
}
上一篇下一篇

猜你喜欢

热点阅读