iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – ObjectiveC – UIButton在突出显示/选中时保持突出显示/选定,背景颜色和字体颜色会发生变化大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经使用接口构建器为不同的时隙创建了以下UIButton,并为搜索创建了UIButton.我希望当用户点击它时,UIButton在不同的时隙中保持选中/突出显示.并且背景颜色和字体颜色也会改变(参见图片).而且,用户一次只能选择一个时隙.

UIButton与不同的时间段

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

我想要实现的是什么按钮

ios – ObjectiveC – 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];

}

解决方法

我能够实现你正在进行功能,下面是我的工作方式.

- (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 ViewController

NSArray* 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

最后的结果

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

希望这可以帮助.

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。