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

//先生成霓虹灯框架,同时为其随机附上不同的颜色
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值