iOS18適配指南之UIUpdateLink

介紹

  • 用于觀察、參與以及影響 UI 更新的過程。
  • 可以監(jiān)聽 UI 更新的時間。
  • 可以影響 UI 更新的發(fā)生方式。
  • 可以在 UI 更新的過程中執(zhí)行某些操作。

使用

  • 基本使用。
import SwiftUI
import UIKit

class ViewController: UIViewController {
    lazy var imageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(systemName: "sun.min.fill"))
        imageView.contentMode = .scaleAspectFit
        imageView.frame = .init(x: 100, y: 100, width: 64, height: 64)
        return imageView
    }()
    // UIUpdateLink
    var updateLink: UIUpdateLink!

    override func viewDidLoad() {
        super.viewDidLoad()

        // 關(guān)聯(lián)UIView,當該UIView添加到父UIView或者UIWindows時自動激活,移除時自動失效
        updateLink = UIUpdateLink(view: imageView)
        // 添加Action,可以添加多個Action
        // 既可以使用Target-Action方式,也可以通過閉包方式
        updateLink.addAction(target: self, selector: #selector(update))
        updateLink.addAction { link, info in
        }
        // 添加到特定階段
        updateLink.addAction(to: .afterUpdateComplete, target: self, selector: #selector(update))
        updateLink.addAction(to: .afterUpdateScheduled) { link, info in
            self.imageView.center.x = cos(info.modelTime) * 150 + self.view.bounds.midX
            print("閉包", link, info)
        }
        updateLink.isEnabled = true
        updateLink.requiresContinuousUpdates = true
        view.addSubview(imageView)
    }

    // MARK: UI更新時調(diào)用
    @objc func update(link: UIUpdateLink, info: UIUpdateInfo) {
        print("Target-Action", link, info)
        // info包含有關(guān)當前UI更新狀態(tài)的詳細信息
        imageView.center.y = sin(info.modelTime) * 300 + view.bounds.midY
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        imageView.removeFromSuperview()
    }
}
  • 案例:監(jiān)聽 UIView 動畫中參數(shù)的變化,以layer.opacity為例。
import UIKit

class ViewController: UIViewController {
    lazy var redView: UIView = {
        let redView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
        redView.backgroundColor = .red
        redView.alpha = 0
        redView.center = view.center
        return redView
    }()
    // UIUpdateLink
    var updateLink: UIUpdateLink!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(redView)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        updateLink = UIUpdateLink(view: redView)
        updateLink.addAction { link, info in
            let layer = self.redView.layer.presentation()
            print(layer?.opacity ?? 0)
        }
        updateLink.isEnabled = true
        updateLink.requiresContinuousUpdates = true

        UIView.animate(withDuration: 10) {
            self.redView.alpha = 1
        } completion: { _ in
            self.updateLink.isEnabled = false
        }
    }
}
UIUpdateLink.gif

注意:UIUpdateLink 只能在主線程中使用。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容