animationControllerForPresentedController未调用

时间:2015-09-29 12:19:09

标签: ios objective-c

我正在尝试创建一个自定义动画来转换像新钱包应用程序中的控制器。

我有一个像这样的ViewController:

@interface ViewController () <UIViewControllerTransitioningDelegate>
@property (nonatomic, strong) UILabel *testLabel;
@property (nonatomic, strong) UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.transitioningDelegate = self;

    _testLabel = [[UILabel alloc] init];
    _testLabel.text = @"View 1";

    [self.view addSubview:_testLabel];



    [_testLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.height.equalTo(@50);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.mas_topLayoutGuide);
    }];

    _button = [[UIButton alloc] init];
    [_button addTarget:self action:@selector(push)forControlEvents:UIControlEventTouchUpInside];
    [_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_button setTitle:@"Show View" forState:UIControlStateNormal];
    [self.view addSubview:_button];

    [_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.and.height.equalTo(@50);
    }];
}

- (void)push {
    NSLog(@"Push controller");
    BackViewController *vc = [[BackViewController alloc] init];
    [self.navigationController presentViewController:vc animated:YES completion:nil];
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    TurnAnimationController *an = [[TurnAnimationController alloc] init];
    an.flipDirection = CEDirectionHorizontal;
    return an;
}

永远不会调用方法animationControllerForPresentedController,因此我的动画永远不会被执行。我没有看到问题?我正在将transitioningDelegate设置为自我?

有人有想法吗?

2 个答案:

答案 0 :(得分:13)

调用animationControllerForPresentedController的另一个要求是将true作为animated

中的presentViewController:animated:completion参数传递

如果你打电话

presentViewController(myVC, animated: false, completion: nil)

即使您设置了animationControllerForPresentedController,也不会调用transitioningDelegate

答案 1 :(得分:7)

而不是将self设置为委托

  

self.transitioningDelegate = self;

将新呈现的viewcontroller的transitioningDelegate设置为self,就像你的情况一样

  

BackViewController * vc = [[BackViewController alloc] init];   vc.transitioningDelegate = self;