自定义 switchBtn

//

//  CustomSwitchBtn.h

//  IntelligentWaterValve

//

//  Created by lxl on 2017/12/22.

//  Copyright © 2017年 komlin. All rights reserved.

//

 

 

 

#import <UIKit/UIKit.h>

 

typedef void(^CurrentStateBlcok) (BOOL state);

 

@interface CustomSwitchBtn : UIView

 

@property (nonatomic , assign) float font;//default 13

 

@property (nonatomic , assign) BOOL currentState;//设置按钮初始状态 1开 0关  default 0

@property (nonatomic , strong) NSString * centerImageName;//中间按钮图片,可选,默认给颜色

 

@property (nonatomic , copy) CurrentStateBlcok currentStateBlcok;//返回按钮触发后的状态 1开 0关

 

- (instancetype)initWithFrame:(CGRect)frame;

 

@end

 //--------------------------------------------------------

 

//

//  CustomSwitchBtn.m

//  IntelligentWaterValve

//

//  Created by lxl on 2017/12/22.

//  Copyright © 2017年 komlin. All rights reserved.

//

 

#define KColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

 

//颜色

#define offColor KColor(197, 62, 37)//关闭时背景色

#define onColor KColor(110, 150, 81)//打开时背景色

#define centerBtnColor KColor(218, 218, 218)//中间按钮颜色,其实是UIImageView,可以自行修改,也可以给图片

 

//间距

#define left_right_margin 10 // 中间按钮距离左右两边距离

#define top_bottom_margin 7 //中间按钮距离上下距离

 

#import "CustomSwitchBtn.h"

@interface CustomSwitchBtn ()<UIGestureRecognizerDelegate>

{

    UILabel * leftL;

    UILabel * rightL;

    UIView * bgV;//背景

    UIImageView * slidingV;//滑动view

    float maxRight;//滑块向右最大 x 位置

    float width_btn;

    float height_btn;

}

 

@end

 

@implementation CustomSwitchBtn

 

 

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {

        [self layoutViewWithFrame:frame];

    }

    return self;

    

}

 

 

 

- (void)layoutViewWithFrame:(CGRect)frame{

    

    width_btn = frame.size.width;

    height_btn = frame.size.height;

    

    bgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_btn, height_btn)];

    [self addSubview:bgV];

    bgV.backgroundColor = offColor;

    bgV.layer.cornerRadius = height_btn/2;

    bgV.layer.masksToBounds = YES;

    

    leftL = [[UILabel alloc]initWithFrame:CGRectMake(left_right_margin, top_bottom_margin, (width_btn - left_right_margin * 2)/2, height_btn - top_bottom_margin * 2)];

    [self addSubview:leftL];

    leftL.text = @"ON";

    leftL.textColor = [UIColor whiteColor];

    leftL.textAlignment = UITextAlignmentCenter;

    leftL.font = [UIFont boldSystemFontOfSize:13 * ScalW];

    leftL.hidden = YES;

    

    rightL = [[UILabel alloc]initWithFrame:CGRectMake(width_btn - left_right_margin - (width_btn - left_right_margin * 2)/2, top_bottom_margin, (width_btn - left_right_margin * 2)/2, height_btn - top_bottom_margin * 2)];

    [self addSubview:rightL];

    rightL.text = @"OFF";

    rightL.textColor = [UIColor whiteColor];

    rightL.textAlignment = UITextAlignmentCenter;

    rightL.font = [UIFont boldSystemFontOfSize:13 * ScalW];

    

    slidingV = [[UIImageView alloc]initWithFrame:CGRectMake(left_right_margin, top_bottom_margin, (width_btn - top_bottom_margin * 2)/2, height_btn - top_bottom_margin * 2)];

    slidingV.backgroundColor = centerBtnColor;

    slidingV.layer.cornerRadius = CGRectGetHeight(slidingV.frame)/2;

    slidingV.layer.masksToBounds = YES;

    slidingV.userInteractionEnabled = YES;

    [self addSubview:slidingV];

    

    maxRight = width_btn - left_right_margin - (width_btn - left_right_margin * 2)/2;

 

    

    // 添加拖拽手势

    UIPanGestureRecognizer * PanGestureTap = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];

    [self addGestureRecognizer:PanGestureTap];

    

    //  添加点击手势

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];

    [self addGestureRecognizer:tap];

}

 

 

