Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

objective c - UIViewControllerTransitioningDelegate method not called in iOS 7

I have created a custom transition animation for a modal view controller by implementing the methods in the UIViewControllerTransitioningDelegate protocol.

In iOS 8 and 9 the methods are called normally and the transition works. However, in iOS 7, the animationControllerForPresentedController:presentingController:sourceController: method never gets called. The animationControllerForDismissedController: method still gets called normally.

#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

In the modal view controller (i.e. the "presented controller") I have the following in its -viewDidLoad method:

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;

Setting the modalPresentationStyle doesn't seem to make any difference in any version of iOS. The method that isn't being called does say that it's available in iOS 7 so I'm not sure why it isn't being called.

The modal view controller is being presented with the following code in the presenting view controller:

[self presentViewController:self.modalViewController
                   animated:YES
                 completion:nil];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If anyone comes across this in later years, and you're using Swift 3, make sure that your call isn't to "animationControllerForPresentedController".

As of Xcode 8.1, the compiler doesn't automatically recognize this problem and thus doesn't offer to convert the code to modern syntax.

The above code will compile but it won't be a protocol implementation. It will just be a custom, uncalled function. (Such are the hazards of optional protocol methods, and the myriad problems with Xcode's autocomplete.)

So, make sure you implement the protocol with Swift 3 syntax:

func animationController(forPresented presented: UIViewController,
        presenting: UIViewController,
        source: UIViewController)
        -> UIViewControllerAnimatedTransitioning?
{
    // ... return your cached controller object here.
}

And remember to set the presentation style on the View Controller to be presented:

self.modalPresentationStyle = .custom

And the delegate:

self.transitioningDelegate = self // or wherever

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...