iOS开发系列之常用自定义控件开发集—继承UIButton控件代码快速创建按钮开发

在开发过程中,我们有时候需要用代码编写UI,如果按照一般流程代码编写按钮需要写很多行代码非常无聊,尤其是需要创建很多按钮时更无聊,我这里就对UIButton进行继承包装一行代码就可以创建出按钮,实现方式如下:
WHC_Button.h头文件如下:

//
//  WHC_Button.h
//  WHC_Button
//
//  Created by 吴海超 on 15/3/25.
//  Copyright (c) 2015年 吴海超. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface WHC_Button : UIButton

+ (UIButton*)createButton:(CGRect)frame titles:(NSArray*)titles images:(NSArray*)images sf:(id)sf action:(SEL)selector;

+ (UIButton*)createButton:(CGRect)frame titles:(NSArray*)titles imageNames:(NSArray*)imageNames sf:(id)sf action:(SEL)selector;

+ (UIButton*)createButton:(CGRect)frame titles:(NSArray*)titles titleColors:(NSArray*)titleColors backColors:(NSArray*)backColors sf:(id)sf action:(SEL)selector;

+ (UIButton*)createButton:(CGRect)frame titles:(NSArray*)titles titleColors:(NSArray*)titleColors backColors:(NSArray*)backColors radius:(CGFloat)radius sf:(id)sf action:(SEL)selector;
@end

WHC_Button.m源文件如下:

//
//  WHC_Button.m
//  WHC_Button
//
//  Created by 吴海超 on 15/3/25.
//  Copyright (c) 2015年 吴海超. All rights reserved.
//

#import "WHC_Button.h"

@interface WHC_Button (){
    UIColor * _selectedBackColor;
    UIColor * _normalBackColor;
    SEL       _selector;
    id        _sf;
}

@end

@implementation WHC_Button

- (void)setSelectedBackColor:(UIColor *)color{
    _selectedBackColor = color;
}

- (void)setNormalBackColor:(UIColor*)color{
    _normalBackColor = color;
}

- (void)setSelector:(SEL)sel{
    _selector = sel;
}

- (void)setSf:(id)sf{
    _sf = sf;
}

+ (WHC_Button*)baseCreateButton:(CGRect)frame titles:(NSArray*)titles titleColors:(NSArray*)titleColors backColors:(NSArray*)backColors images:(NSArray*)images radius:(CGFloat)radius sf:(id)sf action:(SEL)selector{
    WHC_Button  * btn = [WHC_Button buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    if(titles == nil)titles = @[];
    if(titleColors == nil)titleColors = @[];
    if(images == nil)images = @[];
    if(backColors == nil)backColors = @[];
    NSInteger titleCount = titles.count;
    if(titleCount > 1){
        titleCount = 2;
    }
    NSInteger imageCount = images.count;
    if(imageCount > 1){
        imageCount = 2;
    }
    NSInteger titleColorCount = titleColors.count;
    if (titleColorCount > 1) {
        titleColorCount = 2;
    }
    NSInteger backColorCount = backColors.count;
    if(backColorCount){
        [btn setBackgroundColor:backColors[0]];
        btn.normalBackColor = backColors[0];
    }
    if(backColorCount > 1){
        backColorCount = 2;
        btn.selectedBackColor = backColors[1];
        [btn addTarget:btn action:@selector(clickDown:) forControlEvents:UIControlEventTouchDown];
    }
    for (int i = 0; i < titleCount; i++) {
        [btn setTitle:titles[i] forState:i == 0 ? UIControlStateNormal : UIControlStateHighlighted];
    }
    for (int i = 0; i < imageCount; i++) {
        [btn setBackgroundImage:images[i] forState:i == 0 ? UIControlStateNormal : UIControlStateHighlighted];
    }
    for (int i = 0; i < titleColorCount; i++) {
        [btn setTitleColor:titleColors[i] forState:i == 0 ? UIControlStateNormal : UIControlStateHighlighted];
    }
    btn.selector = selector;
    btn.sf = sf;
    btn.layer.cornerRadius = radius;
    [btn addTarget:btn action:@selector(clickUp:) forControlEvents:UIControlEventTouchUpInside];
    return btn;

}

- (void)clickDown:(UIButton*)sender{
    if(_selectedBackColor != nil){
        sender.backgroundColor = _selectedBackColor;
    }
}

- (void)clickUp:(UIButton*)sender{
    if(_normalBackColor != nil){
        sender.backgroundColor = _normalBackColor;
    }
    if([_sf respondsToSelector:_selector]){
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [_sf performSelector:_selector withObject:sender];
    }
#pragma clang diagnostic pop
}

+ (WHC_Button*)createButton:(CGRect)frame titles:(NSArray*)titles images:(NSArray*)images sf:(id)sf action:(SEL)selector{
    return [self baseCreateButton:frame titles:titles titleColors:nil backColors:nil images:images radius:0.0 sf:sf action:selector];
}

+ (WHC_Button*)createButton:(CGRect)frame titles:(NSArray*)titles imageNames:(NSArray*)imageNames sf:(id)sf action:(SEL)selector{
    NSMutableArray * images = [NSMutableArray array];
    for (NSString * imageName in imageNames) {
        [images addObject:[UIImage imageNamed:imageName]];
    }
    return [self baseCreateButton:frame titles:titles titleColors:nil backColors:nil images:images radius:0.0 sf:sf action:selector];
}

+ (WHC_Button*)createButton:(CGRect)frame titles:(NSArray*)titles titleColors:(NSArray*)titleColors backColors:(NSArray*)backColors sf:(id)sf action:(SEL)selector{
    return [self baseCreateButton:frame titles:titles titleColors:titleColors backColors:backColors images:nil radius:0.0 sf:sf action:selector];
}

+ (WHC_Button*)createButton:(CGRect)frame titles:(NSArray*)titles titleColors:(NSArray*)titleColors backColors:(NSArray*)backColors radius:(CGFloat)radius sf:(id)sf action:(SEL)selector{
    return [self baseCreateButton:frame titles:titles titleColors:titleColors backColors:backColors images:nil radius:radius sf:sf action:selector];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值