iOS工具方法

iOS链式编程(试写)

2020-03-27  本文已影响0人  M_PI_4

RTButton.m

#import "RTButton.h"

@implementation RTButton

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if(!self) {
        
    }
    
    return self;
}

- (RTButton * (^)(UIColor * color))btnNC {
    return ^(UIColor *color) {
        [self setTitleColor:color forState:UIControlStateNormal];
        return self;
    };
}

- (RTButton * _Nonnull (^)(UIColor * _Nonnull))btnSC {
    return ^(UIColor *color) {
        [self setTitleColor:color forState:UIControlStateSelected];
        return self;
    };
}

@end 

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    
    RTButton *btn = [RTButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"Click Me" forState:UIControlStateNormal];
    btn.frame = CGRectMake(50, 100, 100, 60);
    btn.btnSC([UIColor redColor]).btnNC([UIColor blueColor]);
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
}
上一篇下一篇

猜你喜欢

热点阅读