0

我试图让我的应用程序从 3 个屏幕进行基本的背景更改。这是我的 .h 文件。

#import <UIKit/UIKit.h>

@class BIDYellowViewController;
@class BIDBlueViewController;
@class BIDGreenViewController;

@interface BIDSwitchViewController : UIViewController

@property (strong, nonatomic) BIDYellowViewController *yellowViewController;
@property (strong, nonatomic) BIDBlueViewController *blueViewController;
@property (strong, nonatomic) BIDGreenViewController *greenViewController;

- (IBAction)switchViews:(id)sender;


@end

这是我在 .m 文件中的方法,当按下 IBAction 按钮时,超级视图将从一个屏幕转到下一个屏幕,并继续循环旋转它们。

- (IBAction)switchViews:(id)sender {
//animations
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//loads yellow
    if (self.yellowViewController.view.superview == nil) {
        if (self.yellowViewController == nil) {
            self.yellowViewController = [[BIDYellowViewController alloc]initWithNibName:@"YellowView" bundle:nil];
        }
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
//removes blue
        [self.blueViewController.view removeFromSuperview];
        [self.view insertSubview:self.yellowViewController.view atIndex:0];
//loads green
    } else if(self.greenViewController.view.superview == nil){
        if (self.greenViewController == nil) {
            self.greenViewController = [[BIDGreenViewController alloc] initWithNibName:@"GreenView" bundle:nil];
        }
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
//removes yellow
        [self.yellowViewController.view removeFromSuperview];
        [self.view insertSubview:self.greenViewController.view atIndex:0];

//loads blue
    } else if(self.blueViewController.view.superview == nil){
        if (self.blueViewController == nil) {
            self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
        }
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
//removes green
        [self.greenViewController.view removeFromSuperview];
        [self.view insertSubview:self.blueViewController.view atIndex:0];
    }
    [UIView commitAnimations];
}

我遇到的问题是,在第一个循环中,它重复绿屏两次,然后继续并正常工作。我是否遗漏了什么,或者我应该使用 switch: case 语句(如果是,我将如何做到这一点)?

4

1 回答 1

0

如果想连续重复,则使用全局计数变量作为

static int count=0;

并使用代码:

- (IBAction)switchViews:(id)sender
{
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

    switch (count) {
        case 0:

            [greenView removeFromSuperview];
            [blueView removeFromSuperview];

            [self.view addSubview:yellowView];
            break;
        case 1:

            [yellowView removeFromSuperview];
            [greenView removeFromSuperview];

            [self.view addSubview:blueView];

            break;
        case 2:

            [blueView removeFromSuperview];
            [yellowView removeFromSuperview];

            [self.view addSubview:greenView];

            break;

    }

    count++;

    if (count==3) {
        count=0;
    }

    [UIView commitAnimations];
}

您也可以将三个 xib 与单个 ViewController 一起使用,只需将 xib 的类更改为您想要使用的 ViewController 的类,如下所示---

BIDViewController *yellowViewController;
BIDViewController *blueViewController;
BIDViewController *greenViewController;


self.yellowViewController = [[BIDViewController alloc]initWithNibName:@"YellowView" bundle:nil];
self.greenViewController = [[BIDViewController alloc] initWithNibName:@"GreenView" bundle:nil];
self.blueViewController = [[BIDViewController alloc] initWithNibName:@"BlueView" bundle:nil];
于 2013-06-17T04:44:01.733 回答