将contentInset动画到UITableView也可以设置行的子视图帧的动画

Vla*_*hin 5 core-animation objective-c uitableview

我有一个带自定义单元格的UITableView.当然,在cellForRowAtIndexPath中,我尝试将旧单元出列并重新使用它.每个单元格都有一个自动调整视图的层次结构:当我在cellForRowAtIndexPath中设置其内容时,我设置了顶视图的框架,并且其子视图相应地更改了它们的框架.请注意,行高也是动态的.当我只滚动表格(包含不同内容和帧的行)时,这一切都很好.但是我在该表的同一视图中也有一个文本字段,所以我需要滚动表的内容来补偿显示/隐藏的键盘.我为它设置了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;
    }];
}
Run Code Online (Sandbox Code Playgroud)

它也运行良好,但是有一个有趣的故障:当键盘被隐藏并且contentInset被动画恢复正常(kHSTableBottomInset是一个保持边距的小值)时,表重新加载将从上面滚动以显示的单元格.问题是这个重新加载也是在动画块中完成的!因此,当我在cellForRowAtIndexPath(被称为部件或重新加载)中更改出列的子视图帧(具体地,宽度)时,此更改也会被动画化,并且当单元格向下滚动以查看时,它也是可见的.我可以避免这种行为吗?

Vla*_*hin 4

我终于找到了解决方案:可以将帧设置代码从动画中排除,如下所示:

[CATransaction begin];
[CATransaction setDisableActions:YES];
mainDataView.frame = rect;
[CATransaction commit];
Run Code Online (Sandbox Code Playgroud)

或者

[UIView setAnimationsEnabled:NO];
mainDataView.frame = rect;
[UIView setAnimationsEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

在这里找到答案:如何将核心动画块内的一段代码排除在动画之外?