0

当用户单击按钮时,我需要为视图设置动画。我正在使用以下代码制作动画

应用代理

ViewController2 *vc2_controller = [[ViewController2 alloc]init];

    UINavigationController *baseViewController = [[UINavigationController alloc]initWithRootViewController:vc2_controller];
    [self.window addSubview:baseViewController.view];
     self.window.rootViewController = vc2_controller;



    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];  

    [self.window addSubview:navigationController.view];


    self.viewController = [[vc1controller alloc]initWithNibName:nil bundle:nil];

//ViewController1.m -- 这是有效的

[self.navigationController pushSlideViewController:self.navigationController];

我在类别中创建了函数定义 //Category

- (void)pushSlideViewController:(UIViewController *)viewController
{
   // NSLog(@"In Navigation Controller");

    //[UIView transitionWithView:viewController.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
    CGRect screensize = [[UIScreen mainScreen]bounds];

    [UIView animateWithDuration:0.5 animations:^{

        NSLog(@"In View ANimatin");
        CGRect current_rect =  viewController.view.frame;
        viewController.view.frame = current_rect;
        current_rect.origin.x = screensize.size.width-50;
        viewController.view.frame = current_rect;

    }];

}

现在我从 ViewController2.m 调用 ViewController1.m -- 动画不工作

ViewController1 *view_control = [[ViewController1 alloc]init];

[self.navigationController popSlideViewController:view_control];

//Category
-(void)popSlideViewController:(UIViewController *)viewController
{
    CGRect screensize = [[UIScreen mainScreen]bounds];
    //NSLog(@"In Pop");
    [UIView animateWithDuration:0.5 animations:^{

        //View is not coming to original position.
        CGRect current_rect =  viewController.view.frame;
        NSLog(@"In View ANimatin %f",current_rect.origin.x);
        viewController.view.frame = current_rect;
        current_rect.origin.x = 0.0;
        viewController.view.frame = current_rect;

    }]; 
}

注意:我没有使用 ios5 :-)

谢谢,

4

1 回答 1

1

您正在从 VC1 加载 VC2,正如您所说,它工作正常。然后,您尝试从 VC2 加载 VC1,这就是问题所在 - VC1 仍在加载,位于 VC2 后面,因此您只需要以与最初启动它的方式类似的方式关闭 VC2,只需设置动画即可它退出屏幕,将其从视图中移除,然后释放它(除非您在显示它时释放它)。或者,您可以使用内置方法,

presentViewController:animated:completion:

dismissViewControllerAnimated:completion:

有自己的动画。

于 2012-04-13T09:08:41.480 回答