//点击

- (void)tap:(UIGestureRecognizer *)sender{

    

    CGPoint point = [sender locationInView:self];

    

    CGFloat target = 0;

 

    if (point.x > width_btn/2) {

        target = maxRight;

    }

 

    __weak typeof(self)weakSelf = self;

    CGFloat offsetX = target - slidingV.frame.origin.x;

    [UIView animateWithDuration:0.3 animations:^{

        slidingV.frame = [weakSelf frameWithOffsetX:offsetX];

    }];

    

}

 

 

//拖拽

- (void)pan:(UIPanGestureRecognizer *)pan{

    //获取偏移量

    CGPoint offsetP = [pan translationInView:self];

    CGFloat offsetX = offsetP.x;

    

    //最新位置

    slidingV.frame = [self frameWithOffsetX:offsetX];

    

    // 复位

    [pan setTranslation:CGPointZero inView:slidingV];

    

    //最左最右

    if (pan.state == UIGestureRecognizerStateEnded) {

        CGFloat target = 0;

        if (slidingV.frame.origin.x >= (width_btn - slidingV.frame.size.width) / 2 ) {

            target = maxRight;

        }

        

        //获取x轴偏移量

        __weak typeof(self)weakSelf = self;

        CGFloat offsetX = target - slidingV.frame.origin.x;

        [UIView animateWithDuration:0.2 animations:^{

            slidingV.frame = [weakSelf frameWithOffsetX:offsetX];

        }];

    }

    

}

 

- (CGRect)frameWithOffsetX:(CGFloat)offsetX{

 

    //最新x

    CGFloat currentX = slidingV.frame.origin.x + offsetX;

    if (currentX >= maxRight) {

        currentX = maxRight ;

        leftL.hidden = NO;

        rightL.hidden = YES;

        bgV.backgroundColor = onColor;

        self.currentStateBlcok(YES);

    }

    if (currentX <= left_right_margin) {

        currentX = left_right_margin;

        leftL.hidden = YES;

        rightL.hidden = NO;

        bgV.backgroundColor = offColor;

        self.currentStateBlcok(NO);

 

    }

    return CGRectMake(currentX, top_bottom_margin, (width_btn - left_right_margin * 2)/2, height_btn - top_bottom_margin * 2);

 

 

}

 

- (void)setCurrentState:(BOOL)currentState{

    

    if (currentState) {

        leftL.hidden = NO;

        rightL.hidden = YES;

        bgV.backgroundColor = onColor;

        slidingV.frame = CGRectMake(width_btn - left_right_margin - ((width_btn - left_right_margin * 2)/2), top_bottom_margin, (width_btn - top_bottom_margin * 2)/2, height_btn - top_bottom_margin * 2);

    }else{

        leftL.hidden = YES;

        rightL.hidden = NO;

        bgV.backgroundColor = offColor;

        slidingV.frame = CGRectMake(left_right_margin, top_bottom_margin, (width_btn - top_bottom_margin * 2)/2, height_btn - top_bottom_margin * 2);

    }

    

}

 

- (void)setCenterImageName:(NSString *)centerImageName{

    slidingV.image = [UIImage imageNamed:centerImageName];

}

 

- (void)setFont:(float)font{

    leftL.font = [UIFont boldSystemFontOfSize:font * ScalW];

    rightL.font = [UIFont boldSystemFontOfSize:font * ScalW];

}

 

 

- (id)hitTest:(CGPoint)point withEvent:(UIEvent *)event

{

    UIView *hitView = [super hitTest:point withEvent:event];

    

    if (hitView == self) {

        return nil;

    } else {

        return hitView;

    }

}

 

@end

 

posted @ 2018-01-17 17:15  奋斗路上的奋青  阅读(499)  评论(0)    收藏  举报