【问题标题】:left to right transition in view based application on moving from one view controller to another从一个视图控制器移动到另一个视图控制器时,在基于视图的应用程序中从左到右转换
【发布时间】:2012-06-01 07:00:16
【问题描述】:
MyView *view=[[[MyView alloc] init] autorelease];
[view setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:view animated:YES];

我在从一个视图控制器移动到另一个视图控制器时使用了翻转过渡。但我的要求是从左到右过渡。请帮我解决这个问题,谢谢。 这是我的代码:

【问题讨论】:

  • 你不能用标准的modalTransition来做到这一点。使用 UINavigationControllers。

标签: iphone objective-c ios xcode


【解决方案1】:

需要添加QuartzCore Framework,然后导入#import <QuartzCore/QuartzCore.h>

MyView *next = [[MyView alloc] init];
CATransition *animation = [CATransition animation];
[self presentModalViewController:next animated:NO];
[animation setDuration:0.40];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
//[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[[next.view layer] addAnimation:animation forKey:@"SwitchToView1"];
[next release];

【讨论】:

  • 嗨赫克托,你的代码工作正常,但它满足我的一半要求。我的要求是新视图来自左侧,当前视图从右侧移动。如果你可以这样做,请帮我看看对你。
  • @AlokSrivastava 我找不到你。请详细说明
  • Hector 我使用此代码返回。 [self.parentViewControllerdismissModalViewControllerAnimated:YES];现在我想要过渡风格应该是从右到左。正如你回答我的那样。请记住我的项目是基于视图的应用程序。请告诉我,我在等待您的回复
  • @AlokSrivastava 你确定你不是在寻找 UINavigationController 吗?这些转换是其原生行为之一。
  • @MDT 是的,我不是在寻找 UINavigationController,因为我几乎完成了我的项目,只有这种过渡风格是休息。
【解决方案2】:

我假设您正在从视图控制器 1 中关闭视图控制器 2。在视图控制器 2 中,您正在使用它

[self  dismissModalViewControlleAnimated: NO];

现在在第一个视图控制器中,导入“QuartzCore”标头并使用代码添加 viewWillAppear 方法

#import <QuartzCore/QuartzCore.h>

(void)viewWillAppear:(BOOL)animated {

    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromLeft];
    
    [animation setDuration:0.50];
    [animation setTimingFunction:
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    
    [self.view.layer addAnimation:animation forKey:kCATransition];

}

【讨论】:

    【解决方案3】:
    - (void)slideView:(UIView*)view direction:(BOOL)isLeftToRight {
        CGRect frame = view.frame;
        frame.origin.x = (isLeftToRight) ? -320 : 320;
        view.frame = frame;
    
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        frame.origin.x = 0;
        view.frame = frame;
        [UIView commitAnimations];
    }
    

    【讨论】:

    • 如果你想实现“自定义modalTransition”,我想这是一个好方法
    【解决方案4】:

    您为什么不使用UINavigationController 并推送UIViewController 而不是模态显示它?

    编辑:

    要将基于视图的应用更改为UINavigationController,只需将其添加到您的 AppDelegate

        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        UINavigationController *nvcontrol = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        [self.window addSubview:nvcontrol.view];
    

    【讨论】:

    • 我的整个应用都基于视图,所以我不能使用基于导航的应用
    • 我如何将基于视图的应用程序更改为基于导航的应用程序。对我来说不是很麻烦
    • 通常当人们想要这样的左右动画时,他们会模仿导航。如果是这样,那么切换真的没那么难。
    • 查看我的编辑以使您的基于视图的应用程序成为基于导航的应用程序
    【解决方案5】:

    是的,您应该通过导航控制器执行此操作。

    这里有一些代码可以帮助你,如果你想通过导航控制器来做的话。

    在 AppDelegate.h 中:

    @interface AppDelegate : UIResponder <UIApplicationDelegate,UINavigationControllerDelegate>{
    
        UINavigationController *navController;
    }
    

    在 AppDelegate.m 中:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        // ViewController is your FirstViewController which loads over Window.
    
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
        self.window.rootViewController = _viewController;
        navController =[[UINavigationController alloc] initWithRootViewController:_viewController];
        self.window.rootViewController = navController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    然后在您的 ViewController.m 中,在您的按钮点击功能中,您可以这样做:

    NextViewController *nextController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
    [self.navigationController pushViewController:nextController animated:YES];
    

    仅此而已。

    【讨论】:

      【解决方案6】:

      试试这个

      [self.navigatationController pushViewController:view animated:YES];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多