gpt4 book ai didi

ios - 在 iOS 中出现键盘时滑动 UIView

转载 作者:行者123 更新时间:2023-11-29 01:20:51 27 4
gpt4 key购买 nike

我知道这里有很多关于当键盘出现在 iOS 上时滑动 UIView 的类似问题。但是,我的实现似乎存在一个我没有看到提到的问题。当我的幻灯片动画出现时,它并没有像我期望的那样从屏幕顶部开始,而是从较低的原点开始并向上移动回到屏幕顶部。

UIView Sliding Incorrectly

这是我为实现这种向上滑动行为而创建的类扩展。

public extension UIViewController {

private dynamic func keyboardWillShow(sender: NSNotification) {
if view.frame.origin.y == 0 {
slideViewVerticallyForKeyboardHeight(sender, directionUp: true)
}
}

private dynamic func keyboardWillHide(sender: NSNotification) {
if view.frame.origin.y < 0 {
slideViewVerticallyForKeyboardHeight(sender, directionUp: false)
}
}

private func slideViewVerticallyForKeyboardHeight(notification: NSNotification, directionUp: Bool) {
UIView.beginAnimations(nil, context: nil)
let keyboardHeight: CGFloat = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().height ?? 0.0
let animationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue ?? 0.0
let animationCurve: Int = notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey]?.integerValue ?? 0
UIView.setAnimationDuration(animationDuration)
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: animationCurve)!)
UIView.setAnimationBeginsFromCurrentState(true)

var newFrameRect = view.frame
if directionUp {
newFrameRect.origin.y -= keyboardHeight
newFrameRect.size.height += keyboardHeight
} else {
newFrameRect.origin.y += keyboardHeight
newFrameRect.size.height -= keyboardHeight
}
view.frame = newFrameRect
UIView.commitAnimations()
}

public func registerViewSlideOnKeyobard() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

public func unregisterViewSlideOnKeyboard() {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
}

我不确定我错过了什么。看看我的 view.frame 值,正如我所期望的,我的 origin.y 为负值。有人看到我错过了什么吗?

编辑:用户aiak提出了非常明智的建议,即我更改为(未弃用的)动画 block 调用版本。有趣的是,动画仍然被破坏,但以不同的方式。以下是修改后的代码以及更新的动画调用所发生情况的屏幕截图。

enter image description here

private func slideViewVerticallyForKeyboardHeight(notification: NSNotification, directionUp: Bool) {
UIView.beginAnimations(nil, context: nil)
let keyboardHeight: CGFloat = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().height ?? 0.0
let animationDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue ?? 0.0
let animationCurve: UIViewAnimationCurve = UIViewAnimationCurve(rawValue: notification.userInfo?[UIKeyboardAnimationCurveUserInfoKey]?.integerValue ?? 0)!
var newFrameRect = view.frame
if directionUp {
newFrameRect.origin.y -= keyboardHeight
newFrameRect.size.height += keyboardHeight
} else {
newFrameRect.origin.y += keyboardHeight
newFrameRect.size.height -= keyboardHeight
}

UIView.animateWithDuration(animationDuration, delay: 0, options: animationCurve.toOptions(), animations: {
self.view.frame = newFrameRect
}, completion: nil)
}

最佳答案

这可能没有完全帮助,但自 iOS 4+ 以来不鼓励使用这些动画方法:

Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.

引用:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/clm/UIView/commitAnimations

您尝试过基于 block 的 API 吗?

编辑:这是自动布局问题!

好的,我想我知道了:你们使用自动布局吗?如果是这样,UIViewController.view 的 subview 可能对 view.top = 0 有约束,并且该约束永远不会被打破。如果您想实现您想要的动画,请考虑:1. 将每个 subview 嵌入 scrollView 中(然后为 contentOffet.y 更改设置动画)或 2. 删除/编辑您的 view.top 约束

关于ios - 在 iOS 中出现键盘时滑动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620580/

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