iOS18适配指南之融合SwiftUI

2024-09-09 11:35:30 浏览数 (1)

介绍

UIKit 进一步融合 SwiftUI,UIView 动画可以使用 SwiftUI 动画。

使用

  • 代码。
代码语言:swift复制
import SwiftUI
import UIKit

class ViewController: UIViewController {
    lazy var redView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 50, width: 100, height: 100))
        view.backgroundColor = .red
        return view
    }()
    lazy var greenView: UIView = {
        let view = UIView(frame: CGRect(x: 120, y: 50, width: 100, height: 100))
        view.backgroundColor = .green
        return view
    }()
    lazy var blueView: UIView = {
        let view = UIView(frame: CGRect(x: 240, y: 50, width: 100, height: 100))
        view.backgroundColor = .blue
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(blueView)
        view.addSubview(greenView)
        view.addSubview(redView)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        animate()
    }
}

extension ViewController {
    // MARK: UIView动画
    func animate() {
        // 使用SwiftUI动画
        // 第1个
        UIView.animate(SwiftUI.Animation.linear(duration: 1.0)) {
            self.redView.center.y = 300
        }
        // 第2个
        UIView.animate(SwiftUI.Animation.easeInOut(duration: 1.0).delay(1.5).speed(2)) {
            self.greenView.center.y = 300
        }
        // 第3个
        UIView.animate(Animation.spring(duration: 2.0, bounce: 0.7, blendDuration: 0)) {
            self.blueView.center.y = 300
        }
    }
}
  • 效果。
SwiftUI动画.gifSwiftUI动画.gif

0 人点赞