【问题标题】:UIView animation does not work after a keyboard notificationUIView 动画在键盘通知后不起作用
【发布时间】:2018-04-16 11:46:37
【问题描述】:

当 iOS 设备旋转并且键盘启动时,NotificationCenter 会发送 UIKeyboardWillChangeFrame 通知。我正在尝试在那时开始自定义动画。它不起作用。

我正在尝试启动布局约束动画,但我不能使用任何自定义动画持续时间,因为它会以某种方式被系统覆盖。我将始终获得与键盘向上/向下移动动画相匹配的恒定持续时间。在这种情况下如何使用我自己的动画持续时间。

下面是我想要的行为的代码示例:

@objc func keyboardWillChange(_ notification: NSNotification) {

    let keyboardFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue


        self.textFieldBottomContstrain.constant = -keyboardFrame.height

        UIView.animate(withDuration: myOwnDuration) {
            self.view.layoutIfNeeded()
        }
}

更新

经过一些测试,我发现了一个非常简单的解决方案,虽然我觉得它有点 hacky。

但无论如何,在 UIView.animate-block 之前只添加两行代码似乎会重置自动动画参数并且我自己的参数再次起作用。这是我的工作补充:

UIView.setAnimationsEnabled(false)
UIView.setAnimationsEnabled(true)

更新 2

matt 提供的最佳解决方案。有一种正确的方法可以覆盖运行时设置的原始动画持续时间:https://developer.apple.com/documentation/uikit/uiviewanimationoptions/1622434-overrideinheritedduration

【问题讨论】:

  • 感谢您的澄清,尽管您没有为我最初的问题提供任何解决方案。看来我更新的解决方案可以按我的意愿工作。
  • 好的,我现在已经给出了更全面的解释和实际的解决方案。

标签: ios swift animation


【解决方案1】:

这里的问题是,当调用keyboardWillChange 时,您已经在一个您看不到的动画块中,由运行时提供以动画键盘的移动。因此:

  • 您不需要说UIView.animate,因为您在此处所做的任何可动画处理都已经被动画化了。

  • 如果您这样做说UIView.animate,您的duration 将被忽略,因为它继承自周围的隐式动画块——除非您添加option 来关闭继承:https://developer.apple.com/documentation/uikit/uiviewanimationoptions/1622434-overrideinheritedduration

  • 你不应该触摸self.view的位置;这不是你的观点。如果您需要在键盘存在时滑动界面,请给self.view 一个完整的子视图并滑动它。

【讨论】:

  • 有趣。希望我以前知道这一点。每个教程中关于动画和移动键盘的大多数示例都忽略了这个事实。您知道我自己找到的解决方案(禁用然后启用动画)是否与您关闭该继承的正确方法相同?
  • 可能,但使用overrideInheritedDuration 选项是正确的方法。看看它是否有效,如果有效,请改用它。
【解决方案2】:

试试这个代码。

UIView.animate(withDuration: 0.3, animations: {
            self.view.layoutIfNeeded();
            self.view.layoutSubviews()

        }, completion: {(value: Bool) in
        });

【讨论】:

    【解决方案3】:

    我可以为您提供这种方法。

    1) 添加结构体:

    struct HeightStruct {
        var height: CGFloat
    }
    

    2) 添加变量:

    var heightStruct: HeightStruct = .init(height: 0)
    

    3) 观察者方法:

    @objc func keyboardWillShow(_ notification: Notification) {
    
        if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
            let keyboardRectangle = keyboardFrame.cgRectValue
            let keyboardHeight = keyboardRectangle.height
    
            DispatchQueue.main.async {
                self.heightStruct.height = keyboardHeight
                self.changeTextFieldPosition()
            }
    
        }
    }
    

    4) 添加changeTextFieldPosition方法:

    func changeTextFieldPosition() {
    
        UIView.animate(withDuration: 5.0) {
            self. textFieldBottomContstrain.constant = - self.heightStruct.height
            self.view.layoutIfNeeded()
        }
    }
    

    它看起来很难看,但它确实有效。

    【讨论】:

    • 我会在某个时候尝试这个,但我想我会暂时坚持自己的解决方案(请参阅我更新的帖子)。
    【解决方案4】:

    1.在 viewDidLoad() 中注册键盘隐藏和显示通知

     NotificationCenter.default.addObserver(self, selector: 
     #selector(keyBoardWillShow(notification:)), name: 
     .UIKeyboardWillShow , object: nil)
    
    NotificationCenter.default.addObserver(self, selector: 
    #selector(keyBoardWillHide(notification:)), name: .UIKeyboardWillHide 
    , object: nil)
    

    2。键盘隐藏和显示时调用

    @objc func keyBoardWillShow(notification: NSNotification){
        adjustInsetForKeyBoards(show: true, notification: notification)
    }
    
    @objc func keyBoardWillHide(notification: NSNotification){
        adjustInsetForKeyBoards(show: false, notification: notification)
    }
    

    3.用动画显示和隐藏键盘的逻辑

    func adjustInsetForKeyBoards(show: Bool, notification: NSNotification){
        let userInfo = notification.userInfo ?? [:]
        let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        UIView.animate(
            withDuration: 1.0,
            animations: {
                let adjustment = (keyboardFrame.height * (show ? 1 : -1)) + 20
                self.scrollView.contentInset.bottom += adjustment
                self.scrollView.scrollIndicatorInsets.bottom += adjustment
        },
            completion: nil)
    
    }
    

    希望这对你有用。

    【讨论】:

    • 我认为这是一个代码示例,其中整个动画块是不必要的 -> 你的 1s 持续时间没有效果。你可以有任何你想要的动画持续时间值,但系统仍然使用默认的键盘动画值(在这种情况下为 0.7)。
    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多