如何在Objective-C中的按钮上创建3个可单击区域

时间:2016-01-26 09:42:56

标签: ios objective-c uibutton

我是Objective-C的新手,我创建了一个项目,使用按钮更改我视图中的某些元素。我希望这取决于我点击的按钮的哪个部分,它显示不同于以前的元素。 例如,当我从按钮点击左侧区域时,会出现一个红色圆圈,当我在中间点击时,它出现的是蓝色圆圈,在右侧区域中它是一个绿色圆圈。 我尝试在谷歌和stackoverflow上搜索,但我无法理解在这种情况下选择器如何使用。有人能帮助我吗?

PS:对不起,我的英语不是很好

1 个答案:

答案 0 :(得分:1)

我帮助你。

首先,您需要以编程方式或通过xib或storyboard

创建小型自定义视图
   UIView *viewButton = [[UIView alloc] initWithFrame: CGRectMake ( 100, 100, 300, 300)];

   [self.view addSubView:viewButton];

然后在viewButton

中创建3个按钮
 //First Button

 UIButton *buttonLeftRed = [UIButton buttonWithType:UIButtonTypeCustom];
           OR
 UIButton *buttonLeftRed = [UIButton buttonWithType:UIButtonTypeSystem];
 buttonLeftRed.frame = CGRectMake(0, 0, 100, 100);
 buttonLeftRed.tag = 1;
 [buttonLeftRed setTitle:@"Red" forState:UIControlStateNormal];
 [buttonLeftRed addTarget:self 
       action:@selector(actionPressMeOne:) forControlEvents:UIControlEventTouchUpInside];
 [viewButton addSubview:buttonLeftRed];


 //Second Button

 UIButton *buttonMiddleBlue = [UIButton buttonWithType:UIButtonTypeCustom];
           OR
 UIButton *buttonMiddleBlue = [UIButton buttonWithType:UIButtonTypeSystem];
 buttonMiddleBlue.frame = CGRectMake(100, 0, 100, 100);
 buttonMiddleBlue.tag = 2;
 [buttonMiddleBlue setTitle:@"Blue" forState:UIControlStateNormal];
 [buttonMiddleBlue addTarget:self 
       action:@selector(actionPressMeSecond:) forControlEvents:UIControlEventTouchUpInside];
 [viewButton addSubview:buttonMiddleBlue];


 //Third Button

 UIButton *buttonRightGreen = [UIButton buttonWithType:UIButtonTypeCustom];
           OR
 UIButton *buttonRightGreen = [UIButton buttonWithType:UIButtonTypeSystem];
 buttonRightGreen.frame = CGRectMake(200, 0, 100, 100);
 buttonRightGreen.tag = 3;
 [buttonRightGreen setTitle:@"Blue" forState:UIControlStateNormal];
 [buttonRightGreen addTarget:self 
       action:@selector(actionPressMeThird:) forControlEvents:UIControlEventTouchUpInside];
 [viewButton addSubview:buttonRightGreen];    


If you want to change circle button, you have to import the Quartzcore Framework
#import <QuartzCore/QuartzCore.h>

 #pragma mark - UIButton Action Methods

 //First
 - (void)actionPressMeOne:(UIButton *)sender 
 {
     NSLog(@"Pressed Button Tag is - %@",sender.tag);
     UIButton *button = sender;
    //Then do whatever you want to do here
    //half of the width
     button.layer.cornerRadius = button.frame.size.width/2.0f;
     button.layer.borderColor=[UIColor redColor].CGColor;
     button.layer.borderWidth=2.0f;
     button.clipsToBounds = YES;
     [button setBackgroundColor:[UIColor redColor]];
 }

 //Second
 - (void)actionPressMeSecond:(UIButton *)sender 
 {
     NSLog(@"Pressed Button Tag is - %@",sender.tag);
     UIButton *buttonTwo = sender;
    //Then do whatever you want to do here
    //half of the width
     buttonTwo.layer.cornerRadius = button.frame.size.width/2.0f;
     buttonTwo.layer.borderColor=[UIColor blueColor].CGColor;
     buttonTwo.layer.borderWidth=2.0f;
     buttonTwo.clipsToBounds = YES;
     [buttonTwo setBackgroundColor:[UIColor blueColor]];
 }    

 //Third
 - (void)actionPressMeThird:(UIButton *)sender 
 {
     NSLog(@"Pressed Button Tag is - %@",sender.tag);
     UIButton *buttonThree = sender;
    //Then do whatever you want to do here
    //half of the width
     buttonThree.layer.cornerRadius = button.frame.size.width/2.0f;
     buttonThree.layer.borderColor=[UIColor greenColor].CGColor;
     buttonThree.layer.borderWidth=2.0f;
     buttonThree.clipsToBounds = YES;
     [buttonThree setBackgroundColor:[UIColor greenColor]];
 }    

谢谢: - )