Turn off the light game source code (iOS), turn off the light game source code ios

Source: Internet
Author: User

Turn off the light game source code (iOS), turn off the light game source code ios
Just click the light itself and the four lights around it will change color.

ViewController. m file

# 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 (onC Lick :) forControlEvents: UIControlEventTouchDown]; btn. tag = I; [self. view addSubview: btn] ;}}/*** Click Event ***/-(void) onClick :( UIButton *) btn {[btn change]; self. btn = btn; self. tag = btn. tag; [self lie]; [self sigiao]; [self hang]; [self qita];} /*** the leftmost column and rightmost column */-(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] ;}}/ *** top and bottom rows */-(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];}/*** Four Corners */-(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. v Iew viewWithTag: self. tag-self.totalColums] change];}/*** other */-(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 didreceivemorywarning]; // 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 (onC Lick :) forControlEvents: UIControlEventTouchDown]; btn. tag = I; [self. view addSubview: btn] ;}}/*** Click Event ***/-(void) onClick :( UIButton *) btn {[btn change]; self. btn = btn; self. tag = btn. tag; [self lie]; [self sigiao]; [self hang]; [self qita];} /*** the leftmost column and rightmost column */-(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] ;}}/ *** top and bottom rows */-(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];}/*** Four Corners */-(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. v Iew viewWithTag: self. tag-self.totalColums] change];}/*** other */-(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 didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

CATEGORY file UIView + change. h

# Import <UIKit/UIKit. h> @ interface UIView (change)/*** change color */-(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

Running Effect

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.