gpt4 book ai didi

objective-c - 动画 contentInset 到 UITableView 也会动画行的 subview 的框架

转载 作者:太空狗 更新时间:2023-10-30 03:53:25 24 4
gpt4 key购买 nike

我有一个带有自定义单元格的 UITableView。自然地,在 cellForRowAtIndexPath 中,我尝试将旧单元格出队并重新使用它。每个单元格都有一个自动调整 View 的层次结构:当我在 cellForRowAtIndexPath 中设置它的内容时,我设置了顶 View 的框架,并且它的 subview 相应地改变了它们的框架。请注意,行高也是动态的。当我滚动表格时(具有不同内容和框架的行),一切都很好。但是我在与该表相同的 View 中也有一个文本字段,因此我需要滚动表的内容以补偿显示/隐藏的键盘。我为此设置了 contentInset 属性的动画:

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
NSTimeInterval animationDuration = [[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationCurve animationCurve = [[aNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];

[UIView setAnimationCurve:animationCurve];
[UIView animateWithDuration:animationDuration animations:^{
messagesTable.contentInset = UIEdgeInsetsMake(0, 0, kHSTableBottomInset, 0);
messagesTable.scrollIndicatorInsets = UIEdgeInsetsZero;
}];
}

它也很好用,但有一个有趣的故障:当键盘被隐藏并且 contentInset 动画恢复正常时(kHSTableBottomInset 是一个很小的值以保持边距),表格重新加载将从上方滚动以显示的单元格。问题是这个重新加载也是在动画 block 内完成的!结果,当我在 cellForRowAtIndexPath 中更改出队的 subview 框架(具体来说,宽度)(称为部分或重新加载)时,此更改也是动画的,并且在单元格向下滚动以查看时可见。我可以避免这种行为吗?

最佳答案

我终于找到了解决方案:帧设置代码可以像这样从动画中排除:

[CATransaction begin];
[CATransaction setDisableActions:YES];
mainDataView.frame = rect;
[CATransaction commit];

[UIView setAnimationsEnabled:NO];
mainDataView.frame = rect;
[UIView setAnimationsEnabled:YES];

在这里找到答案:How can I exclude a piece of code inside a core animation block from being animated?

关于objective-c - 动画 contentInset 到 UITableView 也会动画行的 subview 的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9743259/

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