开发者

how to create navigation controller push effect using core animation?

I Am creating an application and i want to achieve push effect but without using navigation controller.Does someone knows a core animation source code to achieve this? I have a view hierarchy like this.A main controller which has an header image with 4 buttons on it.I mean it will be displayed all the time whatever view in front o开发者_如何转开发f screen is.So i use add subview methods.Now in some view i want to navigate further when cliking on table view cell but unabvle to figure out how to achieve that navigation animation?

here is the code to adding subviews....

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

Now the DealsViewMainObject contains a tableview and i need to navigate further from here.also while removing view from superview how to animate like popviewcontroller animated? BTW thanks for the answer Felixyz.


You can use a CATransition, along these lines:

// 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"];

You don't have to tell the transition which views to push, it will do it automatically (since you removed and inserted views).

Have a look at this example code from Apple: how to perform transitions between two views using built-in Core Animation transitions.

EDIT (in response to comments): This code should be used inside a UIView subclass. If you want to use it in a UIViewController subclass, you have to change self to self.view everywhere. You would simply put this code into a separate method with a signature like this:

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

You put that method into your class, and call it whenever you want to replace one view with another. You need to have a reference to these views, obviously.

My best guess is that you should replace this code:

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

with

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

But you have to work on this a bit more to get all the pieces to fit together. Really, looking at some good example code like the one I linked to, and really understanding it, is a lot better then to get a few code snippets off of SO and try to throw them into your code.


You could use a UINavigationController anyway, with its property navigationBarHidden set to YES, and use your custom UIView to call pushViewController:animated: and popViewControllerAnimated: or popToRootViewControllerAnimated: to drive your animations.

If the only thing you want is the slide-in and slide-out animations, I think this would be much easier to write.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