美文网首页iOS开发攻城狮的集散地iOS底层基础知识
iOS | 继承UIControl才是自定义button的正确姿

iOS | 继承UIControl才是自定义button的正确姿

作者: Lol刀妹 | 来源:发表于2019-04-18 22:15 被阅读163次

    前言

    自定义按钮的方式不少,当遇到复杂的按钮时,我一般是继承UIButton,然后在button上view,这种招式简单粗暴,屡试不爽。掌握这招,从此无视任何复杂按钮。

    虽说屡试不爽,但我还是有不爽的时候:
    UIButton本身就有一个label和一个imageView,我放在那里不用,又给它放个label和imageView上去,给人的感觉有点多余和别扭;用吧,它这两个控件又是readonly的,并不给我充分发挥的空间。

    有点难受。

    强迫症患者接受不了

    再多看一眼UIButton

    然后我就想看看UIButton是怎么来的:

    @interface UIButton : UIControl <NSCoding>
    @interface UIControl : UIView
    

    UIButton继承自UIControl,UIControl继承自UIView。

    UIView就不说了,来看看UIControl的部分属性和方法:

    @property(nonatomic,getter=isEnabled) BOOL enabled;
    @property(nonatomic,getter=isSelected) BOOL selected;
    @property(nonatomic,getter=isHighlighted) BOOL highlighted;  
    @property(nonatomic,readonly) UIControlState state;
    - (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
    

    是不是很熟悉?你自定义button时用到的那些属性和方法,全在UIControl中,应有尽有。

    也就是说,即使是自定义一个比较复杂的按钮,也不需要继承UIButton,继承UIControl这个更轻量的控件就完全够用了。UIButton能给你的,UIControl都能给你。

    栗子

    下面是基于UIControl的简单自定义按钮:

    效果:

    Controller里的代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(90, 150, 200, 40)];
        [self.view addSubview:button];
        button.backgroundColor = [UIColor orangeColor];
        button.selected = NO; // 默认处于未选中状态
        [button addTarget:self action:@selector(myButtonClicked:) forControlEvents:UIControlEventTouchDown];
    }
    
    - (void)myButtonClicked:(MyButton *)sender {
        sender.selected = !sender.isSelected;
    }
    

    按钮的代码:

    @interface MyButton : UIControl
    
    @end
    
    ==================================
    
    @interface MyButton ()
    
    @property (nonatomic, strong) UILabel *label1;
    @property (nonatomic, strong) UIImageView *imageView;
    @property (nonatomic, strong) UILabel *label2;
    
    @end
    
    @implementation MyButton
    
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            //========== 子控件想怎么放就怎么放 ==========//
            self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
            [self addSubview:self.label1];
            
            self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 0, 40, 40)];
            [self addSubview:self.imageView];
            
            self.label2 = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 80, 40)];
            [self addSubview:self.label2];
        }
        return self;
    }
    
    - (void)setSelected:(BOOL)selected {
        [super setSelected:selected];
        
        if (selected) {
            self.label1.text = @"选中";
            self.label1.font = [UIFont boldSystemFontOfSize:16];
            self.imageView.image = [UIImage imageNamed:@"left"];
            self.label2.text = @"呵呵";
            self.label2.font = [UIFont boldSystemFontOfSize:20];
            self.label2.textColor = [UIColor redColor];
        } else {
            self.label1.text = @"未选中";
            self.label1.font = [UIFont systemFontOfSize:14];
            self.imageView.image = [UIImage imageNamed:@"right"];
            self.label2.text = @"嘻嘻";
            self.label2.font = [UIFont systemFontOfSize:18];
            self.label2.textColor = [UIColor blueColor];
        }
    }
    
    @end
    
    

    最后

    终于不会因为继承UIButton重复添加label和imageView而耿耿于怀了。

    舒服了。

    相关文章

      网友评论

        本文标题:iOS | 继承UIControl才是自定义button的正确姿

        本文链接:https://www.haomeiwen.com/subject/hxexgqtx.html