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

发布于 2024-09-07 09:47:06 字数 530 浏览 6 评论 0原文

CATransitions 可用于在向下钻取时为导航控制器中的过渡设置动画。但是,当使用后退按钮和导航控制器(返回)时,动画仍然滑出。有谁知道如何将 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];

CATransitions can be used to animate transitions in Navigation Controllers when drilling down. However when using Back button og Navigation Controller (going back up) animation is still slide out. Does anyone know how to attach CATransition to the Back button of Navigation Controller? thanks.

Code used to animate when "drilling down":

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

二货你真萌 2024-09-14 09:47:08

不需要创建自定义按钮...您可以简单地执行以下操作:

- (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

There is no need to create a custom button... You can simply do something like this:

- (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];
}

Edit: you must add the quartz framework first in order to use CATransition

§对你不离不弃 2024-09-14 09:47:08

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

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

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

    CATransition *transition = [CATransition 动画];
    过渡.持续时间 = 1;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    转换类型 = kCATransitionPush;
    过渡.子类型 = kCATransitionFromBottom;
    过渡.委托=自我;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController popViewControllerAnimated:YES];
    

For adding animation to the back button, you have to create your own back button, and on the back button action specify the animation you want.

  1. Adding a back button to the navigation bar: add this line to your viewDidLoad method:

    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(back)];
    
  2. In the back method add this code:

    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];
    
陪你到最终 2024-09-14 09:47:08

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

I'm not sure I understand the question. UINavigationController already uses a slide transition when switching views. If you want to attach a custom transition to the back button, you're going to have to create a custom back button and connect it to an action that will run your CATransition code.

贵在坚持 2024-09-14 09:47:08

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

[self.navigationController popViewControllerAnimated:YES];

If you want animation like back button of navigation controller bar just put following code in your action.

[self.navigationController popViewControllerAnimated:YES];

素年丶 2024-09-14 09:47:08

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)

Swift 3 version of shadowfax answer:

    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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文