ios - iOS8出现键盘的动画速度是多少?

标签 ios objective-c swift ios8

以下是当键盘出现时向上移动的 textField 和 toolBar 的动画。

    baseConstraint.constant = 211
    self.view.setNeedsUpdateConstraints()

    UIView.animateWithDuration(0.30, animations: {
        self.view.layoutIfNeeded()
        })

它很接近但不完全相同。你会如何修改上面的动画?

编辑:

这是使用下面答案的最终代码!

   func keyboardWillShow(aNotification: NSNotification)    {

        let duration = aNotification.userInfo.objectForKey(UIKeyboardAnimationDurationUserInfoKey) as Double
        let curve = aNotification.userInfo.objectForKey(UIKeyboardAnimationCurveUserInfoKey) as UInt

        self.view.setNeedsLayout()
        baseConstraint.constant = 211
        self.view.setNeedsUpdateConstraints()

        UIView.animateWithDuration(duration, delay: 0, options: UIViewAnimationOptions.fromMask(curve), animations: {
        self.view.layoutIfNeeded()
        }, completion: {
        (value: Bool) in println()
        })
}

最佳答案

可以从keyboardWillShow: notifications上的userInfo字典中获取动画时长和动画曲线。

先注册通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

然后从通知 userInfo 键中获取值。

- (void)keyboardWillShow:(NSNotification*)notification {
    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [notification.userInfo objectForKey: UIKeyboardAnimationCurveUserInfoKey];

   // Do stuff with these values.
}

这些键还有很多,您也可以从 UIKeyboardWillDismiss 通知中获取它们。

此功能一直可用到 iOS 3.0 :D

这是文档:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/doc/constant_group/Keyboard_Notification_User_Info_Keys

快速版本

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)

@objc func keyboardWillShow(_ notification: Notification) {
    let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey]
    let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey]
}

关于ios - iOS8出现键盘的动画速度是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24923086/

相关文章:

ios - Firestore 文档创建失败,但没有错误

ios - 两个文本字段显示相同的 GMSPlace 自动完成

ios - 无法检测到键盘消失?

带有 Assets 目录的 iOS 图像切片会产生奇怪的伪像

objective-c - iOS/Objective-C : Adding NSString object to NSMutableArray, NSMutableArray 是(空)

ios - 在 Objective-C 中定义变量、setter 和 getter

ios - Swift 避免在初始化器中重复代码

ios - 我想要三个字母格式的月份,但 MMM 在 iOS UIDatePicker 的开头返回月份编号为 "M"

ios - swift 4 中带有文本字段和标签的自定义控件

iphone - CCLabelTTF 垂直对齐 (Cocos2D)