iphone - 正确使用 transitionFromViewController :toViewController:duration:options:animations:completion:

标签 iphone objective-c ios ipad

我似乎找不到关于如何正确使用 transitionFromViewController:toViewController:duration:options:animations:completion: 的好例子。

这是正确的吗? (假设我想用另一个 VC 交换)

// Assume fromVC and toVC view controllers are defined and fromVC is already added as a child view controller
[self addChildViewController:toVC];

[self transitionFromViewController:fromVC toViewController:toVC duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:NULL completion:^(BOOL finished) {
    [fromVC willMoveToParentViewController:nil];
    [fromVC removeFromParentViewController];
    [toVC didMoveToParentViewController:self];
}];

文档并不清楚何时调用什么:

The addChildViewController: method calls the willMoveToParentViewController: method of the view controller to be added as a child before adding it, but it does not call the didMoveToParentViewController: method. The container view controller class must call the didMoveToParentViewController: of the child view controller after the transition to the new child is complete or, if there is no transition, immediately after calling the addChildViewController: method.

Likewise, it is is the responsibility of the container view controller to call the willMoveToParentViewController: method before calling the removeFromParentViewController: method. The removeFromParentViewController: method calls the didMoveToParentViewController: method of the child view controller.

另一件事是,在这种情况下如何使用动画 block ?请注意,在上面的代码中,我只是放置了 NULL。 (我对 block 本身很熟悉,我只是不确定在这个里面放什么)

最佳答案

我过去也做过类似的事情。但是,我会将 -willMoveToParentViewController: 移到完成 block 之外,因为该 View Controller 应该在移动之前得到通知(即,在完成 block 运行时,fromVC已经将其父 VC 设置为 nil。所以总而言之,是这样的:

[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];

[self transitionFromViewController:fromVC toViewController:toVC duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{} completion:^(BOOL finished) {
    [fromVC removeFromParentViewController];
    [toVC didMoveToParentViewController:self];
}];

根据方法文档,就动画而言,永远不要将此参数设置为 NULL。如果您没有要设置动画的 View 属性,则只需向它传递一个空 block ^{}。基本上,此参数用于在过渡期间为 View 层次结构中的 View 属性设置动画。动画属性列表可以在 the UIView documentation 中找到。在“动画”标题下。例如,假设您不希望由 fromVC 处理的整个 View 交叉融合,而只希望其 View 层次结构中名为 subview1 的一个 subview 淡出。您可以使用动画 block 执行此操作:

[self addChildViewController:toVC];
[fromVC willMoveToParentViewController:nil];

[self transitionFromViewController:fromVC 
                  toViewController:toVC
                          duration:0.3
                           options:UIViewAnimationOptionTransitionNone
                        animations:^{
                                       [subview1 setAlpha:0.0];
                                   }
                        completion:^(BOOL finished) {
                                       [fromVC removeFromParentViewController];
                                       [toVC didMoveToParentViewController:self];
                                   }];

关于iphone - 正确使用 transitionFromViewController :toViewController:duration:options:animations:completion:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8453653/

相关文章:

iphone - 如何在 iPhone 的 UIWebView 中正确保存/显示换行符?

ios - libsqlite3.dylib 框架意外从 xcode 中删除

objective-c - 查找某个div的xpath

ios - 如何防止 iOS 应用在更改相机权限后重置?

ios - 如何创建一个不是应用程序但在其他项目中可见的Xcode项目?

iphone - IOS - 意外崩溃,没有崩溃信息

iphone - iCloud:KeyValue 存储还是文档存储?

iphone - UIPinchGestureRecognizer 和 UIImageView 的问题

iphone - 从手机获取 safari 历史记录

ios - 将搜索字符串从 UISearchBar 转换为 MK MapView 地址