Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 滚动UICollectionView以响应键盘通知_Ios_Swift_Autolayout_Uicollectionview_Core Animation - Fatal编程技术网

Ios 滚动UICollectionView以响应键盘通知

Ios 滚动UICollectionView以响应键盘通知,ios,swift,autolayout,uicollectionview,core-animation,Ios,Swift,Autolayout,Uicollectionview,Core Animation,我正在构建一个messenger应用程序 我当前的键盘通知功能如下: func handleKeyboardNotification(notification: NSNotification) { if let userInfo = notification.userInfo { let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue() p

我正在构建一个messenger应用程序

我当前的键盘通知功能如下:

func handleKeyboardNotification(notification: NSNotification) {

        if let userInfo = notification.userInfo {
            let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue()
            print(keyboardFrame)


            let isKeyboardShowing = notification.name == UIKeyboardWillShowNotification


            bottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0



            UIView.animateWithDuration(0, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {

                self.view.layoutIfNeeded()
                }, completion: { (completed) in

                    if isKeyboardShowing {
                        let indexPath = NSIndexPath(forItem: self.messages!.count - 1, inSection: 0)
                        self.collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
                    }

            })

    }
}
(bottomConstraint.constant是指位于屏幕底部或键盘框架顶部的消息输入文本字段)

但是,我希望完成参数中的操作与键盘打开同时发生,而不是在键盘打开后发生。目前,当我关闭键盘时,它似乎自动布局良好,只是在我打开键盘时没有

到目前为止,我已经尝试了很多方法,但都没有效果。
有什么建议吗?

如果您希望在键盘按下的同时完成编写的代码块,则不应在完成块中编写。 您可以在调用带持续时间的动画函数之前使用dispatch_async,这样它就可以同时工作:

   func handleKeyboardNotification(notification: NSNotification) {

            if let userInfo = notification.userInfo {
                let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue()
                print(keyboardFrame)


                let isKeyboardShowing = notification.name == UIKeyboardWillShowNotification


                bottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0

DispatchQueue.main.async {
    if isKeyboardShowing {
                    let indexPath = NSIndexPath(forItem: self.messages!.count - 1, inSection: 0)
                            self.collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}


            UIView.animateWithDuration(0, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {self.view.layoutIfNeeded()}, completion: nil})

    }
}

如果您希望在键盘按下的同时完成编写的代码块,则不应在完成块中编写。 您可以在调用带持续时间的动画函数之前使用dispatch_async,这样它就可以同时工作:

   func handleKeyboardNotification(notification: NSNotification) {

            if let userInfo = notification.userInfo {
                let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue()
                print(keyboardFrame)


                let isKeyboardShowing = notification.name == UIKeyboardWillShowNotification


                bottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0

DispatchQueue.main.async {
    if isKeyboardShowing {
                    let indexPath = NSIndexPath(forItem: self.messages!.count - 1, inSection: 0)
                            self.collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}


            UIView.animateWithDuration(0, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {self.view.layoutIfNeeded()}, completion: nil})

    }
}

我需要ScrollView在我的键盘出现时也移动,所以我使用了上面答案的一个版本:*DispatchQueue.main.async{self.scrollToBottomAnimated(动画:true)}**我需要ScrollView在我的键盘出现时也移动,因此,我使用了上述答案的一个版本:*DispatchQueue.main.async{self.scrollToBottomAnimated(动画:true)}**