【发布时间】:2019-09-07 06:06:16
【问题描述】:
在我的聊天应用程序中,当键盘从视图底部向上显示时,我无法为滚动视图(聊天视图)设置动画以向上滑动/调整。
如果您使用过任何主要的聊天应用程序,例如 Messenger、微信、Line 等,您会看到您的聊天屏幕随着键盘的出现而流畅地上升。现在,我已经模仿了 KeyboardAvoidingView 并实现了类似的东西:
// Triggered by Keyboard.addListener("keyboardWillChangeFrame", onKeyboardChange);
const onKeyboardChange = e => {
if (!e) {
//this is scroll view's contentInset.top
setSpacing(0);
return;
}
const { duration, easing, endCoordinates, startCoordinates } = e;
let offset;
if (startCoordinates) {
offset = startCoordinates.screenY - endCoordinates.screenY;
} else {
offset = endCoordinates.height;
}
const spacing = offset > 0 ? offset : 0;
if (duration && easing) {
LayoutAnimation.configureNext({
// We have to pass the duration equal to minimal accepted duration defined here: RCTLayoutAnimation.m
duration: duration > 10 ? duration : 10,
update: {
duration: duration > 10 ? duration : 10,
type: LayoutAnimation.Types[easing] || "keyboard"
}
});
}
//this is scroll view's contentInset.top
setSpacing(spacing);
};
如您所见,我目前只是使用 KeyboardWillChangeFrame 传递的事件计算偏移量,并使用 LayoutAnimation 为滚动视图上滑设置动画。 它工作得相当好,但我仍然可以看到轻微的延迟。
在原生iOS开发中,可以通过animateKeyframes来实现:
@objc func keyboardWillShow(notification: NSNotification){
let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
let curFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let targetFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let deltaY = targetFrame.origin.y - curFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.view.frame.origin.y += deltaY
})
}
但它似乎在本机反应中不可用。 无论如何我可以调整滚动视图,因为键盘从底部尽可能平滑地显示?
【问题讨论】:
标签: ios react-native react-native-ios react-animated