[英]How to present a UIView from bottom to half of the screen in Swift3 iOS?
我想将UIView
,它已经添加到我的Storyboard
,并带有一些动画,从屏幕的底部移动到屏幕的一半。
我不想展示任何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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.