ios 自定义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;
@property (nonatomic , assign) BOOL currentState;//1开 0关
@property (nonatomic , assign) CurrentStateBlcok currentStateBlcok;
@end
###############
//
// CustomSwitchBtn.m
// IntelligentWaterValve
//
// Created by lxl on 2017/12/22.
// Copyright © 2017年 komlin. All rights reserved.
//
//颜色
#define offColor KColor(197, 62, 37)
#define onColor KColor(110, 150, 81)
//间距
#define left_right_margin 10
#define top_bottom_margin 7
#import "CustomSwitchBtn.h"
@interface CustomSwitchBtn ()<UIGestureRecognizerDelegate>
{
UILabel * leftL;
UILabel * rightL;
UIView * bgV;//背景
UIView * slidingV;//滑动view
float maxRight;
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 = [[UIView alloc]initWithFrame:CGRectMake(left_right_margin, top_bottom_margin, (width_btn - top_bottom_margin * 2)/2, height_btn - top_bottom_margin * 2)];
slidingV.backgroundColor = KColor(218, 218, 218);
slidingV.layer.cornerRadius = CGRectGetHeight(slidingV.frame)/2;
slidingV.layer.masksToBounds = 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)setFont:(float)font{
leftL.font = [UIFont boldSystemFontOfSize:font * ScalW];
rightL.font = [UIFont boldSystemFontOfSize:font * ScalW];
}
@end
#################
用法
1.
#import "CustomSwitchBtn.h"
2.
CustomSwitchBtn * btn = [[CustomSwitchBtn alloc]initWithFrame:CGRectMake(100, 550, 200, 36)];
[self.view addSubview:btn];
btn.currentState = YES;//default NO
btn.currentStateBlcok = ^(BOOL state) {
NSLog(@"%@",state ? @"开":@"关");
};