我试图让我的应用程序从 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 语句(如果是,我将如何做到这一点)?