【问题标题】:CATransition in Custom Segue自定义 Segue 中的 CATransition
【发布时间】:2011-12-04 21:43:20
【问题描述】:

我想知道是否可以在自定义 Segue 转换中使用 CATransition。我创建了我的 CustomSegueTransition.m 文件并在我希望发生转换的自定义 segue 类中引用它。

这是我的 CustomSegueTransition.m 文件的代码:

#import "CustomSegueTransition.h"
#import "QuartzCore/QuartzCore.h"

@implementation CustomSegueTransition

-(void) perform {

    // set up an animation for the transition the content
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.25];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.25];

    [UIView commitAnimations];
}

@end

它没有给我任何错误警告,但是当按下按钮导致 segue 转换时转换不会执行。我是否遗漏了 CATransition 代码中的某些内容,或者这不可能?

谢谢!

【问题讨论】:

    标签: iphone objective-c xcode4.2 caanimation


    【解决方案1】:

    @格兰特

    我没有完整的答案给你,但我注意到你从来没有调用一个新的视图,因此没有转换。我不知道您是针对推送、弹出还是模态进行拍摄,但这是您需要添加的代码类型:

    UIViewController *src = (UIViewController *) self.sourceViewController;
    UIViewController *dst = (UIViewController *) self.destinationViewController;   
    [src presentModalViewController:dst animated:YES];
    

        [src presentViewController:<#(UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>];
    

    我相信设置自定义动画还有其他问题,尽管我对解决方案感兴趣,但我没有答案。

    【讨论】:

    • presentModalViewController:animated: 已弃用,因此在这种情况下应使用presentViewController:animated:completion:
    【解决方案2】:

    不确定你在尝试什么,但我确实发现了问题。

    您创建了一个过渡动画,但从未使用过它。您需要在要动画的视图层上添加动画。

    另外,如果您要使用较低级别的 CoreAnimation 功能,则无需使用 [UIView beginAnimation]...[UIView commitAnimation]。

    如果需要,使用 [CATransaction begin]...[CATransaction commit]。

    【讨论】:

      【解决方案3】:

      这是完整的代码

      -(void)perform {
      
      __block UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
      __block UIViewController *destinationController = (UIViewController*)[self destinationViewController];                    
      
      CATransition* transition = [CATransition animation];
      transition.duration = .25;
      transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
      transition.type = kCATransitionMoveIn; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
      transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom  
      
      [sourceViewController.navigationController.view.layer addAnimation:transition
                                                  forKey:kCATransition];
      
      [sourceViewController.navigationController pushViewController:destinationController animated:NO];
      
       }
      

      【讨论】:

        【解决方案4】:

        导入 QuartzCore 框架。

        - (void)pushViewWithCustomTranstion {
            CATransition* transition = [CATransition animation];
            transition.duration = 0.4f;
            transition.type = kCATransitionMoveIn;
            transition.subtype = kCATransitionFromRight;
            [self.navigationController.view.layer addAnimation:transition
                                                        forKey:kCATransition];
            YourDestinataionController *yourVC = [[YourDestinataionController alloc] init];
            [self.navigationController pushViewController:yourVC animated:NO];
        }
        

        将您的视图控制器推送到具有自定义过渡效果的导航控制器中是简单易用的代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-23
          • 2017-12-06
          • 2012-06-02
          相关资源
          最近更新 更多