自定义switch

//.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


//.m

#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值