gpt4 book ai didi

iphone - 当 contentOffset 有值时,Scrollview 的位置在动画过程中跳跃

转载 作者:行者123 更新时间:2023-12-03 21:06:56 25 4
gpt4 key购买 nike

我正在使用此处找到的 TPKeyboardAvoidingScrollView:https://github.com/michaeltyson/TPKeyboardAvoiding

以下是该类的相关代码块:

- (void)keyboardWillShow:(NSNotification*)notification {
if ( !CGRectEqualToRect(priorFrame, CGRectZero) ) return;

UIView *firstResponder = [self findFirstResponderBeneathView:self];
if ( !firstResponder ) {
// No child view is the first responder - nothing to do here
return;
}

priorFrame = self.frame;

// Use this view's coordinate system
CGRect keyboardBounds = [self convertRect:[[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
CGRect screenBounds = [self convertRect:[UIScreen mainScreen].bounds fromView:nil];
if ( keyboardBounds.origin.y == 0 ) keyboardBounds.origin = CGPointMake(0, screenBounds.size.height - keyboardBounds.size.height);

CGFloat spaceAboveKeyboard = keyboardBounds.origin.y - self.bounds.origin.y;
CGFloat offset = -1;

CGRect newFrame = self.frame;
newFrame.size.height -= keyboardBounds.size.height -
((keyboardBounds.origin.y+keyboardBounds.size.height)
- (self.bounds.origin.y+self.bounds.size.height));

CGRect firstResponderFrame = [firstResponder convertRect:firstResponder.bounds toView:self];
if ( firstResponderFrame.origin.y + firstResponderFrame.size.height >= screenBounds.origin.y + screenBounds.size.height - keyboardBounds.size.height ) {
// Prepare to scroll to make sure the view is above the keyboard
offset = firstResponderFrame.origin.y + self.contentOffset.y;
if ( self.contentSize.height - offset < newFrame.size.height ) {
// Scroll to the bottom
offset = self.contentSize.height - newFrame.size.height;
} else {
if ( firstResponder.bounds.size.height < spaceAboveKeyboard ) {
// Center vertically if there's room
offset -= floor((spaceAboveKeyboard-firstResponder.bounds.size.height)/2.0);
}
if ( offset + newFrame.size.height > self.contentSize.height ) {
// Clamp to content size
offset = self.contentSize.height - newFrame.size.height;
}
}
}

// Shrink view's height by the keyboard's height, and scroll to show the text field/view being edited
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
self.frame = newFrame;

if ( offset != -1 ) {
[self setContentOffset:CGPointMake(self.contentOffset.x, offset) animated:YES];
}
[UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification*)notification {
if ( CGRectEqualToRect(priorFrame, CGRectZero) ) return;

// Restore dimensions to prior size
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
self.frame = priorFrame;
priorFrame = CGRectZero;
[UIView commitAnimations];
}

我遇到一个问题,当键盘开始隐藏时,scrollView 立即向上跳跃,因此它的原点位于窗口顶部上方。然后它会动画到正确的位置。

我发现只有当动画运行时 contentOffset 大于 0 时才会发生这种情况。

在调试时,我注意到当键盘关闭动画开始时,内容偏移量为 54。我发现如果我将此代码添加到keyboardWillHide的开头:

priorFrame.size.height -= self.contentOffset;

...这样就不会发生跳转,动画也能正常运行,但最终scrollView会太短。

有什么想法吗?

另请注意,我的 ScrollView 的正常框架是 (0, 44, 320, 416),因为它位于标题栏下方。

最佳答案

这似乎可能是 UIKit 中的一个错误,不确定。我通过动画 contentInset 而不是frame.size.height 找到了解决方案。你可以在我的项目分支 http://github.com/wordofchristian/TPKeyboardAvoiding 中看到它

关于iphone - 当 contentOffset 有值时,Scrollview 的位置在动画过程中跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6239697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com