ios實現button變換顏色並可以放大、縮小、旋轉,iosbutton

來源:互聯網
上載者:User

ios實現button變換顏色並可以放大、縮小、旋轉,iosbutton

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

    }

}

模擬器開啟的介面旋轉放大縮小

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.