简易的关灯游戏设计



//初始化

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.isOpen = YES;

        self.backgroundColor = [UIColor whiteColor];

    }

    return self;

}

//改变自己的颜色

- (void)exchange

{

    self.backgroundColor = _isOpen ? [UIColor blackColor]:[UIColor whiteColor];

    _isOpen = !_isOpen;

}

//改变周围的颜色

- (void)suroundChange

{

    TouchView *left = (TouchView *)[self.superview viewWithTag:self.tag - 1];

    TouchView *right = (TouchView *)[self.superview viewWithTag:self.tag + 1];

    

    TouchView *up = (TouchView *)[self.superview viewWithTag:self.tag - 20];

    TouchView *down = (TouchView *)[self.superview viewWithTag:self.tag + 20];

    [left exchange];

    [right exchange];

    [up exchange];

    [down exchange];

 }

//点击实现颜色的跳转

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

      NSLog(@"自己的tag:%ld",self.tag);

    [self exchange];

    [self suroundChange];

  }


视图控制器里面的实现 :

    MainViewController *main = [[MainViewController alloc]init];

    self.window.rootViewController = main;

    [main release];


//实现6*8的视图:(在主视图控制器里实现)   Length是定义的宏:

    for (NSInteger i = 0; i < 48; i++) {

        TouchView *touch = [[TouchView alloc]initWithFrame:CGRectMake(5 + i % 6 * Length, 25 + i / 6 * Length , Length - 1, Length - 1)];

        touch.tag = 100 + i / 6 * 20 + i % 6;

        [self.view addSubview:touch];

        [touch release];

 }



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: