gpt4 book ai didi

ios - 如何在 Objective-C 中的按钮上制作 3 个可点击区域

转载 作者:行者123 更新时间:2023-11-29 01:17:18 25 4
gpt4 key购买 nike

我是 objective-C 的新手,我创建了一个项目,它使用一个按钮来更改我 View 中的一些元素。我希望根据我单击按钮的哪一部分,它显示与以前不同的元素。例如,当我单击按钮的左侧区域时,会出现一个红色圆圈,当我单击中间区域时,会出现蓝色圆圈,右侧区域会出现绿色圆圈。我尝试在 google 和 stackoverflow 上搜索,但我无法理解在这种情况下如何使用选择器。有人可以帮我吗?

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

最佳答案

我帮你。

首先,您需要以编程方式或通过 xib 或 Storyboard创建小型自定义 View

   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]];
}

谢谢你:-)

关于ios - 如何在 Objective-C 中的按钮上制作 3 个可点击区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35010834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com