iOS-封装小眼睛

app里面涉及到密码的地方,可能都需要使用到“小眼睛”,如下:


小眼睛.gift

注册,登录,交易密码,忘记密码,太多地方需要用到了,所以就简单封装了一下。调用的时候简易方便。
代码也是简单得很。

.h文件中

#import 

typedef void (^ActionBlock)(BOOL selected);
@interface SmallEye : UIButton
@property (nonatomic ,copy)ActionBlock actionBlock;
@end

.m文件中

#import "SmallEye.h"
@implementation SmallEye
-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame: frame]) {
        [self setImage:[UIImage imageNamed:@"eye2"] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"eye1"] forState:UIControlStateSelected];
        [self addTarget:self action:@selector(selectedChanged) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return self;
}
-(void)selectedChanged{
    self.selected = !self.selected;
    _actionBlock(self.selected);
}
@end

ok!!!已经实现了。看看调用好了...

SmallEye *eyeBtn = [[SmallEye alloc]initWithFrame:CGRectMake(100, 100, 80, 40)];
eyeBtn.actionBlock = ^(BOOL selected) {
        self.view.backgroundColor = selected ? [UIColor redColor] : [UIColor whiteColor];
    };
    
[self.view addSubview:eyeBtn];

非常渴望得到您的指点。

你可能感兴趣的:(iOS-封装小眼睛)