iOS-点击视图,视图背景颜色随机更改

时间:2023-03-09 20:47:15
iOS-点击视图,视图背景颜色随机更改

一、效果图

iOS-点击视图,视图背景颜色随机更改

二、代码

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.title = @"背景颜色的随机显示";

}

//点击视图,视图颜色随机更改

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

CGFloat red = (CGFloat)arc4random() / 0x100000000;

CGFloat green = (CGFloat)arc4random() / 0x100000000;

CGFloat blue = (CGFloat)arc4random() / 0x100000000;

//设置三原色

self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end