Ios 动画滚动视图高度-内容在动画之前突然跳起来

Ios 动画滚动视图高度-内容在动画之前突然跳起来,ios,animation,uiscrollview,Ios,Animation,Uiscrollview,下面是一些正在发生的事情的截图 绿色背景是scrollview中的视图(即标记“内容大小”和“内容偏移”) 橙色是滚动视图框 键盘打开了。。。滚动视图很好地滚动了内容。 现在我单击文本字段,键盘开始隐藏 尽管滚动视图(橙色)看起来高度合适,但内容现在已经上升了约100px(绿色) 我不知道是什么原因导致了这种情况——是ios错误还是什么(它也发生在我的设备上) 这是根据键盘显示/隐藏的时间调整视图大小的代码 - (void)keyboardWillShow:(NSNotification *

下面是一些正在发生的事情的截图

绿色背景是scrollview中的视图(即标记“内容大小”和“内容偏移”)

橙色是滚动视图框

键盘打开了。。。滚动视图很好地滚动了内容。

现在我单击文本字段,键盘开始隐藏

尽管滚动视图(橙色)看起来高度合适,但内容现在已经上升了约100px(绿色)

我不知道是什么原因导致了这种情况——是ios错误还是什么(它也发生在我的设备上)

这是根据键盘显示/隐藏的时间调整视图大小的代码

- (void)keyboardWillShow:(NSNotification *)notif {
    [self.mainScrollView setAutoresizesSubviews:NO];
    // get the scroll view frame size
    CGRect frame = [self.mainScrollView frame];
    frame.size.height = self.view.frame.size.height-216;

    [UIView beginAnimations:@"scrollViewAnimations" context:nil];
    [UIView setAnimationDuration:0.3];
    [self.mainScrollView setFrame:frame];
    [UIView commitAnimations];

    // add tap gesture recognizer
    _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_dismissKeyboard:)];
    [_tap setDelegate:self];
    [self.view addGestureRecognizer:_tap];
}

- (void)keyboardWillHide:(NSNotification *)notif {
    CGRect frame = [self.mainScrollView frame];
    frame.size.height = self.view.frame.size.height;

    [UIView beginAnimations:@"scrollViewAnimations" context:nil];
    [UIView setAnimationDuration:0.3];
    [self.mainScrollView setFrame:frame];
    [UIView commitAnimations];

    [self.view removeGestureRecognizer:_tap];
}

任何帮助或指导都会很好,谢谢!为什么要这样做?它是一个bug吗?它是埋在我代码中的什么东西吗?如果是的话,我找不到。这是我制作视图动画时的错误吗?

以下是我对RubyMotion中类似问题的解决方案:

def keyboardWillShow(notification)
  point = searchBar.convertPoint(searchBar.frame.origin, toView:tableView)
  tableView.setContentOffset(point, animated: true)
end

def keyboardWillHide(notification)
  info = notification.userInfo
  duration = info.objectForKey(UIKeyboardAnimationDurationUserInfoKey)

  UIView.beginAnimations(nil, context:nil)
  UIView.setAnimationDuration(duration)
  tableView.setContentOffset([0,0], animated: false)
  UIView.commitAnimations
end

这里的想法是,您并不真的想改变帧,而是想调整UIScrollView的内容偏移量。以下是我对RubyMotion中类似问题的解决方案:

def keyboardWillShow(notification)
  point = searchBar.convertPoint(searchBar.frame.origin, toView:tableView)
  tableView.setContentOffset(point, animated: true)
end

def keyboardWillHide(notification)
  info = notification.userInfo
  duration = info.objectForKey(UIKeyboardAnimationDurationUserInfoKey)

  UIView.beginAnimations(nil, context:nil)
  UIView.setAnimationDuration(duration)
  tableView.setContentOffset([0,0], animated: false)
  UIView.commitAnimations
end

这里的想法是,您实际上不想改变帧,而是想调整UIScrollView的内容偏移量,这很有趣。我也许能像这样解决一些问题,而不是我是怎么做的我会让你知道我是怎么做的。+1用于将动画的持续时间设置为键盘动画的持续时间。好的,我已经了解了一些你正在做的事情。我保留了“向上滚动视图”代码。然后在隐藏键盘时,我将内容高度设置为
+=216
,将滚动视图帧设置为视图高度(无动画),然后将内容偏移设置为
{0,0}
。这很有趣。我也许能像这样解决一些问题,而不是我是怎么做的我会让你知道我是怎么做的。+1用于将动画的持续时间设置为键盘动画的持续时间。好的,我已经了解了一些你正在做的事情。我保留了“向上滚动视图”代码。然后在隐藏键盘时,我将内容高度设置为
+=216
,将滚动视图帧设置为视图高度(无动画),然后将内容偏移设置为
{0,0}