Lightsout--easy

Source: Internet
Author: User



First, use the For loop to create the view on the emulator

for (Nsinteger i = 0; i < 6; i++) {
for (Nsinteger j = 0; J <; J + +) {
// Create and light bulb view equal number of buttons to control the lamp switch
UIButton *lightbutton = [Uibuttonbuttonwithtype:uibuttontypesystem];
Lightbutton.frame = CGRectMake (i *, + (J * 64), 64,64);
//Add a picture to button
[Lightbutton setbackgroundimage:[uiimage imagenamed:@ "2"]forstate:uicontrolstatenormal];
//to associate an implementation method for button buttons
[Lightbutton addtarget:self Action: @selector (Pressbutton:) forcontrolevents:uicontroleventtouchupinside];
//set the Tag value for button buttons for easy search
Lightbutton.tag = + J + + * I;
//to add a button to the window view
[Self.window Addsubview:lightbutton];
}
}

if you want to implement a light bulb switch, you need a way to click on the button

-(void) Pressbutton: (UIButton *) button {
//randomly Select a button, then click on it can affect the button up to five, respectively, and up and down
Nsintegerarray[5] = {Button.tag, Button.tag + 1, Button.tag + 100,button.tag-1, button.tag-100};
if ([Button.currentbackgroundimage isequal:[uiimage imagenamed:@ "2"]) {
for (Nsinteger i = 0; i < 5; i++) {
UIButton *alightbutton = (UIButton *) [self.windowviewwithtag:array[i]];
if ([Alightbutton.currentbackgroundimage isequal:[uiimageimagenamed:@ "1"]) {
[Alightbutton setbackgroundimage:[uiimage imagenamed:@ "2"]forstate:uicontrolstatenormal];
} else {
[Alightbutton setbackgroundimage:[uiimage imagenamed:@ "1"]forstate:uicontrolstatenormal];
}
}
} else{
for (Nsinteger i = 0; i < 5; i++) {
UIButton *alightbutton = (UIButton *) [self.windowviewwithtag:array[i]];
if ([Alightbutton.currentbackgroundimage isequal:[uiimageimagenamed:@ "2"]) {
[Alightbutton setbackgroundimage:[uiimage imagenamed:@ "1"]forstate:uicontrolstatenormal];
} else {
[Alightbutton setbackgroundimage:[uiimage imagenamed:@ "2"]forstate:uicontrolstatenormal];
}
}
}
}

Lightsout--easy

Related Article

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.