ios – ObjectiveC – UIButton在突出显示选中时保持突出显示选定,背景颜色和字体颜色会发生变化

ios – ObjectiveC – UIButton在突出显示选中时保持突出显示选定,背景颜色和字体颜色会发生变化,第1张

概述我已经使用接口构建器为不同的时隙创建了以下UIButton,并为搜索创建了UIButton.我希望当用户点击它时,UIButton在不同的时隙中保持选中/突出显示.并且背景颜色和字体颜色也会改变(参见图片).而且,用户一次只能选择一个时隙. UIButton与不同的时间段 我想要实现的是什么按钮 码 #import "Search.h"#import <QuartzCore/QuartzCore 我已经使用接口构建器为不同的时隙创建了以下UIbutton,并为搜索创建了UIbutton.我希望当用户点击它时,UIbutton在不同的时隙中保持选中/突出显示.并且背景颜色和字体颜色也会改变(参见图片).而且,用户一次只能选择一个时隙.

UIbutton与不同的时间段

我想要实现的是什么按钮

#import "Search.h"#import <QuartzCore/QuartzCore.h>@interface Search(){}@end@implementation Search@synthesize btn1;@synthesize btn2;@synthesize btn3;@synthesize btn4;@synthesize btn5;@synthesize btn6;@synthesize btn7;@synthesize btn8;@synthesize btn9;@synthesize btnSearch;- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    _borderBox.layer.shadowRadius  = 5;    _borderBox.layer.shadowcolor   = [UIcolor colorWithRed:211.f/255.f green:211.f/255.f blue:211.f/255.f Alpha:1.f].CGcolor;    _borderBox.layer.shadowOffset  = CGSizeMake(0.0f,0.0f);    _borderBox.layer.shadowOpacity = 0.9f;    _borderBox.layer.masksToBounds = NO;    btn1.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;    btn1.layer.borderWIDth =1.0f;    btn2.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;    btn2.layer.borderWIDth =1.0f;    btn3.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;    btn3.layer.borderWIDth =1.0f;    btn4.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;    btn4.layer.borderWIDth =1.0f;    btn5.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;    btn5.layer.borderWIDth =1.0f;    btn6.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;    btn6.layer.borderWIDth =1.0f;    btn7.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;    btn7.layer.borderWIDth =1.0f;    btn8.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;    btn8.layer.borderWIDth =1.0f;    btn9.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;    btn9.layer.borderWIDth =1.0f;}-(voID)vIEwWillAppear:(BOol)animated{}- (voID)vIEwDIDAppear:(BOol)animated{    [super vIEwDIDAppear:animated];}- (voID)vIEwDIDdisappear:(BOol)animated{    [super vIEwDIDdisappear:animated];}+(voID)makebuttoncolored:(UIbutton*)button color1:(UIcolor*) color{    CALayer *layer = button.layer;    layer.cornerRadius = 8.0f;    layer.masksToBounds = YES;    layer.borderWIDth = 4.0f;    layer.opacity = .3;//    layer.bordercolor = [UIcolor colorWithWhite:0.4f Alpha:0.2f].CGcolor;    CAGradIEntLayer *colorLayer = [CAGradIEntLayer layer];    colorLayer.cornerRadius = 8.0f;    colorLayer.frame = button.layer.bounds;    //set gradIEnt colors    colorLayer.colors = [NSArray arrayWithObjects:                     (ID) color.CGcolor,(ID) color.CGcolor,nil];    //set gradIEnt locations    colorLayer.locations = [NSArray arrayWithObjects:                        [NSNumber numberWithfloat:0.0f],[NSNumber numberWithfloat:1.0f],nil];    [button.layer addSublayer:colorLayer];}
解决方法 我能够实现你正在进行的功能,下面是我的工作方式.

I created the design via storyboard and connected all the 9 button’s actions methods to a single Selector method,insIDe the action method with the help sender parameter we can get the selected buttons reference and use it.

