2012-04-13 148 views
0

我需要在用戶單擊按鈕時爲視圖添加動畫。我使用下面的代碼動畫Xcode中的視圖控制器動畫

應用代表

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

我創建的類 //分類

內部函數定義
- (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; 

    }]; 

} 

現在我從View中調用ViewController1.m Controller2.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的:-)

感謝,

回答

1

你從VC1,你說哪個,裝VC2,工作正常。然後,你試圖從VC2加載VC1,這是問題所在 - VC1仍然在VC2後面加載,所以你只需要以類似的方式關閉VC2,就像你第一次啓動VC2一樣,只需要動畫它退出屏幕,將其從視圖中移除,然後釋放它(除非您在顯示時釋放它)。另外,您可以使用內置的方法,

presentViewController:animated:completion: 

dismissViewControllerAnimated:completion: 

有自己的動畫。

+0

Franklyn:我沒有加載任何新的ViewController。它已經加載在appdelegate中。我從VC1本身開始動畫VC1。當我從VC2爲VC1創建動畫時,它不起作用 – 2012-04-13 09:11:58

+0

你的代碼顯示你確實正在從VC2加載一個新的VC1 - 如果這樣: ViewController1 * view_control = [[ViewController1 alloc] init]; [self.navigationController popSlideViewController:view_control]; ...就是你在做什麼,你試圖「流行」一個你甚至還沒有添加視圖的VC。 – SomaMan 2012-04-13 09:25:58

+0

也許你可以發佈更多的代碼,包括appDelegate的東西 – SomaMan 2012-04-13 09:31:28