介紹
UIKit 進(jìn)一步融合 SwiftUI涩金,UIView 動(dòng)畫可以使用 SwiftUI 動(dòng)畫弟晚。
使用
- 代碼辆雾。
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動(dòng)畫
func animate() {
// 使用SwiftUI動(dòng)畫
// 第1個(gè)
UIView.animate(SwiftUI.Animation.linear(duration: 1.0)) {
self.redView.center.y = 300
}
// 第2個(gè)
UIView.animate(SwiftUI.Animation.easeInOut(duration: 1.0).delay(1.5).speed(2)) {
self.greenView.center.y = 300
}
// 第3個(gè)
UIView.animate(Animation.spring(duration: 2.0, bounce: 0.7, blendDuration: 0)) {
self.blueView.center.y = 300
}
}
}
- 效果。