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

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

    

    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    _window.backgroundColor = [UIColor whiteColor];

    [_window makeKeyAndVisible];

    

    //定义一个父视图

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

    [_window addSubview:superView];

    

    _views = [[NSMutableArray alloc] init];

    

    //循环创建七个子视图

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

        

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

        //设置随机色

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

        

        [_window addSubview:view];

        

        UIView *lastView = [_views lastObject];

        if (lastView != nil) {

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

        }

        [_views addObject:view];

        

    }

    

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

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    

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

    

    button1.tag = 101;

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

   

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

    

    [_window addSubview:button1];


    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    

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


    

    button2.tag = 102;

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

    

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

    

    [_window addSubview:button2];

    

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    

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

    

    button3.tag = 103;

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

    

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

    

    [_window addSubview:button3];

    

    _index = 0;

    return YES;

}


//button的点击事件

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

    if (button.tag == 103) {

        for (UIView *view in _views) {

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

        }

        }else if (button.tag == 102)

        {

            for (UIView *view in _views) {

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

        }

    

        }else{

    //开启一个定时器

            [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];

        }

}


//定时器

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

    

    [UIView beginAnimations:nil context:nil];

    

    [UIView setAnimationDuration:1.9];

    

    for (UIView *view in _views) {

        

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

       

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

    }

    [UIView commitAnimations];

    

    _index++;

    if (_index == 10) {

        

        for (UIView *view in _views) {

            [view removeFromSuperview];

        }

        

        [time invalidate];

    }

}

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

猜你喜欢

转载自blog.csdn.net/Remember29/article/details/45175691