본문 바로가기

Programming/iOS

Swift keyboard notification

반응형
Swift version : 1.2
iOS version : 8.4


Swift 문법도 익숙치 않은데 NSNotificationCenter 사용하려는데 막히는 부분이 많고...

Notificaiton 에서 keyboard 관련 정보 가져오려는데.... 왜이리 오류가 많은지..

다음과 코드가 동작함을 확인하였으니 참고하세요.


 NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)


    func keyboardWillShow(notification: NSNotification) {
        let userInfo = notification.userInfo!

        let beginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
        let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        let animationOptions = UIViewAnimationOptions((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedLongValue << 16)

        UIView.animateWithDuration(duration, delay: 0.0, options: animationOptions, animations: { () -> Void in
            
        }, completion: nil)
    }


Swift version : 2.0
iOS version : 9.0


animationOptions 를 구하는 방법이 변경되었습니다.


    func keyboardWillShow(notification: NSNotification) {
        let userInfo = notification.userInfo!

        let beginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
        let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        let animationOptions = UIViewAnimationOptions(rawValue: (userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedLongValue)

        UIView.animateWithDuration(duration, delay: 0.0, options: animationOptions, animations: { () -> Void in
            
        }, completion: nil)
    }


반응형