【问题标题】:UIView transitionWithView is smooth when using changes outside of animation block使用动画块之外的更改时,UIView transitionWithView 是平滑的
【发布时间】:2016-10-30 08:46:22
【问题描述】:

我正在重新加载 UITableview 的数据,因为行数、节数和行高发生了巨大变化。为了软化过渡,我在点击按钮时选择了下面的过渡。如果我将 reloadData 和/或内容偏移代码放在动画块内,结果是“hacky”/“stuttering”。如果我将视图更改放在动画之前,它就像做梦一样。 为什么这行得通?我是否使用了 transitionWithView (iOS 10.1) 的错误行为。这应该如何正确完成?

[self.tableView reloadData];
CGPoint bottomOffset = CGPointMake(0, [self.tableView contentSize].height + [self.tableView contentInset].bottom - self.tableView.frame.size.height);
[self.tableView setContentOffset:bottomOffset animated:NO];

[UIView transitionWithView: self.tableView
                          duration: 0.35f
                           options: UIViewAnimationOptionTransitionCrossDissolve
                        animations: ^(void)
                        {
                        }
                        completion: nil];

【问题讨论】:

    标签: ios animation uiview reloaddata


    【解决方案1】:

    transitionWithView 常用于改变屏幕的整体视图,而不是显示部分部分的变化。 就像苹果说的:

    过渡动画将一个视图控制器的内容交换为另一个视图控制器的内容。

    所以在大多数情况下,建议使用简单的动画

    [UIView animateWithDuration:(NSTimeInterval) animations:^(void)animations completion:^(BOOL finished)completion]
    

    或者还有另一种方法可以为您的视图设置动画

    [UIView beginAnimations:(nullable NSString *) context:(nullable void *)]
    //place your view changes here to animate it
    //......
    [UIView commitAnimations]
    

    tableView还有一个方法

    [tableView insertRowsAtIndexPaths:(nonnull NSArray<NSIndexPath *> *) withRowAnimation:(UITableViewRowAnimation)]
    

    您可以通过多种选项来选择动画将新数据添加到 tableView。所以基本上,您将新数据添加到您在表格视图中显示的对象数组中,然后调用此方法来显示您所做的更改。 另一方面,您可以尝试其他方法

    [tableView beginUpdates];
    //make some changes
    [tableView endUpdates];
    

    在上述两种情况下,您不需要调用 [tableView reloadData],因为它们都自己工作。

    【讨论】:

    • 感谢您的回答。然而问题是 animateWithDuration 根本无法顺利工作(可能由于内容偏移而跳起来并拥有,而不是像 crossdisolve 那样只是淡入和淡出。如果我使用transitionWithView,并将视图设置为tableview,或者只是控制器视图,当排除动画块中的更改时效果很好。有什么想法吗?
    • @damcify 使用了一些新方法进行了编辑。希望其中一些可以帮助您解决问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    相关资源
    最近更新 更多