【问题标题】:How to present a UIView from bottom to half of the screen in Swift3 iOS?如何在 Swift3 iOS 中从屏幕底部到屏幕的一半呈现 UIView?
【发布时间】:2018-11-18 05:52:45
【问题描述】:

我想将我添加到Storyboard 的UIView 从屏幕底部移动到屏幕的一半,并带有一些动画。

我不想展示任何UIViewController,也不想使用任何Third party Library。

我试图通过设置 UIView 的框架原点 y 来解决这个问题,但没有实现。请给我建议。

【问题讨论】:

  • 添加一个 bottomConstraint 作为 IBOutlet 并更改动画块内的约束常量值

标签: ios animation swift3 uiview


【解决方案1】:

使用以下约束添加您的视图

leadingConstraint: 0
trailingConstraint: 0
heightConstraint: screenheight/2
botttomConstraint: -height

当你想展示你的观点时,请执行以下操作

bottomConstraint = 0

UIView.animate(withDuration: 0.3) {
    yourView.superview?.layoutIfNeeded()
}

【讨论】:

    【解决方案2】:

    添加一个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
    
            }
        }
    
    }
    

    结果

    【讨论】:

    • 感谢您的回复@Reinier,让我应用此代码。
    • 根据您的输出,UIView 从底部开始,来到超级视图中心。但根据我的要求它应该像选择器,而不是在中心。
    • 是的,我已经看到了,让我将其更新到我的代码中。我会尽快更新你。再次感谢..
    • @user2786 我的答案已更新,您的约束原本应该是 = -yourCustomView.height 并且在动画中应该更改为 0
    • 好吧,假设如果我需要用动画隐藏它,那么必须将 y 约束设置为 -yourCustomView.height?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多