导航控制器中的后退按钮动画

时间:2010-06-30 04:20:09

标签: iphone navigationcontroller

CATransitions可用于在向下钻取时为导航控制器中的过渡设置动画。但是,当使用后退按钮og导航控制器(返回)时,动画仍然会滑出。有谁知道如何将CATransition附加到导航控制器的后退按钮?感谢。

用于“向下钻取”时动画的代码:

CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromTop;
transition.delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:nil];

5 个答案:

答案 0 :(得分:7)

无需创建自定义按钮......您可以执行以下操作:

- (void)viewWillDisappear:(BOOL)animated
{
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
        // back button was pressed.  We know this is true because self is no longer
        // in the navigation stack.  
        CATransition *transition = [CATransition animation];
        [transition setDuration:0.75];
        [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [transition setType:@"oglFlip"];
        [transition setSubtype:kCATransitionFromLeft];
        [transition setDelegate:self];
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
    }

    [super viewWillDisappear:animated];
}

编辑:您必须先添加石英框架才能使用CATransition

答案 1 :(得分:6)

要将动画添加到后退按钮,您必须创建自己的后退按钮,并在后退按钮操作中指定所需的动画。

  1. 在导航栏中添加后退按钮:将此行添加到viewDidLoad方法中:

    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(back)];
    
  2. 在back方法中添加以下代码:

    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromBottom;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController popViewControllerAnimated:YES];
    

答案 2 :(得分:0)

我不确定我理解这个问题。切换视图时UINavigationController已使用幻灯片切换。如果要将自定义转换附加到后退按钮,则必须创建自定义后退按钮并将其连接到将运行CATransition代码的操作。

答案 3 :(得分:0)

如果您想要像导航控制器栏的后退按钮这样的动画,只需在您的操作中添加以下代码即可。

[self.navigationController popViewControllerAnimated:YES];

答案 4 :(得分:0)

Swift 3版本的shadowfax回答:

    let transition:CATransition = CATransition.init()
    transition.duration = 0.75
    transition.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition.type = "oglFlip"
    transition.subtype = kCATransitionFromRight
    //transition.delegate = self // not needed
    self.navigationController?.view.layer.add(transition, forKey:nil)