从一个视图控制器移动到另一个视图控制器时,在基于视图的应用程

时间:2012-06-01 07:00:16

标签: iphone objective-c ios xcode

MyView *view=[[[MyView alloc] init] autorelease];
[view setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:view animated:YES];

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

6 个答案:

答案 0 :(得分:5)

您需要添加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];

答案 1 :(得分:3)

我认为您正在从视图控制器1中解除视图控制器2.在视图控制器2中,您正在使用此

  

[self dismissModalViewControlleAnimated:NO];

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

  

#import <QuartzCore/QuartzCore.h>

     

(void)viewWillAppear:(BOOL)动画{

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

我希望这段代码可以为您提供很多帮助。

答案 2 :(得分:2)

为什么不使用UINavigationController并推送UIViewController而不是以模态方式展示?

编辑:

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

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

答案 3 :(得分:2)

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

答案 4 :(得分:2)

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

如果您想通过导航控制器执行此操作,可以使用以下代码来帮助您。

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

就是这样。

答案 5 :(得分:1)

试试这个

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