ios实现button变换颜色并可以放大、缩小、旋转

来源:互联网 发布:复杂网络聚类分析 编辑:程序博客网 时间:2024/05/16 01:29

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    

    _window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];

    _window.backgroundColor = [UIColorwhiteColor];

    [_windowmakeKeyAndVisible];

    

    //定义一个父视图

    UIView *superView = [[UIViewalloc] initWithFrame:CGRectMake(65,65, 300, 300)];

    [_window addSubview:superView];

    

    _views = [[NSMutableArrayalloc] init];

    

    //循环创建七个子视图

    for (int i =0; i < 7; i ++) {

        

        UIView *view = [[UIViewalloc] initWithFrame:CGRectMake(50,50, 250, 250)];

        //设置随机色

        view.backgroundColor = [UIColorcolorWithRed:arc4random()%10*.1green:arc4random()%10*.1blue:arc4random()%10*.1alpha:1];

        

        [_window addSubview:view];

        

        UIView *lastView = [_viewslastObject];

        if (lastView != nil) {

            view.transform = CGAffineTransformScale(lastView.transform,0.8, 0.8);

        }

        [_views addObject:view];

        

    }

    

    //在界面上添加几个button,来实现不同的功能

    UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    

    [button1 setTitle:@"旋转"forState:UIControlStateNormal];

    

    button1.tag = 101;

    button1.frame = CGRectMake(50, 320, 50, 30);

   

    [button1 addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

    

    [_window addSubview:button1];


    UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    

    [button2 setTitle:@"放大"forState:UIControlStateNormal];


    

    button2.tag = 102;

    button2.frame = CGRectMake(150, 320, 50, 30);

    

    [button2 addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

    

    [_window addSubview:button2];

    

    UIButton *button3 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    

    [button3 setTitle:@"缩小"forState:UIControlStateNormal];

    

    button3.tag = 103;

    button3.frame = CGRectMake(250, 320, 50, 30);

    

    [button3 addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];

    

    [_window addSubview:button3];

    

    _index = 0;

    return YES;

}


//button的点击事件

- (void)buttonAction:(UIButton *)button{

    if (button.tag ==103) {

        for (UIView *viewin _views) {

            view.transform = CGAffineTransformScale(view.transform, .5, .5);

        }

        }else if (button.tag ==102)

        {

            for (UIView *viewin _views) {

                view.transform = CGAffineTransformScale(view.transform, 2, 2);

        }

    

        }else{

    //开启一个定时器

            [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(timeAction:)userInfo:nilrepeats:YES];

        }

}


//定时器

- (void)timeAction:(NSTimer *)time{

    

    [UIViewbeginAnimations:nilcontext:nil];

    

    [UIViewsetAnimationDuration:1.9];

    

    for (UIView *viewin _views) {

        

        view.backgroundColor = [UIColorcolorWithRed:arc4random()%10*.1green:arc4random()%10*.1blue:arc4random()%10*.1alpha:1];

       

        view.transform = CGAffineTransformRotate(view.transform,M_PI/10);

    }

    [UIViewcommitAnimations];

    

    _index++;

    if (_index ==10) {

        

        for (UIView *viewin _views) {

            [view removeFromSuperview];

        }

        

        [time invalidate];

    }

}

模拟器打开的界面
旋转
放大
缩小
0 0