iOS Question Get UIKeyboardAnimationDurationUserInfoKey and get UIKeyboardAnimationCurveUserInfoKey

Alexander Stolte

Expert
Licensed User
Longtime User
I would like to synchronize the opening of my bottom sheet with a textfield on it, with the opening of the keyboard to create a smoother user experience.
I come across tutorials online that read and work with the following 2 objects:

How can I read out these two properties in B4i?

Thanks :)
Alex
 
Solution
Ignoring the curve function, at least for now.

Add this objc code to the end of one of the modules:
B4X:
#if OBJC
@end
@interface B4IViewController (Keyboard2)
@end
@implementation B4IViewController (Keyboard2)
- (void) addWillHide {
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    [self storeDuration:notification];
    [B4IObjectWrapper raiseEvent:self :@"_keyboardstatechanged:" :@[@(keyboardSize.height)]];
}
- (void)storeDuration:(NSNotification*)notification {...

Erel

B4X founder
Staff member
Licensed User
Longtime User
Ignoring the curve function, at least for now.

Add this objc code to the end of one of the modules:
B4X:
#if OBJC
@end
@interface B4IViewController (Keyboard2)
@end
@implementation B4IViewController (Keyboard2)
- (void) addWillHide {
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    [self storeDuration:notification];
    [B4IObjectWrapper raiseEvent:self :@"_keyboardstatechanged:" :@[@(keyboardSize.height)]];
}
- (void)storeDuration:(NSNotification*)notification {
     double duration = ((NSNumber*)[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey]).doubleValue;
    [B4IObjectWrapper getMap:self][@"animation_duration"] = @(duration);
}
- (void)keyboardDidHide:(NSNotification *)notification {
    [self storeDuration:notification];
    [B4IObjectWrapper raiseEvent:self :@"_keyboardstatechanged:" :@[@(0)]];
}
#end if

In B4XPage_Created call this:
B4X:
#if B4i
    Dim no As NativeObject = B4XPages.GetNativeParent(Me) 'or Page1 in non-B4XPages project.
    no.RunMethod("addWillHide", Null)
#End If
This code also adds the "will hide" event (https://www.b4x.com/android/forum/threads/keyboard-will-hide-notification.126640/#content). Note that the KeyboardStateChanged event will be raised twice when the keyboard closes.

And to get the duration:
B4X:
Private Sub B4XPage_KeyboardStateChanged (Height As Float)
    Dim ow As NativeObject
    Dim duration As Int = ow.Initialize("B4IObjectWrapper").RunMethod("getMap:", Array(B4XPages.GetNativeParent(Me))) _
        .RunMethod("objectForKey:", Array("animation_duration")).AsNumber * 1000
    Log("duration in ms: " &  duration)
    Log("keyboard state changed: " & Height)
End Sub
 
Upvote 0
Solution

Alexander Stolte

Expert
Licensed User
Longtime User
Thank you very much :)

Ignoring the curve function, at least for now.
I have now been able to extend the code and made a tutorial out of it :)
 
Upvote 0
Top