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];

    }

}

模拟器打开的界面
旋转
放大
缩小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值