作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
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/
我是一名优秀的程序员,十分优秀!