我通过实现 UIViewControllerTransitioningDelegate
协议(protocol)中的方法,为模态视图 Controller 创建了自定义过渡动画。
在 iOS 8 和 9 中,方法被正常调用并且转换有效。但是,在 iOS 7 中,永远不会调用 animationControllerForPresentedController:presentingController:sourceController:
方法。 animationControllerForDismissedController:
方法仍然被正常调用。
#import "MyModalTransitioningDelegate.h"
#import "MyModalFadeTransition.h"
@implementation MyModalTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source
{
return [[MyModalFadeTransition alloc] init];
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
return [[MyModalFadeTransition alloc] init];
}
@end
在模态视图 Controller (即“呈现的 Controller ”)中,我在其 -viewDidLoad
方法中具有以下内容:
self.modalTransitionDelegate = [[OTModalTransitioningDelegate alloc] init]; // This is a custom strong private property due to `tranisitioningDelegate` being a weak property.
self.transitioningDelegate = self.modalTransitionDelegate;
self.modalPresentationStyle = UIModalPresentationCustom;
设置 modalPresentationStyle
似乎在任何版本的 iOS 中都没有任何区别。未被调用的方法确实表明它在 iOS 7 中可用,所以我不确定为什么它没有被调用。
模态视图 Controller 在呈现 View Controller 中使用以下代码呈现:
[self presentViewController:self.modalViewController
animated:YES
completion:nil];
最佳答案
如果以后有人遇到这个问题,并且您使用的是 Swift 3,请确保您的调用不是“animationControllerForPresentedController
”。
从 Xcode 8.1 开始,编译器不会自动识别此问题,因此不会提供将代码转换为现代语法的功能。
上面的代码可以编译,但它不是协议(protocol)实现。它只是一个自定义、未调用的函数。 (这就是可选协议(protocol)方法的危险,以及 Xcode 自动完成的无数问题。)
因此,请确保使用 Swift 3 语法实现协议(protocol):
func animationController(forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController)
-> UIViewControllerAnimatedTransitioning?
{
// ... return your cached controller object here.
}
并且记得在要呈现的 View Controller 上设置呈现样式:
self.modalPresentationStyle = .custom
委托(delegate)人:
self.transitioningDelegate = self // or wherever
关于ios - iOS 7 中未调用 UIViewControllerTransitioningDelegate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32959085/