gpt4 book ai didi

swift - 调出键盘时,更改滚动插图会自动设置动画

转载 作者:行者123 更新时间:2023-11-28 13:55:37 25 4
gpt4 key购买 nike

当更改 ScrollView 的插图时,它们会随着键盘的显示和隐藏而出现令人不快的动画

这是我应用程序的消息传递部分。当我想调出键盘时,我需要立即更改显示消息的 Collection View 的 ScrollView 的大小

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {

@IBOutlet weak var myCollectionView: UICollectionView!
@IBOutlet weak var textfield: UITextField!


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = UIColor.random()
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 50)
}

override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidHide), name: UIResponder.keyboardDidHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)


}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

}

@objc func handleKeyboardDidHide(notification: NSNotification){
//we only wanna do this if we're at the bottom
// if checkIfScrolledToPosition(distanceFromBottom: 0) {
//
// self.scrollToBottomOfCollectionView(animated: true)
// }
}

@objc func handleKeyboardDidShow(notification: NSNotification){
//temp commented for testing
// if shouldScrollToBottomOnceKeyboardShow{
// self.scrollToBottomOfCollectionView(animated: true)
// shouldScrollToBottomOnceKeyboardShow = false
// }
}
@objc func handleKeyboardNotification(notification: NSNotification?){

if let keyboardFrame: NSValue = notification?.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
let isKeyboardShowing = notification!.name == UIResponder.keyboardWillShowNotification

if isKeyboardShowing{
setCollectionViewScrollInsets(heightFromBottom: keyboardHeight)
}else{
setCollectionViewScrollInsets(heightFromBottom: 0)

}
}

}


@IBAction func button(_ sender: Any) {
textfield.resignFirstResponder()
}

func setCollectionViewScrollInsets(heightFromBottom: CGFloat){
// messagesCollectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: heightFromBottom, right: 0)
myCollectionView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: heightFromBottom, right: 0)
//myCollectionView.layoutIfNeeded()//without this is seems to cause severe fuck up
}


}

我希望 ScrollView 立即改变大小,但实际变化是渐进的,并且是通过键盘进行动画处理的。我怀疑这与在与键盘显示相关的一些隐藏方法中更新布局有关,但这会影响 View 的其余部分。

此问题的一个可能解决方案是在显示键盘时强制 ScrollView 保持滚动到底部,这样我就不必担心插图会立即发生变化,如果有人知道该怎么做的话。

最佳答案

您正在键盘动画中执行您的布局(因为通知是在此动画期间发送的),因此您的动画更改(例如,您对后续显式布局过程中的插图和框架更改的操作)在其中进行动画处理那个动画。您可以使用 [UIView performWithoutAnimation:]如果您真的需要在没有动画的情况下执行这些操作。

关于swift - 调出键盘时,更改滚动插图会自动设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53907385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com