關燈遊戲源碼(iOS),關燈遊戲源碼ios

來源:互聯網
上載者:User

關燈遊戲源碼(iOS),關燈遊戲源碼ios
就是點一下燈 它本身和周圍4盞燈會變色

ViewController.m檔案

#import "ViewController.h"#import "UIView+change.h"@interface ViewController ()@property(assign,nonatomic)int totalColums;@property(assign,nonatomic)NSInteger tag;@property(strong,nonatomic)UIButton *btn;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    CGFloat height=50;    CGFloat width=50;    self.view.tag=1000;    self.totalColums=5;    CGFloat marginX=(self.view.frame.size.width-width*self.totalColums)/(self.totalColums+1);    CGFloat marginY=30;    for (int i=0; i<45; i++) {        int row=i/self.totalColums;        int col=i%self.totalColums;        CGFloat appX=marginX+col*(width+marginX);        CGFloat appY=marginY+row*(height+marginY);        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(appX, appY, width, height)];        btn.backgroundColor=[UIColor redColor];        [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];        btn.tag=i;        [self.view addSubview:btn];    }}/** *  點擊事件 * * */-(void)onClick:(UIButton *)btn{    [btn change];    self.btn=btn;    self.tag=btn.tag;    [self lie];    [self sigiao];    [self hang];    [self qita];}/** *  最左邊一列和最右邊一列 */-(void)lie{            if ((self.tag%self.totalColums==0||self.tag%self.totalColums==4)&&(self.tag/self.totalColums!=0&&self.tag/self.totalColums!=8)) {        [[self.view viewWithTag:self.tag+self.totalColums] change];        [[self.view viewWithTag:self.tag-self.totalColums] change];        if (self.tag%self.totalColums==0) {            [[self.view viewWithTag:self.tag+1] change];        }else if(self.tag%self.totalColums==4){            [[self.view viewWithTag:self.tag-1] change];        }    }}/** *  最上面一行和最下面一行 */-(void)hang{            if ((self.tag/self.totalColums==0||self.tag/self.totalColums==8)&&(self.tag%self.totalColums!=0&&self.tag%self.totalColums!=4)) {        [[self.view viewWithTag:self.tag-1] change];        [[self.view viewWithTag:self.tag+1] change];        if (self.tag/self.totalColums==0) {            [[self.view viewWithTag:self.tag+self.totalColums] change];        }else if(self.tag/self.totalColums==8){            [[self.view viewWithTag:self.tag-self.totalColums] change];        }    }    }/** *  四個角 */-(void)sigiao{    if (self.tag==0) {        [[self.view viewWithTag:self.tag+1] change];        [[self.view viewWithTag:self.tag+self.totalColums] change];    }else if (self.tag==4){        [[self.view viewWithTag:self.tag-1] change];        [[self.view viewWithTag:self.tag+self.totalColums] change];    }else if(self.tag==45-self.totalColums){        [[self.view viewWithTag:self.tag+1] change];        [[self.view viewWithTag:self.tag-self.totalColums] change];    }else if(self.tag==44){        [[self.view viewWithTag:self.tag-1] change];        [[self.view viewWithTag:self.tag-self.totalColums] change];    }}/** *  其他的 */-(void)qita{        if (self.tag/self.totalColums!=0&&self.tag/self.totalColums!=8&&self.tag%self.totalColums!=0&&self.tag%self.totalColums!=4) {        [[self.view viewWithTag:self.tag-self.totalColums]change];        [[self.view viewWithTag:self.tag-1]change];        [[self.view viewWithTag:self.tag+self.totalColums]change];        [[self.view viewWithTag:self.tag+1] change];    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
#import "ViewController.h"#import "UIView+change.h"@interface ViewController ()@property(assign,nonatomic)int totalColums;@property(assign,nonatomic)NSInteger tag;@property(strong,nonatomic)UIButton *btn;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    CGFloat height=50;    CGFloat width=50;    self.view.tag=1000;    self.totalColums=5;    CGFloat marginX=(self.view.frame.size.width-width*self.totalColums)/(self.totalColums+1);    CGFloat marginY=30;    for (int i=0; i<45; i++) {        int row=i/self.totalColums;        int col=i%self.totalColums;        CGFloat appX=marginX+col*(width+marginX);        CGFloat appY=marginY+row*(height+marginY);        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(appX, appY, width, height)];        btn.backgroundColor=[UIColor redColor];        [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown];        btn.tag=i;        [self.view addSubview:btn];    }}/** *  點擊事件 * * */-(void)onClick:(UIButton *)btn{    [btn change];    self.btn=btn;    self.tag=btn.tag;    [self lie];    [self sigiao];    [self hang];    [self qita];}/** *  最左邊一列和最右邊一列 */-(void)lie{            if ((self.tag%self.totalColums==0||self.tag%self.totalColums==4)&&(self.tag/self.totalColums!=0&&self.tag/self.totalColums!=8)) {        [[self.view viewWithTag:self.tag+self.totalColums] change];        [[self.view viewWithTag:self.tag-self.totalColums] change];        if (self.tag%self.totalColums==0) {            [[self.view viewWithTag:self.tag+1] change];        }else if(self.tag%self.totalColums==4){            [[self.view viewWithTag:self.tag-1] change];        }    }}/** *  最上面一行和最下面一行 */-(void)hang{            if ((self.tag/self.totalColums==0||self.tag/self.totalColums==8)&&(self.tag%self.totalColums!=0&&self.tag%self.totalColums!=4)) {        [[self.view viewWithTag:self.tag-1] change];        [[self.view viewWithTag:self.tag+1] change];        if (self.tag/self.totalColums==0) {            [[self.view viewWithTag:self.tag+self.totalColums] change];        }else if(self.tag/self.totalColums==8){            [[self.view viewWithTag:self.tag-self.totalColums] change];        }    }    }/** *  四個角 */-(void)sigiao{    if (self.tag==0) {        [[self.view viewWithTag:self.tag+1] change];        [[self.view viewWithTag:self.tag+self.totalColums] change];    }else if (self.tag==4){        [[self.view viewWithTag:self.tag-1] change];        [[self.view viewWithTag:self.tag+self.totalColums] change];    }else if(self.tag==45-self.totalColums){        [[self.view viewWithTag:self.tag+1] change];        [[self.view viewWithTag:self.tag-self.totalColums] change];    }else if(self.tag==44){        [[self.view viewWithTag:self.tag-1] change];        [[self.view viewWithTag:self.tag-self.totalColums] change];    }}/** *  其他的 */-(void)qita{        if (self.tag/self.totalColums!=0&&self.tag/self.totalColums!=8&&self.tag%self.totalColums!=0&&self.tag%self.totalColums!=4) {        [[self.view viewWithTag:self.tag-self.totalColums]change];        [[self.view viewWithTag:self.tag-1]change];        [[self.view viewWithTag:self.tag+self.totalColums]change];        [[self.view viewWithTag:self.tag+1] change];    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

類目檔案UIView+change.h

#import <UIKit/UIKit.h>@interface UIView (change)/** *  變顏色 */-(void)change;@end

UIView+change.m

#import "UIView+change.h"@implementation UIView (change)-(void)change{    if (self.backgroundColor==[UIColor redColor]) {        self.backgroundColor=[UIColor blueColor];    }else{        self.backgroundColor=[UIColor redColor];    }}@end

運行效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.