繁体   English   中英

在一个时间间隔内切换UIViewController的背景颜色

[英]Switch the background color of a UIViewController within a time interval

嗨,我对我想做的事情有点迷茫。 我可以通过创建视图本身的属性,然后使用类似以下内容的方法轻松切换视图控制器的颜色:

@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
    }
}

完成操作后,您可能想invalidate此计时器invalidate ...

暂无
暂无

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

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