pushViewController的iOS过渡动画

时间:2012-05-30 16:22:55

标签: objective-c ios uiview

我有一个AppDelegate,它拥有一个窗口,从那里开始,一个主视图,当我进入下一个视图时,我推动新视图以使用相同的导航控制器和工具栏:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    self.appview = [[AppointmentView alloc] initWithNibName:@"AppointmentView" bundle:[NSBundle mainBundle]];

    @synchronized(self.MahAppntmnts){                             // Set the view's appointment
        [appview setMyAppointment:[[MahAppntmnts MyAppointments] objectAtIndex:indexPath.section]];
    }
    [super addChildViewController:self.appview];
    AppDelegate *del = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [UIView  beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [del.navigationController pushViewController:self.appview animated:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
}

现在这适用于切换视图,但是当我想要回去时,我会得到同样无聊的翻转过渡。 我尝试在AppView的viewWillDissapear委托方法上设置:

-(void) viewWillDisappear:(BOOL)animated 
{
    [UIView  beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:0.375];
    [self.navigationController popViewControllerAnimated:NO];
    [UIView commitAnimations];
    [super viewWillDisappear:animated];
}

当我回到上一个视图时,这似乎有用,但是当我尝试转换到新视图时(使用与之前相同的方法),它们要么不出现,要么转到相同的视图。所以我猜这个代码不应该在viewWillDissapear上,或者我做错了...

1 个答案:

答案 0 :(得分:1)

我找到了解决方案,问题是我对动画所属的“谁”的想法。我将viewWillDissapear(位于推送视图中)中找到的代码添加到首先执行推送的视图的viewWillAppear委托(在accessoryButtonTappedForRowWithIndexPath中找到的代码),以便控制视图在推送另一个视图时启动动画,当另一个视图导航回到它时,将显示反向动画。解决了所有问题并且像魅力一样工作。