您的位置:首页 > 移动开发 > IOS开发

ios—跑马灯.开始暂停

2015-12-12 16:23 267 查看
全局变量{

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;

    }

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 开始暂停 跑马灯