iOS 7 - Keyboard animation

后端 未结 8 1594
广开言路
广开言路 2020-12-02 09:51

I\'m trying to understand the new keyboard animation in iOS 7.0 on the iPhone 5 Simulator. I want to resize my UITableView when the keyboard appears, but I can\

8条回答
  •  無奈伤痛
    2020-12-02 10:23

    In Swift 4


    Add observers for keyboard notifications:


    • UIKeyboardDidShowNotification
    • UIKeyboardDidHideNotification

    via

    NSNotificationCenter.defaultCenter().addObserver(_ observer: Any, 
            selector aSelector: Selector, 
            name aName: NSNotification.Name?, 
            object anObject: Any?)
    

    And implement selector to animate the UI with Keyboard animation. In order to create a valid curve value we need to shift UIResponder.keyboardAnimationCurveUserInfoKey by << 16


    func keyboardWillShow(_ notification: Notification!) {
        if let info = notification.userInfo {
            let keyboardSize = info[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
            let duration = info[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double 
            let curveVal = (info[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber)?.intValue ?? 7 // default value for keyboard animation
            let options = UIView.AnimationOptions(rawValue: UInt(curveVal << 16))
            UIView.animate(withDuration: duration, delay: 0, options: options, animations: {
            // any operation to be performed
        }, completion: nil)
        }
    }
    

提交回复
热议问题