gpt4 book ai didi

xcode - UIKeyboardAnimationDurationUserInfoKey 返回极小的值

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:50 25 4
gpt4 key购买 nike

func keyboardWillShow(notification:NSNotification){
let userInfo = notification.userInfo
let keyboardFrame = userInfo?[UIKeyboardFrameEndUserInfoKey] as NSValue
let keyboardSize = keyboardFrame.CGRectValue().size
let animationDurationValue = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as NSValue
var animationDuration : NSTimeInterval = 0
animationDurationValue.getValue(&animationDuration)

self.keyboardDelegate?.keyboardWillShowWithSize(keyboardSize, andDuration: animationDuration)
}

在我的程序中,我尝试使用上面的函数在出现键盘时重新定位我的 View 。在我的 objective-c 代码中以相同的方式获取键盘动画持续时间。这个给了我以下状态:

duration 5.18065378653631e-315

这是一个异常小的值。我哪里做错了?请帮忙!

编辑:完整代码:

import Foundation
import UIKit

@objc protocol LPKeyboardViewControllerDelegate {
func keyboardWillShowWithSize(size:CGSize, andDuration duration:NSTimeInterval)
func keyboardWillHideWithSize(size:CGSize,andDuration duration:NSTimeInterval)
}

/**
This view controller will move up its view when a keyboard appears in its view
*/
class LPKeyboardViewController: UIViewController {
var keyboardDelegate: LPKeyboardViewControllerDelegate?

// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

println("Start listening to keyboard")
}

func keyboardWillShow(notification:NSNotification){
let userInfo = notification.userInfo
let keyboardFrame = userInfo?[UIKeyboardFrameEndUserInfoKey] as NSValue
let keyboardSize = keyboardFrame.CGRectValue().size
let animationDurationValue = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as NSValue
var animationDuration : NSTimeInterval = 0
animationDurationValue.getValue(&animationDuration)

self.keyboardDelegate?.keyboardWillShowWithSize(keyboardSize, andDuration: animationDuration)
}

func keyboardWillHide(notification:NSNotification){
let userInfo = notification.userInfo
let keyboardFrame = userInfo?[UIKeyboardFrameEndUserInfoKey] as NSValue
let keyboardSize = keyboardFrame.CGRectValue().size
let animationDurationValue = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as NSNumber
var animationDuration : NSTimeInterval = animationDurationValue.doubleValue

self.keyboardDelegate?.keyboardWillHideWithSize(keyboardSize, andDuration: animationDuration)

}


}

最佳答案

文档指出这个键的对象是一个 NSNumber 所以你不必跳过那些你可以做的事情

let animationDurationValue = userInfo?[UIKeyboardAnimationDurationUserInfoKey] as NSNumber
let animationDuration = animationDurationValue.doubleValue

关于xcode - UIKeyboardAnimationDurationUserInfoKey 返回极小的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973688/

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