如何在Objective-C中的按钮上制作3个可点击区域
Posted
技术标签:
【中文标题】如何在Objective-C中的按钮上制作3个可点击区域【英文标题】:How to make 3 clickable area on a button in Objective-C 【发布时间】:2016-01-26 09:42:55 【问题描述】:我是 Objective-C 的新手,我创建了一个项目,该项目使用按钮从我的视图中更改某些元素。我希望这取决于我单击按钮的哪个部分,它显示的元素与以前不同。 例如,当我从按钮单击左侧区域时,会出现一个红色圆圈,当我单击中间时,出现的是蓝色圆圈,右侧区域是绿色圆圈。 我尝试在 google 和 *** 上进行搜索,但我无法理解在这种情况下如何使用选择器。有人可以帮我吗?
PS:对不起,我的英语不太好
【问题讨论】:
如何检测用户点击的位置,或者使用带有后端 3 个按钮的自定义项?你试过什么? 也许您正在寻找UISegmentedControl
?
Larme :这是给学校的。我的老师希望我们使用一个按钮,圆圈的颜色取决于我点击的女巫部分。他给出的唯一线索是我必须使用选择器。当我通过其他项目中的代码创建按钮时,我已经使用了选择器。但是这里我不明白如何使用选择器来选择按钮的一部分。
RolandasR:我在研究中从未遇到过这种方法。谢谢,我会挖的
【参考方案1】:
我帮你。
首先,您需要以编程方式或通过 xib 或故事板创建小型自定义视图
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]];
谢谢你:-)
【讨论】:
您的解决方案很有趣,但我认为最后我会使用 RolandasR 的解决方案。这对我来说是最简单的,我认为我的老师无论如何都会验证。我勾选了您的答案,它可以用于其他时间,也可以用于我的其他时间。感谢您的帮助。以上是关于如何在Objective-C中的按钮上制作3个可点击区域的主要内容,如果未能解决你的问题,请参考以下文章