UIKit 进一步融合 SwiftUI,UIView 动画可以使用 SwiftUI 动画。
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
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有