- (IBAction)btnpressed:(UIbutton*)sender {/* Below for loop works as a reset for setting the default colour of button and to not select the same one twice*/for (UIbutton* button in buttons) {    [button setSelected:NO];    [button setBackgroundcolor:[UIcolor whitecolor]];    [button setUserInteractionEnabled:true];// [button setTitlecolor:[UIcolor blackcolor] forState:UIControlStatenormal];    [button setTitlecolor:[UIcolor blackcolor] forState:UIControlStateSelected];}NSInteger tag = sender.tag;        // Here we get the sender tag,which we can use for our needs. Also we can directly use the sender and get the Title or whatsoever needed./*Now below line works as a toggle for the button where multiple buttons can't be selected at the same time.*/sender.selected = ! sender.selected;      if(sender.selected){/* Here we set the color for the button and handle the selected function*/    [sender setSelected:YES];    [sender setUserInteractionEnabled:false];    [sender setBackgroundcolor:[UIcolor magentacolor]];}}

您还可以使用“sender.Layer”属性为按钮添加自定义图层.

整个代码添加在下面,

所有按钮的动作都需要连接到单个选择器方法,
– (IBAction)btnpressed:(UIbutton *)寄件人;

#import "VIEwController.h"@interface VIEwController ()@property (weak,nonatomic) IBOutlet UIVIEw *mainVIEwol;@property (weak,nonatomic) IBOutlet UIbutton *btn1;@property (weak,nonatomic) IBOutlet UIbutton *btn2;@property (weak,nonatomic) IBOutlet UIbutton *btn3;@property (weak,nonatomic) IBOutlet UIbutton *btn4;@property (weak,nonatomic) IBOutlet UIbutton *btn5;@property (weak,nonatomic) IBOutlet UIbutton *btn6;@property (weak,nonatomic) IBOutlet UIbutton *btn7;@property (weak,nonatomic) IBOutlet UIbutton *btn8;@property (weak,nonatomic) IBOutlet UIbutton *btn9;@end@implementation VIEwControllerNSArray* buttons;- (voID)vIEwDIDLoad {    [super vIEwDIDLoad];    buttons = [NSArray arrayWithObjects:_btn1,_btn2,_btn3,_btn4,_btn5,_btn6,_btn7,_btn8,_btn9,nil];    self.mainVIEwol.layer.shadowRadius  = 5;    self.mainVIEwol.layer.shadowcolor   = [UIcolor colorWithRed:211.f/255.f green:211.f/255.f blue:211.f/255.f Alpha:1.f].CGcolor;    self.mainVIEwol.layer.shadowOffset  = CGSizeMake(0.0f,0.0f);    self.mainVIEwol.layer.shadowOpacity = 0.9f;    self.mainVIEwol.layer.masksToBounds = NO;    /* I Have added the 9 button's in an array and used it to reduce the lines of code and for easy understanding as well*/    for (UIbutton* button in buttons) {        button.layer.bordercolor = [UIcolor lightGraycolor].CGcolor;        button.layer.borderWIDth =1.0f;    }}- (IBAction)btnpressed:(UIbutton*)sender {    for (UIbutton* button in buttons) {        [button setSelected:NO];        [button setBackgroundcolor:[UIcolor whitecolor]];        [button setUserInteractionEnabled:true];     // [button setTitlecolor:[UIcolor blackcolor] forState:UIControlStatenormal];        //Based on your needs and colour variant you cant add propertIEs to the button for different control states.        [button setTitlecolor:[UIcolor blackcolor] forState:UIControlStateSelected];    }    NSInteger tag = sender.tag;    sender.selected = ! sender.selected;    if(sender.selected)    {        [sender setSelected:YES];        [sender setUserInteractionEnabled:false];        [sender setBackgroundcolor:[UIcolor purplecolor]];        sender.backgroundcolor = [UIcolor magentacolor];    }}@end

最后的结果

Ignore the delay in button selection,it is caused by the vIDeo to gif conversion.

希望这可以帮助.

总结

以上是内存溢出为你收集整理的ios – ObjectiveC – UIButton在突出显示/选中时保持突出显示/选定,背景颜色和字体颜色会发生变化全部内容,希望文章能够帮你解决ios – ObjectiveC – UIButton在突出显示/选中时保持突出显示/选定,背景颜色和字体颜色会发生变化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1073414.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存