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

时间:2012-11-06 16:38:26

标签: ios animation uiscrollview

以下是正在发生的事情的截图。

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

橙色是滚动视图框架。

键盘已启动...滚动视图可以很好地滚动内容。

enter image description here

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

enter image description here

虽然滚动视图(橙色)似乎是正确的高度,但内容现在已经上升了大约100px(绿色)。

我不知道造成这种情况的原因是 - 它是一个ios bug还是什么(它也发生在我的设备上)。

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

- (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];
}

任何帮助或指导都会很棒,谢谢!它为什么这样做?这是一个错误 - 它是否隐藏在我的代码中的某个地方?如果是的话,我找不到它。这与我正在观看动画的方式有误吗?

1 个答案:

答案 0 :(得分:5)

这是我对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的内容偏移量