如何在 Swift3 iOS 中将 UIView 从底部呈现到屏幕的一半?
How to present a UIView from bottom to half of the screen in Swift3 iOS?
我想将我添加到 Storyboard
的 UIView
从底部移动到屏幕的一半,并带有一些动画。
我不想展示任何 UIViewController
,也不想使用任何 Third party Library
。
我试图通过设置 UIView
的帧原点 y 来解决这个问题,但没有实现。请建议我。
添加具有以下约束的视图
leadingConstraint: 0
trailingConstraint: 0
heightConstraint: screenheight/2
botttomConstraint: -height
当您想表达您的观点时,请执行以下操作
bottomConstraint = 0
UIView.animate(withDuration: 0.3) {
yourView.superview?.layoutIfNeeded()
}
添加 bottomConstraint 作为 IBOutlet
并更改动画块中的约束常量值
完整代码
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var customView: UIView!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.animate()
}
}
func animate() {
self.bottomConstraint.constant = 0
UIView.animate(withDuration: 2, animations: {
self.view.layoutIfNeeded()
}) { (finished) in
}
}
}
结果
我想将我添加到 Storyboard
的 UIView
从底部移动到屏幕的一半,并带有一些动画。
我不想展示任何 UIViewController
,也不想使用任何 Third party Library
。
我试图通过设置 UIView
的帧原点 y 来解决这个问题,但没有实现。请建议我。
添加具有以下约束的视图
leadingConstraint: 0
trailingConstraint: 0
heightConstraint: screenheight/2
botttomConstraint: -height
当您想表达您的观点时,请执行以下操作
bottomConstraint = 0
UIView.animate(withDuration: 0.3) {
yourView.superview?.layoutIfNeeded()
}
添加 bottomConstraint 作为 IBOutlet
并更改动画块中的约束常量值
完整代码
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var customView: UIView!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.animate()
}
}
func animate() {
self.bottomConstraint.constant = 0
UIView.animate(withDuration: 2, animations: {
self.view.layoutIfNeeded()
}) { (finished) in
}
}
}
结果