Ios 在时间间隔内切换UIViewController的背景色
嗨,我对我正在做的事情有点迷茫。我可以通过创建视图本身的属性,然后使用以下方法轻松切换视图控制器的颜色:Ios 在时间间隔内切换UIViewController的背景色,ios,ios7,storyboard,xcode5,Ios,Ios7,Storyboard,Xcode5,嗨,我对我正在做的事情有点迷茫。我可以通过创建视图本身的属性,然后使用以下方法轻松切换视图控制器的颜色: @property (strong, nonatomic) IBOutlet UIView *viewController; self.viewController.backgroundColor = [UIColor redColor]; //first color self.viewController.backgroundColor = [UIColor whiteColor]; /
@property (strong, nonatomic) IBOutlet UIView *viewController;
self.viewController.backgroundColor = [UIColor redColor]; //first color
self.viewController.backgroundColor = [UIColor whiteColor]; //second color
现在让我们假设我想要一个特定的视图每秒在两种颜色之间反复地来回改变。我怎么能这样做
提前感谢您应该查看NSTimer以了解此
// In viewDidLoad or wherever you want to start alternating...
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerListener) userInfo:nil repeats:YES];
- (void)timerListener {
if (viewIsFirstColor) {
// Code to switch colors
} else {
// Code to switch colors
}
}
您可能希望在使用完此计时器后,
使其无效。为什么有名为viewController
的UIView
?这是一个混乱的配方…而且:我唯一有点麻烦的是viewsfirstcolor变量应该是什么,以便它每秒钟切换一次?这是我唯一不明白的事情。请尝试使用(self.viewController.backgroundColor==[UIColor redColor])而不是(viewsfirstcolor)。您应该使用-isEqual:
而不是=
来比较Objective-C对象。哦,很抱歉viewsfirstcolor
,这只是逻辑的占位符,用于确定视图是否为第一种颜色:)这起作用了用于函数的代码是:-(void)timerListener{if([self.mainViewController.backgroundColor isEqual:[UIColor colorWithRed:249.0/255.0 green:70.0/255.0 blue:28.0/255.0 alpha:1]){self.mainViewController.backgroundColor=[UIColor whiteColor];}如果([self.mainViewController.backgroundColor isEqual:[UIColor whiteColor]]){self.mainViewController.backgroundColor=[UIColor colorWithRed:249.0/255.0 green:70.0/255.0 blue:28.0/255.0 alpha:1];//切换颜色的代码}