0

我有一个UIViewController我使用 IB 编辑的。我在上面放了aUINavigationBar和a ,在它们下面放了UISegmentedControl3 UIViews。我希望能够在UIViews使用动画之间切换,但我只想制作动画UIViews,我希望导航栏和分段控件不移动。我展示了我现在如何做的代码。

知道我如何只能移动 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];

    }
}
4

2 回答 2

1

假设您的 3 个视图被命名为 view1、view2、view3。如果要删除 view1 并显示 view2 或 view3,只需执行现有代码,但更改

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

进入

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

这将为 view1 设置动画,而不是整个视图。同样,您可以尝试,

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


}
于 2013-08-26T08:23:59.600 回答
0

您可以在 IB 中的 UISegmentedControl* segmentedControl 下方定义一个 UIView* containerView,它具有您想要的视图的坐标和位置。然后您可以在该视图中使用此功能在所需的三个 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];
}

并且您从要更改视图的位置调用此函数:

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

[self replacePreviousViewInContainerViewWith: newViewVC.view];
于 2013-08-26T08:35:09.257 回答