ios—跑马灯.开始暂停

全局变量{

NSArray *list;

    int a;

NSTimer *timer;

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self.window makeKeyAndVisible];

#pragma mark------3-------

#pragma mark-----跑马灯-------

    a = 1;//给tag赋初始值
    list = [NSArray array];//初始化数组
    list = @[[UIColor purpleColor],[UIColor redColor],[UIColor blueColor],[UIColor greenColor],[UIColor lightGrayColor]];
    
    for (int i=0; i<list.count; i++) {
        view = [[UIView alloc]initWithFrame:CGRectMake(100*i, 150, 50, 50)];//创建视图
        view.backgroundColor = list[arc4random()%list.count];
        view.tag = i+1;
        view.alpha = 0;
        [self.window addSubview:view];
    }
 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"开始" forState:UIControlStateNormal];
    [button1 setTitle:@"暂停" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor purpleColor];
    button1.backgroundColor = [UIColor blueColor];
    button.frame = CGRectMake(80, 300, 50, 50);
    button1.frame = CGRectMake(300 , 300, 50, 50);
    [self.window addSubview:button1];
    [self.window addSubview:button];
    
    [button addTarget:self action:@selector(kaishi) forControlEvents:UIControlEventTouchDown];
     [button1 addTarget:self action:@selector(over) forControlEvents:UIControlEventTouchDown];
    
    
    return YES;
}






-(void)kaishi{
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(start) userInfo:nil repeats:YES];
    
}
-(void)over{
    [timer invalidate];

}

-(void)start{
    switch (a) {
        case 1:
            
            [self.window viewWithTag:a].backgroundColor = list[1];
            [self.window viewWithTag:a].alpha  = 1;
            a++;
            break;
        case 2:
            [self.window viewWithTag:a-1].alpha = 0;
            
            [self.window viewWithTag:a].backgroundColor = list[2];
            [self.window viewWithTag:a].alpha = 1;
            
            a++;
            break;
        case 3:
            [self.window viewWithTag:a-1].alpha = 0;
            
            [self.window viewWithTag:a].backgroundColor = list[4];
            [self.window viewWithTag:a].alpha = 1;
            a++;
            break;
        case 4:
            [self.window viewWithTag:a-1].alpha = 0;
            
            [self.window viewWithTag:a].backgroundColor = list[0];
            [self.window viewWithTag:a].alpha = 1;
            a++;
            break;
        default:
            a = 1;
            [self.window viewWithTag:4].alpha = 0;
            break;
    }
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值