iOS中实现心块的颜色轮转(重点:交换颜色,设置定时器)

  
    UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(80, 280, 30, 30)];
    view5.backgroundColor = [UIColor redColor];
     view5.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
    view5.tag = 105;
    [self.window addSubview:view5];
    [view5 release];
    UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(40, 240, 30, 30)];
    view6.backgroundColor = [UIColor redColor];
     view6.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
    view6.tag = 106;
    [self.window addSubview:view6];
    [view6 release];
    
    
    
    
    
    //布局按钮
    UIButton *starbtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [starbtn setTitle:@"开始" forState:UIControlStateNormal];
    
    starbtn.frame = CGRectMake(100, 380, 45, 35);
    [self.window addSubview:starbtn];
    
  
    
    
    UIButton *overbtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [overbtn setTitle:@"取消" forState:UIControlStateNormal];
    
    overbtn.frame = CGRectMake(180, 380, 45, 35);
    [self.window addSubview:overbtn];
  
    
    
    [starbtn addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [overbtn addTarget:self action:@selector(cancleAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)startAction:(UIButton *)sender
{
    //timeInterInterval  时间间隔
    //target 目标
    //seclctor  target要执行selector方法
    //userInfo 用户信息
    //repeats 是否反复定时执行
    [self.timer invalidate];  //取消上一个定时器
   self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];
}
- (void)cancleAction
{

    [self.timer setFireDate:[NSDate distantFuture]];
}

//颜色交换
-(void)changeColor{
    UIView *tempView = [[UIView alloc] init];
    tempView.backgroundColor = [self.window viewWithTag:100].backgroundColor;
    
    for (int i = 100; i < 106; i++) {
        [self.window viewWithTag:i].backgroundColor = [self.window viewWithTag:( i + 1) ].backgroundColor;
    }
    [self.window viewWithTag:106].backgroundColor = tempView.backgroundColor;
}

 

转载于:https://www.cnblogs.com/wohaoxue/p/4764707.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值