iOS UIButton点击相邻圆圈换颜色

2016-03-09  本文已影响36人  yymyb

#import "ViewController.h"

@interface ViewController ()

@property(strong,nonatomic)UIButton *button;

@property(copy,nonatomic)NSString *str;

@property(copy,nonatomic)NSString *str1;

@property(assign,nonatomic)CGRect rect;

@property(assign,nonatomic)BOOL a;

@end

@implementation ViewController

- (void)viewDidLoad {

   [super viewDidLoad];

   int indxe=5;

   CGFloat appW=60;

   CGFloat appH=60;

   CGFloat appjxX=(self.view.frame.size.width-appW*indxe)/(indxe+1);

   CGFloat appjxY=appjxX;

   CGFloat apptopY=20;

   for (int i=0; i<45; i++) {

       int X=i%indxe;

       int Y=i/indxe;

        self.button=[[UIButton alloc]initWithFrame:CGRectMake((appjxX+appW)*X+appjxX, (appH+appjxY)*Y+appjxY+apptopY, appW, appH)];

        self.str=[NSString stringWithFormat:@"1.jpg"];

        self.str1=[NSString stringWithFormat:@"2.jpg"];

       [self.button setBackgroundImage:[UIImage imageNamed:self.str] forState:(UIControlStateNormal)];

       [self.button setBackgroundImage:[UIImage imageNamed:self.str1] forState:UIControlStateHighlighted];

       self.button.tag=i;

       [self.button addTarget:self action:@selector(change:) forControlEvents:(UIControlEventTouchDown)];

       [self.view addSubview:self.button];

   }

}

-(void)change:(UIButton *)button

{

   int i=(int)button.tag;

   int arr[4]={i+1,i-1,i-5,i+5};

   if (i%5==0)

   {

       arr[1]=i+1;

   }

   else if (i%5==4)

   {

       arr[0]=i-1;

   }

   NSLog(@"%d",i);

   if (self.a==YES) {

       [self red:button];

       for (int i=0; i<=4; i++) {

       self.button=[[UIButton alloc]init];

       self.button.frame=[self.view viewWithTag:arr[i]].frame;

       [self red:self.button];

       [self.view addSubview:self.button];

       }

   }

   else

   {

       if (i%5==0) {

           [self yea:button];

           self.button=[self.view viewWithTag:i+1];

           [self yea:self.button];

           [self.view addSubview:self.button];

           [self yea:button];

           self.button=[self.view viewWithTag:i+1];

           [self yea:self.button];

           [self.view addSubview:self.button];

           [self yea:button];

           self.button=[self.view viewWithTag:i-5];

           [self yea:self.button];

           [self.view addSubview:self.button];

           [self yea:button];

           self.button=[self.view viewWithTag:i+5];

           [self yea:self.button];

           [self.view addSubview:self.button];

       }

       else if (i%5==4)

       {

           [self yea:button];

           self.button=[self.view viewWithTag:i-1];

           [self yea:self.button];

           [self.view addSubview:self.button];

           [self yea:button];

           self.button=[self.view viewWithTag:i-1];

           [self yea:self.button];

           [self.view addSubview:self.button];

           [self yea:button];

           self.button=[self.view viewWithTag:i-5];

           [self yea:self.button];

           [self.view addSubview:self.button];

           [self yea:button];

           self.button=[self.view viewWithTag:i+5];

           [self yea:self.button];

           [self.view addSubview:self.button];

       }

       else

       {

       [self yea:button];

       self.button=[self.view viewWithTag:i+1];

       [self yea:self.button];

       [self.view addSubview:self.button];

       [self yea:button];

       self.button=[self.view viewWithTag:i-1];

       [self yea:self.button];

       [self.view addSubview:self.button];

       [self yea:button];

       self.button=[self.view viewWithTag:i-5];

       [self yea:self.button];

       [self.view addSubview:self.button];

       [self yea:button];

       self.button=[self.view viewWithTag:i+5];

       [self yea:self.button];

       [self.view addSubview:self.button];

       }

   }

}

-(void)red:(UIButton *)button

{

   [button setBackgroundImage:[UIImage imageNamed:self.str1] forState:UIControlStateNormal];

   self.a=NO;

}

-(void)yea:(UIButton *)button

{

   [button setBackgroundImage:[UIImage imageNamed:self.str] forState:UIControlStateNormal];

   self.a=YES;

}

@end

上一篇下一篇

猜你喜欢

热点阅读