2013-03-26 53 views
3

我沒有使用導航控制器,而我正在使用故事板。視圖控制器的轉換 - 從左到右

我必須從1視圖控制器轉換到其他,我正在使用segue。

現在我將segue樣式設置爲Custom,並在相應的類中覆蓋perform方法。

-(void)perform 
{ 
    UIViewController *sourceViewController = [self sourceViewController]; 
    UIViewController *destinationViewController = [self destinationViewController]; 

    CATransition *transition = [CATransition animation]; 
    transition.duration = 0.25; 
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromLeft; 

    [sourceViewController.view.layer addAnimation:transition forKey:kCATransition];animated:NO]; 
    [sourceViewController presentViewController:destinationViewController animated:YES completion:nil]; 
} 

但是,目標視圖控制器modalTransitionStyle也有一個屬性。

所以現在,源代碼VC從左到右好像被推入,但我想表明它正在被目標視圖控制器推送。相反,目標VC在默認情況下會執行「遮蓋垂直」,並且modalTransitionStyle屬性只有四個選項可用,但其中沒有一個適用於我。

我想讓它工作,這個動畫應該被添加到一些超級視圖層,一個超級視圖從這兩個視圖控制器已經呈現。但是,有沒有這樣的上海華..

回答

7

通常你會給一個storyboardId到destinationController從sourceViewController這樣稱呼它:

//push next view 
UIStoryboard *storyboard = self.storyboard; 
YourViewControllerClass *destVC = [storyboard instantiateViewControllerWithIdentifier:@"StoryboardID"]; 
[self.navigationController pushViewController:destVC animated:YES]; 

或者,您也可以手動做這樣的:

// Get the views. 
    UIView * fromView = sourceViewController.view; 
    UIView * toView = destinationViewController.view; 

    // Get the size of the view area. 
    CGRect viewSize = fromView.frame; 

    // Add the toView to the fromView 
    [fromView.superview addSubview:toView]; 

    // Position it off screen. 
    toView.frame = CGRectMake(320 , viewSize.origin.y, 320, viewSize.size.height); 

    [UIView animateWithDuration:0.4 animations: 
    ^{ 
     // Animate the views on and off the screen. This will appear to slide. 
     fromView.frame =CGRectMake(-320 , viewSize.origin.y, 320, viewSize.size.height); 
     toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height); 
    } 
        completion:^(BOOL finished) 
    { 
     if (finished) 
     { 
      // Remove the old view from its parent. 
      [fromView removeFromSuperview]; 

      //I use it to have navigationnBar and TabBar at the same time 
      //self.tabBarController.selectedIndex = indexPath.row+1; 
     } 
    }]; 

** EDIT **

逆函數(類似於在導航的返回按鈕控制器):

// Get the views. 
UIView * fromView = fromViewController.view; 
UIView * toView = destViewController.view; 

// Get the size of the view area. 
CGRect viewSize = fromView.frame; 

// Add the to view to the tab bar view. 
[fromView.superview addSubview:toView]; 

// Position it off screen. 
toView.frame = CGRectMake(-320 , viewSize.origin.y, 320, viewSize.size.height); 

[UIView animateWithDuration:0.4 animations: 
^{ 
    // Animate the views on and off the screen. This will appear to slide. 
    fromView.frame =CGRectMake(320 , viewSize.origin.y, 320, viewSize.size.height); 
    toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height); 
} 
       completion:^(BOOL finished) 
{ 
    if (finished) 
    { 
     // Remove the old view from the tabbar view. 
     [fromView removeFromSuperview]; 
    } 
}]; 
+0

我不使用導航控制器。你的第二個方法會和'提出'一個觀點一樣嗎?我的意思是它會調用'viewWillAppear'和其他類似的方法嗎?我能否'解僱'這個觀點,還是我會做類似的事情來刪除這個觀點? – neeraj 2013-03-26 08:41:09

+0

或者你能告訴我一些使用我可以將我的應用程序轉換爲使用導航控制器的步驟嗎?我有VC1,它可以呈現VC2或VC3。 VC2和VC3必須相互關聯。 – neeraj 2013-03-26 08:44:13

+1

@neeraj第二種方法會在右側附加第二個視圖,向右滑動直到只有該視圖可見,然後關閉第一個視圖。所有相應的方法,如'viewDidLoad'或'viewWillAppear'將照常被調用。如果通過解僱你的意思是像'dismissModalViewControllerAnimated:'產生的行爲,你可以通過調用一個函數來做到這一點,但函數按相反的順序(將新視圖附加到左側並向左滑動)。 – Daniel 2013-03-27 10:30:13

1

所有在我的應用程序的ViewControllers從MyViewController繼承它,此外還有一些其他的默認行爲,有此屬性:

@property(readwrite, nonatomic) BOOL slideFromSide; 

而這種代碼:

- (void) presentViewController: (UIViewController*) vc animated: (BOOL) flag completion: (void (^)(void)) completion 
{ 
    BOOL slideIn = NO; 
    if ([vc isKindOfClass: [MyViewController class]]) 
    { 
     MyViewController *svc = (MyViewController*) vc; 
     slideIn = svc.slideFromSide; 
    } 

    if (slideIn) 
    { 
     [self addChildViewController: vc]; 
     [self.view addSubview: vc.view]; 

     CGRect frame = vc.view.frame; 
     frame.origin.x = frame.size.width; 
     vc.view.frame = frame; 

     frame.origin.x = 0; 
     [UIView animateWithDuration: 0.33 
         animations: ^{ 
          vc.view.frame = frame; 
         } 
     ]; 
    } 
    else 
    { 
     [super presentViewController: vc animated: flag completion: completion]; 
    } 
} 


- (IBAction) backAction: (id) sender 
{ 
    if (_slideFromSide) 
    { 
     CGRect frame = self.view.frame; 
     frame.origin.x = frame.size.width; 
     [UIView animateWithDuration: 0.33 
         animations: ^{ 
          self.view.frame = frame; 
         } 
         completion:^(BOOL finished) { 
          [self removeFromParentViewController]; 
         } 
     ]; 
    } 
    else 
    { 
     [self dismissViewControllerAnimated: YES completion: nil]; 
    } 
} 

然後,在我想滑入的ViewControllers中,我有:

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; 
    if (self) 
    { 
     self.slideFromSide = YES; 
     // Whatever else you want to do 
    } 

    return self; 
} 

調用一個新的視圖控制器就像是正常的:

WhateverViewController *vc = [WhateverViewController alloc] initWithNibName: nil bundle: nil]; 
[self presentViewController: vc animated: YES completion: nil]; 
相關問題