2009-09-14 42 views
32

Một lúc trước, tôi nhớ đã nhìn thấy một hằng số của một số loại định nghĩa tốc độ hoạt hình của Bàn phím trên iPhone và tôi không thể cho cuộc sống của tôi nhớ nơi tôi nhìn thấy nó .... bất kỳ cái nhìn sâu sắc?Tỷ lệ hoạt ảnh bàn phím mặc định của iPhone là gì?

+4

Nó luôn luôn 0.3 ! : P –

+8

Kiểu bàn phím và hành vi xoay được thay đổi giữa 2.2.1 và 3.0; ai nói rằng họ sẽ không thay đổi tốc độ hoạt hình trong các phiên bản sau? – rpetrich

+0

Một câu trả lời tốt hơn cho điều này có thể được tìm thấy tại http://stackoverflow.com/a/19235995/39946 Nó cung cấp thời gian chính xác và đường cong animtation chính xác. –

Trả lời

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

Nếu bạn đến đây tìm kiếm câu trả lời MonoTouch, hãy nhìn không xa hơn 'e.AnimationDuration'. –

+2

Lưu ý rằng để chính xác, bạn cũng nên điều chỉnh cho các phím hoạt ảnh khác, như 'UIKeyboardAnimationCurveUserInfoKey'. – Rick

+0

@Dan, tôi là người dùng MonoTouch, nhưng 'e' là gì? –

2

UIKeyboardAnimationDurationUserInfoKey Chìa khóa cho đối tượng NSValue có chứa gấp đôi xác định thời lượng của hoạt ảnh trong vài giây.

+0

Hey mate, là chìa khóa UIKeyboardAnimationDurationUserInfoKey trong từ điển thông tin người dùng của thông báo ???? -thx – ShortCircuit

11

Vì đây là lần đầu tiên google hit, tôi muốn chỉ ra rằng cứng mã hóa 0.3 sẽ có nghĩa rằng quan điểm của bạn không đúng cách sẽ làm động khi người dùng quốc tế (ví dụ như Nhật Bản) trao đổi giữa bàn phím kích thước khác nhau (khi hành động đó phải là ngay lập tức).

Luôn sử dụng thông số UIKeyboardAnimationDurationUserInfoKey của từ điển userInfo - số này được đặt thành 0 khi người dùng nhấp qua bàn phím.

+2

NB: tại thời điểm viết (iOS 5.1.1) thời lượng mặc định là 0,25 giây. Vì vậy, như @greenlight nói, không bao giờ khó mã này - sử dụng dữ liệu từ từ điển userInfo của thông báo. –

6

Để thêm một chút nữa vào những gì Shaggy Frog đã viết. Thực hiện đầy đủ sẽ là một cái gì đó như:

[[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 bây giờ là một đối tượng NSNumber, mà làm cho mã ngắn hơn.

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

Một lớp lót: 'double duration = [notification.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];' –

2

Trong Swift code của bạn sẽ trông như thế này:

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

Swift 4 - làm việc cho tôi:

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

Trong chế độ gỡ lỗi của tôi duration được 3.499999

Các vấn đề liên quan