2013-08-26 70 views
0

我有一個UIViewController,我編輯使用IB。我在上面放了一個UINavigationBar和一個UISegmentedControl,在他們的下面放了一個UIViews。我希望能夠使用動畫在UIViews之間切換,但我只想動畫UIViews,我希望navigationBar和athe segmentedControl不移動。我顯示代碼現在我怎麼做。iOS UIView過渡,不要更改導航欄

任何想法如何我只能移動3個視圖?

- (IBAction)segmentedControlValueChanged:(id)sender { 

    UISegmentedControl* segmentedControl = sender; 

    if(lastSelectedViewIndex != [segmentedControl selectedSegmentIndex]) { 

     CATransition *transition = [CATransition animation]; 
     transition.duration = 0.4; 
     transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
     transition.type = kCATransitionMoveIn; 

     if(lastSelectedViewIndex < [segmentedControl selectedSegmentIndex]) 
      transition.subtype = kCATransitionFromLeft; 
     else 
      transition.subtype = kCATransitionFromRight; 

     transition.removedOnCompletion = YES; // force removal of animation when completed. 
     { 
      switch ([segmentedControl selectedSegmentIndex]) { 

       case 0: 

        [self.usageScenarioView setHidden:NO]; 
        [self.loginCredentialsView setHidden:YES]; 
        [self.whatItCoversView setHidden:YES]; 

        [self.pageControl setCurrentPage:0]; 
        break; 

       case 1: 
        [self.usageScenarioView setHidden:YES]; 
        [self.loginCredentialsView setHidden:NO]; 
        [self.whatItCoversView setHidden:YES]; 

        [self.pageControl setCurrentPage:1]; 
        break; 

       case 2: 
        [self.usageScenarioView setHidden:YES]; 
        [self.loginCredentialsView setHidden:YES]; 
        [self.whatItCoversView setHidden:NO]; 

        [self.pageControl setCurrentPage:2]; 

        break; 

      } 
     } 

     lastSelectedViewIndex = [segmentedControl selectedSegmentIndex]; 
     [self.view.layer addAnimation:transition forKey:nil]; 

    } 
} 

回答

1

假設您的3個視圖命名爲view1,view2,view3。如果你想刪除廠景和顯示視圖2或視圖3,只是做現有的代碼,但改變

[self.view.layer addAnimation:transition forKey:nil]; 

[view1.layer addAnimation:transition forKey:nil]; 

,將動畫視圖1只不是整個視圖。同樣,你可以試試,

[view2.layer addAnimation:transition forKey:nil]; 
[view3.layer addAnimation:transition forKey:nil]; 

更準確地說,是不喜歡

transition.removedOnCompletion = YES; // force removal of animation when completed. 
    { 
     switch ([segmentedControl selectedSegmentIndex]) { 

      case 0: 

       [self.usageScenarioView setHidden:NO]; 
       [self.loginCredentialsView setHidden:YES]; 
       [self.whatItCoversView setHidden:YES]; 

       [self.pageControl setCurrentPage:0]; 

       [self.usageScenarioView.layer addAnimation:transition forKey:nil]; 
       break; 

      case 1: 
       [self.usageScenarioView setHidden:YES]; 
       [self.loginCredentialsView setHidden:NO]; 
       [self.whatItCoversView setHidden:YES]; 

       [self.pageControl setCurrentPage:1]; 

       [self.loginCredentialsView.layer addAnimation:transition forKey:nil]; 
       break; 

      case 2: 
       [self.usageScenarioView setHidden:YES]; 
       [self.loginCredentialsView setHidden:YES]; 
       [self.whatItCoversView setHidden:NO]; 

       [self.pageControl setCurrentPage:2]; 

       [self.whatItCoversView.layer addAnimation:transition forKey:nil]; 
       break; 

     } 
    } 

    lastSelectedViewIndex = [segmentedControl selectedSegmentIndex]; 


} 
0

你可以定義一個UIView * containerView在下面的UISegmentedControl * segmentedControl IB具有座標和你想要的觀點立場。 然後你可以在視圖內,你可以要使用此功能三個UIViews之間做轉換:

-(void) replacePreviousViewInContainerViewWith:(UIView*) newView { 
[UIView transitionWithView:_containerView duration:0.7 
        options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionCurveEaseIn 
       animations:^ { 
        [_containerView.subviews[0] removeFromSuperview]; 
        [_containerView addSubview:newViewVC.view]; 
       } 
       completion:^(BOOL finished) { 
        if (finished) { 
         NSLog(@"Now displaying %@.", [newView class]); 
        } 
       }]; 
[UIView commitAnimations]; 
} 

,你調用這個函數像這樣從那裏,你想改變瀏覽:

UIViewController *newViewVC = [[UIViewController alloc] [email protected]"YOURNAME"]; 
newViewVC.view.frame = _containerView.frame; 

[self replacePreviousViewInContainerViewWith: newViewVC.view];