【问题标题】:ViewController transition视图控制器过渡
【发布时间】:2012-07-28 04:37:09
【问题描述】:

我有代码:

ListViewController * listViewController = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil];    

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self viewWillDisappear:YES];
[listViewController viewWillAppear:YES];

self.view.hidden = YES;
listViewController.view.hidden = NO;


[self viewDidDisappear:YES];
[listViewController viewDidAppear:YES];
[UIView commitAnimations];    

但它不起作用,listViewController不显示(请有人告诉我这个问题的解决方案吗?

【问题讨论】:

    标签: iphone objective-c ios transition


    【解决方案1】:

    尝试类似:

    UIViewAnimationOptions ops = UIViewAnimationOptionTransitionFlipFromRight;
    NSArray *temp = [[NSBundle mainBundle] loadNibNamed:@"NameOfNib" owner:self options:nil];
    UIView* newView = [[temp objectAtIndex:0] view];
    [UIView transitionFromView:self.view toView:newView duration:1.5 options:ops completion:nil];
    self.view = newView; //Lets you control the new view from the current controller (you might want to save a reference to the old one if you need to change back)
    

    正如 meronix 所说,Apple 不鼓励在较新的 iOS 版本中使用基于非块的动画。上述方法是“认可”的方法。

    您知道,viewWillAppearviewDidDisappear 和类似的方法并不是您调用来使视图执行操作的方法。当这些事情发生时,它们会被自动调用。

    您的代码有一些误解;我在下面评论过他们

    //This looks fine (depending on what is in the nib)
    ListViewController * listViewController = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil]; 
    
    //Normally I use these to move things around, not change the view
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
    
    [self viewWillDisappear:YES]; //These two methods aren't things that you call
    [listViewController viewWillAppear:YES];
    
    self.view.hidden = YES; //If you're flipping or otherwise moving a view out of 
    listViewController.view.hidden = NO; //sight then you don't need to hide/unhide views
    
    [self viewDidDisappear:YES]; //Same as above, you don't call these
    [listViewController viewDidAppear:YES];
    [UIView commitAnimations]; 
    

    【讨论】:

    • 感谢您的澄清!!)我仍在处理这个问题)我是 Objective-c 的新手((
    • 如果您需要一些教程,请查看raywenderlich.com。 SO 上几乎每个人都强烈推荐它们。
    【解决方案2】:

    删除不必要的代码,只写这个......

    ListViewController * listViewController = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil];    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
    [self.view addSubview:listViewController.view];
    [UIView commitAnimations];
    

    【讨论】:

    • 当我使用此代码时,我当前的视图(它是地图视图)仅在现场旋转..((我需要将当前视图(IBOutlet MKMapView * 地图)更改为 listView。
    • 它会翻转 listViewController.view 您可以翻转任何视图,只需将 listViewcontroller.view 替换为您的视图。因为在你的问题中你只是要求翻转。
    【解决方案3】:

    它不能工作!

    您只需创建和分配一个 UIViewController,但永远不要将其推送到任何堆栈上,或将其视图添加到可见视图。

    当您将 listViewController.view.hidden 设置为 no 时,您不会神奇地将其显示在屏幕上:您需要将其视图添加到已经在屏幕上的视图(或窗口)中...

    ps beginAnimation 已弃用:改用块动画...

    【讨论】:

    • beginAnimations 没有被弃用...我认为它只是不鼓励。
    • 不,你是对的,苹果只是说:“在 iOS 4.0 及更高版本中不鼓励使用这种方法。你应该使用基于块的动画方法来指定你的动画。”和:“在 iOS 4 及更高版本中,使用基于块的动画方法。(推荐)”
    • 好的,我很担心,不得不检查一下,因为我的一些旧应用程序使用 beginAnimations,我不想被 iOS 6 搞砸。另外,你会看到我的回答确实如此推荐基于块的方法。
    • :-) - 正如我所说,你是对的,推荐只是意味着苹果迟早会将其签署为折旧,也许在遥远的将来(希望如此)......无论如何,我喜欢 beginAnimation 方法,但只是因为我习惯了它们......然后我强迫自己使用块,我现在喜欢它们......
    • 我只是一个初学者,想和有经验的开发者交流)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多