2009-09-14 39 views
32

前段時間我記得看到一些那種定義iPhone上的鍵盤的動畫速度的恆定,我不能爲我的生活記得在那裏我看到它....任何見解?什麼是iPhone的默認鍵盤動畫速率?

+4

它總是0.3 ! :P – 2009-09-14 00:49:51

+8

鍵盤風格和旋轉行爲在2.2.1和3.0之間變化;誰說他們不會在更高版本中更改動畫速率? – rpetrich 2009-09-15 21:14:45

+0

一個更好的答案,這可以在http://stackoverflow.com/a/19235995/39946它提供了正確的時間和正確的animtation曲線上找到。 – 2013-12-06 13:39:52

回答

65
- (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification*)notification 
{ 
    NSDictionary* info = [notification userInfo]; 
    NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSTimeInterval duration = 0; 
    [value getValue:&duration]; 
    return duration; 
} 
+1

如果您來這裏尋找答案的MonoTouch,那沒有比'e.AnimationDuration'。 – 2013-08-27 20:12:00

+2

請注意,確切地說,您還應該調整其他動畫鍵,例如'UIKeyboardAnimationCurveUserInfoKey'。 – Rick 2013-11-26 23:16:26

+0

@丹,我是一個MonoTouch用戶,但什麼是'e'? – 2014-04-16 19:02:48

2

UIKeyboardAnimationDurationUserInfoKey 爲包含標識以秒爲動畫的持續時間的雙一個NSValue對象的鍵。

+0

嘿伴侶,是在通知的用戶信息的字典的關鍵UIKeyboardAnimationDurationUserInfoKey ???? -thx – ShortCircuit 2009-09-14 04:21:52

11

由於這是第一款谷歌命中,我想指出的是硬編碼0.3將意味着你的看法將不正確時動畫國際用戶不同尺寸鍵盤之間(如日本)掉期(時該行動應該是即時的)。

始終使用通知的用戶信息字典的UIKeyboardAnimationDurationUserInfoKey值 - 它被當用戶通過鍵盤輕彈設置爲0。

+2

注意:在撰寫本文時(iOS 5.1.1),默認持續時間現在爲0.25秒。所以就像@greenlight所說,永遠不要硬編碼 - 使用通知的userInfo字典中的數據。 – 2012-05-24 13:56:28

6

要添加多到什麼長毛青蛙寫了一點。全面實施會是這樣的:

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


-(void)keyboardMovement:(NSNotification *)notification{ 
    if (_numericKeyboardShowing == false){ 
     [UIView animateWithDuration:[self keyboardAnimationDurationForNotification:notification] delay:0 
         options:UIViewAnimationCurveEaseInOut 
        animations:^ { 
         self.bottomContainerView.center = CGPointMake(self.bottomContainerView.center.x, (self.bottomContainerView.center.y - 218)); 
            } 
        completion:NULL]; 

    _numericKeyboardShowing = true; 
    } 
    else{ 
    [UIView animateWithDuration:[self keyboardAnimationDurationForNotification:notification] delay:0 
         options:UIViewAnimationCurveLinear 
        animations:^ { 
         self.bottomContainerView.center = CGPointMake(self.bottomContainerView.center.x, (self.bottomContainerView.center.y + 218)); 
        } 
        completion:NULL]; 

    _numericKeyboardShowing = false; 
} 

- (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification *)notification 
{ 
    NSDictionary *info  = [notification userInfo]; 
    NSValue* value   = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    NSTimeInterval duration = 0; 
    [value getValue:&duration]; 
    return duration; 
} 
16

UIKeyboardAnimationDurationUserInfoKey現在是一個NSNumber的對象,這使得代碼更短。

- (void)keyboardWillShowNotification:(NSNotification *)notification 
{ 
    NSDictionary *info = [notification userInfo]; 
    NSNumber *number = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
    double duration = [number doubleValue]; 
} 
+0

一襯墊:'雙持續時間= [notification.userInfo [UIKeyboardAnimationDurationUserInfoKey]的doubleValue];' – 2015-08-05 06:26:07

2

在斯威夫特你的代碼看起來就像這樣:

let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size 

let animationDuration = ((userInfo[UIKeyboardAnimationDurationUserInfoKey]) as! NSNumber).floatValue 
let animationOptions = ((userInfo[UIKeyboardAnimationCurveUserInfoKey]) as! NSNumber).unsignedLongValue 

UIView.animateWithDuration(NSTimeInterval(animationDuration), delay: 0, 
    options: UIViewAnimationOptions(rawValue: animationOptions), 
    animations: {() -> Void in 
       self.view.frame.origin.y += keyboardSize.height 
       }, 
    completion: nil) 
0

斯威夫特4 - 爲我工作:

 if let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double { 
      UIView.animate(withDuration: duration, animations: { 
       self.view.layoutIfNeeded() 
      }) 
     } 

在調試模式下我duration3.499999