//.h
#import <UIKit/UIKit.h>
@protocol switchView <NSObject>
- (void)passwordIsVisibleWithBool:(BOOL)secureTextEntry;
@end
@interface switchView : UIView
@property (nonatomic, strong) UIView * backView;
@property (nonatomic, strong) UIView * thumClose;
@property (nonatomic, strong) UIView * thumOpen;
@property (nonatomic, strong) id<switchView> delegate;
@end
#import "switchView.h"
#define heightSwich 24
#define widthSwich 50
#define borderCustomColor kColorWithRGB(160, 160, 160, 1).CGColor;
@implementation switchView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addAllViews];
}
return self;
}
- (void)addAllViews{
[self addSubview:self.backView];
[self addSubview:self.thumClose];
[self addSubview:self.thumOpen];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
[self addGestureRecognizer:tap];
}
- (void)tapGesture:(UITapGestureRecognizer *)tap{
self.thumOpen.hidden = !self.thumOpen.hidden;
self.thumClose.hidden = !self.thumClose.hidden;
[_delegate passwordIsVisibleWithBool:self.thumOpen.hidden];
}
- (UIView *)backView{
if (!_backView) {
_backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, widthSwich, heightSwich)];
_backView.layer.cornerRadius = heightSwich / 2;
_backView.layer.masksToBounds = YES;
_backView.layer.borderWidth = 1;
_backView.layer.borderColor = borderCustomColor;
}
return _backView;
}
- (UIView *)thumClose{
if (!_thumClose) {
_thumClose = [[UIView alloc] initWithFrame:CGRectMake(0, 0, heightSwich, heightSwich)];
_thumClose.layer.cornerRadius = heightSwich / 2;
_thumClose.layer.masksToBounds = YES;
_thumClose.layer.borderWidth = 1;
_thumClose.layer.borderColor = borderCustomColor;
}
return _thumClose;
}
- (UIView *)thumOpen{
if (!_thumOpen) {
_thumOpen = [[UIView alloc] initWithFrame:CGRectMake(widthSwich - heightSwich, 0, heightSwich, heightSwich)];
_thumOpen.backgroundColor = kColorCommon;
_thumOpen.layer.cornerRadius = heightSwich / 2;
_thumOpen.layer.masksToBounds = YES;
_thumOpen.hidden = YES;
}
return _thumOpen;
}
@end