UI Development Exercise 2----turn off the lights game

Source: Internet
Author: User
Tags set background

General idea: 1. First, the basic layout is completed by loop method

2. Each point must ensure that the state of the adjacent point is consistent with it, (the position of the side special consideration can be)

 //Turn off the light game
1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4 5@property (nonatomic, strong) UIButton *But ;6@property (nonatomic, strong) NSString *str;//used to record the current status of the light7 8 @end9 Ten @implementationViewcontroller One A- (void) Viewdidload { - [Super Viewdidload]; - the[Self Loadbutton];//Lamp Layout - - - } + - //Basic Layout +- (void) Loadbutton A { at //set the light's initial state to light -_STR = [NSString stringWithFormat:@"iconfont-bulb"]; - //implementing basic layouts in a circular form -Nsinteger n = -; - for(inti =0; I <4; i++) - for(inty =0; Y <4; y++) in { -_but =[UIButton Buttonwithtype:uibuttontypecustom]; to //set location and size +_but.frame = CGRectMake (i*self.view.frame.size.width/4+Ten, y*self.view.frame.size.width/4+ the, self.view.frame.size.width/4- -, -); - //Add the [Self.view addsubview:_but]; * //Set Background color $ [_but setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal];Panax Notoginseng //Set Button addtarget/action event -[_but addtarget:self Action: @selector (Click:) forcontrolevents:uicontroleventtouchupinside];//Click events the //set Tag Value, distinguish button + [_but settag:n]; A then++; + } - } $ $ //button Click to trigger Event -- (void) Click: (UIButton *) Butt - { theNsinteger n =Butt.tag; -NSLog (@"%ld", n);Wuyi //if it's not a light state the if([_str isequal:@"iconfont-4-2"] ==NO) - { Wu //background color set to bright light -[Butt setbackgroundimage:[uiimage imagenamed:@"iconfont-4-2"] forstate:uicontrolstatenormal]; About_str =@"iconfont-4-2"; $ } - Else //otherwise set to off light State - { -NSLog (@"%@", _str); A_str =@"iconfont-bulb"; + [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal]; the } - $ //If the first row is the left and right state to be aligned with it the if(n = =104|| n = =108|| n = = the) the { the //button state remains consistent theButt = (UIButton *) [Self.view viewwithtag:n +1]; - in [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal]; the theButt = (UIButton *) [Self.view viewwithtag:n +4]; About the [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal]; the theButt = (UIButton *) [Self.view viewwithtag:n-4]; + - [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal]; the }Bayi the //If this is the last line, leave the left and right to match its state the Else if(n = =103|| n = =107|| n = =111) - { - //button state remains consistent theButt = (UIButton *) [Self.view viewwithtag:n-1]; the the [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal]; the -Butt = (UIButton *) [Self.view viewwithtag:n +4]; the the [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal]; the 94Butt = (UIButton *) [Self.view viewwithtag:n-4]; the the [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal]; the }98 //the rest of the place is aligned with its state . About Else - {101Butt = (UIButton *) [Self.view viewwithtag:n +1];102 103 [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal];104 theButt = (UIButton *) [Self.view viewwithtag:n-1];106 107 [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal];108 109Butt = (UIButton *) [Self.view viewwithtag:n +4]; the 111 [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal]; the 113Butt = (UIButton *) [Self.view viewwithtag:n-4]; the the [Butt setbackgroundimage:[uiimage imagenamed:_str] forstate:uicontrolstatenormal]; the 117 }118}

:

UI Development Exercise 2----turn off the lights game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.