刚学ui,自己写了个霓虹灯,我觉的我写的这个比比百度上简单多了!!!

来源:互联网 发布:八皇后问题递归算法 编辑:程序博客网 时间:2024/04/27 16:46

//先生成霓虹灯框架,同时为其随机附上不同的颜色
for (int i = 0; i < 7; i++) {
CGRect frame = CGRectMake(0 + 30 * i, 100 + 30 * i, 375 - 30 * 2 * i, 375 - 30 * i * 2);
UIView *view = [[UIView alloc] initWithFrame:frame];
view.tag = 100 + i;
view.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1.0];
[self.window addSubview:view];
[view release];
}

//从里到外
[self insideToOutside];
//从外道里
[self outsideToInside];

//写从里到外的方法
//生成按钮
- (void)insideToOutside {

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];btn.frame = CGRectMake(50, 525, 100, 40);btn.backgroundColor = [UIColor redColor];[btn setTitle:@"从里到外" forState:UIControlStateNormal];[btn addTarget:self action:@selector(toout:) forControlEvents:UIControlEventTouchUpInside];[self.window addSubview:btn];}

//颜色从里到外, 上面我自己附的tag值是100-106,所有遍历的是100-106,
让当前的颜色被后一个替代.到了最后一个时,再随机生成一种颜色.
- (void)toout:(UIButton *)sender {
for (int i = 100; i < 107; i ++) {
[self.window viewWithTag:i].backgroundColor = [self.window viewWithTag:i + 1].backgroundColor;
[self.window viewWithTag:106].backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1.0];
}

}
//从外到里
- (void)outsideToInside {

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];btn.frame = CGRectMake(200, 525, 100, 40);btn.backgroundColor = [UIColor redColor];[btn setTitle:@"从外到里" forState:UIControlStateNormal];[btn addTarget:self action:@selector(toinside:) forControlEvents:UIControlEventTouchUpInside];[self.window addSubview:btn];

}
//变化方法同上,只需要注意替换位置就行.
- (void)toinside:(UIButton *)sender {
for (int i = 107; i >= 100; i –) {
[self.window viewWithTag:i].backgroundColor = [self.window viewWithTag:i - 1].backgroundColor;
[self.window viewWithTag:100].backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1.0];
}

}

0 0
原创粉丝点击