简体   繁体   English

iOS UIView过渡,请勿更改导航栏

[英]iOS UIView transition, don't change the navigationbar

I have a UIViewController that i edit using IB. 我有一个使用IB编辑的UIViewController I put a UINavigationBar and a UISegmentedControl on the top and 3 UIViews under them. 我在顶部放置了一个UINavigationBar和一个UISegmentedControl在其下面放置了3个UIViews I want to be able to switch between the UIViews using an animation, but i only want to animate the UIViews , i want the navigationBar and athe segmentedControl to not move. 我希望能够给之间切换UIViews使用动画,但我只想动画UIViews ,我要的导航栏和athe segmentedControl就挪不动。 I show the code how i do it now. 我显示代码现在如何执行。

Any idea how i could only move the 3 views? 任何想法我怎么只能移动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];

    }
}

Say your 3 views are named as view1, view2, view3. 假设您的3个视图分别命名为view1,view2,view3。 If you want to remove view1 and show view2 or view3, just do the existing code, but change 如果要删除view1并显示view2或view3,则只需执行现有代码,但更改

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

into 进入

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

that will animate the view1 only not the whole view. 这将使view1而不是整个视图动起来。 Similarly you can try, 同样,您可以尝试,

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

more precisely, do like 更确切地说,喜欢

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


}

you can define a UIView* containerView in IB below your UISegmentedControl* segmentedControl which has the coordinates and position of the views you want. 您可以在UISegmentedControl * segmentedControl下面的IB中定义一个UIView * containerView,该容器具有所需视图的坐标和位置。 Then you can inside that view you can do transition between the three UIViews you want with this function: 然后,您可以在该视图内使用此功能在三个UIView之间进行转换:

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

and you call this function like this from where you want to change the Views: 然后您要在其中更改视图的位置调用此函数:

UIViewController *newViewVC = [[UIViewController alloc] initWithNibName@"YOURNAME"];
newViewVC.view.frame = _containerView.frame;

[self replacePreviousViewInContainerViewWith: newViewVC.view];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM