如何使用核心动画创建导航控制器推送效果?

时间:2010-01-04 13:44:15

标签: objective-c iphone-sdk-3.0 uiviewcontroller core-animation

我正在创建一个应用程序,我想实现推送效果但不使用导航控制器。有人知道核心动画源代码来实现这一目标吗?我有一个像这样的视图层次结构。一个主控制器,它有一个带有4个按钮的标题图像。我的意思是它将一直显示在屏幕前面的任何视图。所以我使用添加子视图方法。现在在某些视图中我想要在桌面视图单元格上进行操作时进一步导航,但是无法弄清楚如何实现该导航动画?

这是添加子视图的代码....

-(IBAction)ShowDashBoardContentView
{
if(ButtonDashBoard.selected==YES)
    [ButtonDashBoard setSelected:YES];
else
{
    [ButtonDashBoard setSelected:YES];
    [ButtonFreebies setSelected:NO];
    [self.FreebiesViewMainObject.view removeFromSuperview];
    [ButtonConnect setSelected:NO];
    [self.ConnectContentViewObject.view removeFromSuperview];
    [ButtonDeals setSelected:NO];
    [self.DealsViewMainObject.view removeFromSuperview];
}
if(DashBoardContentView==nil)
{
    DashBoardContent *vController =[[DashBoardContent alloc] initWithNibName:@"DashBoardContent" bundle:[NSBundle mainBundle]];
    vController.view.frame=CGRectMake(0, 90, 320, 370);
    self.DashBoardContentView=vController;
    [vController release];
}
[self.DashBoardContentView viewWillAppear:YES];
[self.view addSubview:[self.DashBoardContentView view]];
[self BringSubviewsToFront];
}

-(IBAction)ShowConnectContentView
{
if(ButtonConnect.selected==YES)
    [ButtonConnect setSelected:YES];
else
{
    [ButtonConnect setSelected:YES];
    [ButtonFreebies setSelected:NO];
    [self.FreebiesViewMainObject.view removeFromSuperview];
    [ButtonDashBoard setSelected:NO];
    [self.DashBoardContentView.view removeFromSuperview];
    [ButtonDeals setSelected:NO];
    [self.DealsViewMainObject.view removeFromSuperview];
}
if(ConnectContentViewObject==nil)
{
    ConnectContentView *vController =[[ConnectContentView alloc] initWithNibName:@"ConnectContentView" bundle:[NSBundle mainBundle]];
    vController.view.frame=CGRectMake(0, 90, 320, 370);
    self.ConnectContentViewObject=vController;
    [vController release];
}
[self.ConnectContentViewObject viewWillAppear:YES];
[self.view addSubview:[self.ConnectContentViewObject view]];
[self BringSubviewsToFront];
}

-(IBAction)ShowDealsView
{
if(ButtonDeals.selected==YES)
    [ButtonDeals setSelected:YES];
else
{
    [ButtonDeals setSelected:YES];
    [ButtonFreebies setSelected:NO];
    [self.FreebiesViewMainObject.view removeFromSuperview];
    [ButtonDashBoard setSelected:NO];
    [self.DashBoardContentView.view removeFromSuperview];
    [ButtonConnect setSelected:NO];
    [self.ConnectContentViewObject.view removeFromSuperview];
}

if(DealsViewMainObject==nil)
{
    DealsViewMain *vController =[[DealsViewMain alloc] initWithNibName:@"DealsViewMain" bundle:[NSBundle mainBundle]];
    vController.view.frame=CGRectMake(0, 90, 320, 370);
    self.DealsViewMainObject=vController;
    [vController release];
}
[self.DealsViewMainObject viewWillAppear:YES];
[self.view addSubview:[self.DealsViewMainObject view]];
[self BringSubviewsToFront];
}

-(IBAction)ShowFreebiesView
{
if(ButtonFreebies.selected==YES)
    [ButtonFreebies setSelected:YES];
else
{
    [ButtonFreebies setSelected:YES];
    [ButtonDeals setSelected:NO];
    [self.DealsViewMainObject.view removeFromSuperview];
    [ButtonDashBoard setSelected:NO];
    [self.DashBoardContentView.view removeFromSuperview];
    [ButtonConnect setSelected:NO];
    [self.ConnectContentViewObject.view removeFromSuperview];
}
if(FreebiesViewMainObject==nil)
{
    FreebiesViewMain *vController =[[FreebiesViewMain alloc] initWithNibName:@"FreebiesViewMain" bundle:[NSBundle mainBundle]];
    vController.view.frame=CGRectMake(0, 90, 320, 370);
    self.FreebiesViewMainObject=vController;
    [vController release];
}
[self.FreebiesViewMainObject viewWillAppear:YES];
[self.view addSubview:[self.FreebiesViewMainObject view]];
[self BringSubviewsToFront];
}


-(void)BringSubviewsToFront
{
[self.view bringSubviewToFront:HeaderView];
[self.view bringSubviewToFront:ButtonDeals];
[self.view bringSubviewToFront:ButtonDashBoard];
[self.view bringSubviewToFront:ButtonConnect];
[self.view bringSubviewToFront:ButtonFreebies];
}

现在DealsViewMainObject包含一个tableview,我需要从这里进一步导航。同时从superview中移除视图如何动画像popviewcontroller动画? 顺便说一句,感谢Felixyz的回答。

2 个答案:

答案 0 :(得分:2)

你可以使用UINavigationController,其属性navigationBarHidden设置为YES,并使用你的自定义UIView来调用pushViewController:animated:和popViewControllerAnimated:或popToRootViewControllerAnimated:来驱动你的动画。

如果您想要的唯一内容是幻灯片和幻灯片动画,我认为这样写起来会容易得多。

答案 1 :(得分:2)

您可以按照这些方式使用CATransition:

// Get index of view to be removed (oldSubView)
NSUInteger index;
for(index = 0; [subViews objectAtIndex:index] != oldSubView; ++index) {}

// Remove old view
[oldSubView removeFromSuperview];

// Inser new view at right index
[self insertSubview:newSubView atIndex:index];

// Create the transition
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft;];
[animation setDuration:0.5f];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self layer] addAnimation:animation forKey:@"pushIn"];

您不必告诉过渡要推送哪些视图,它会自动执行(因为您删除并插入了视图)。

查看Apple的示例代码:how to perform transitions between two views using built-in Core Animation transitions

编辑(回应评论): 此代码应在UIView子类中使用。如果要在UIViewController子类中使用它,则必须将self更改为self.view。您只需将此代码放入一个带有如下签名的单独方法中:

-(void)replaceView:(UIView*)oldSubView withView:(UIView*)newSubView;

您将该方法放入您的类中,并在您想要将一个视图替换为另一个视图时调用它。显然,您需要引用这些视图。

我最好的猜测是你应该替换这段代码:

[self.view addSubview:[self.FreebiesViewMainObject view]];
[self BringSubviewsToFront];

[self replaceView:self.rootView withView:freebiesViewMainObject.view];

但你需要更多地研究这一点,以便将所有部分组合在一起。真的,看一些好的示例代码,比如我链接到的代码,以及真正理解它,比从SO获取一些代码片段并尝试将它们放入代码中要好得多。