您的位置:首页 > 其它

自定义(外观)Switch开关

2015-09-18 05:59 399 查看
- (id)init {
self = [super initWithFrame:CGRectMake(0, 0, 50, 30)];
if (self) {
[self setup];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}

- (id)initWithFrame:(CGRect)frame
{
// use the default values if CGRectZero frame is set
CGRect initialFrame;
if (CGRectIsEmpty(frame)) {
initialFrame = CGRectMake(0, 0, 50, 30);
}
else {
initialFrame = frame;
}
self = [super initWithFrame:initialFrame];
if (self) {
[self setup];
}
return self;
}

//创建
- (void)setup {
self.on = NO;//设置开关是开还是关
self.inactiveColor = [UIColor clearColor];//设置不活动的颜色
self.activeColor = [UIColor colorWithRed:0.89f green:0.89f blue:0.89f alpha:1.00f];
self.onColor = [UIColor colorWithRed:1.0f green:0.85f blue:0.39f alpha:1.00f];//设置打开开关的颜色
self.borderColor = [UIColor colorWithRed:0.89f green:0.89f blue:0.91f alpha:1.00f];//设置边框颜色
self.knobColor = [UIColor whiteColor];//设置开关上按钮的颜色
self.shadowColor = [UIColor grayColor];//设置阴影的颜色
background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
background.backgroundColor = [UIColor clearColor];
background.layer.cornerRadius = self.frame.size.height * 0.5;//圆角半径
background.layer.borderColor = self.borderColor.CGColor;
background.layer.borderWidth = 1.0;
background.userInteractionEnabled = NO;
[self addSubview:background];
label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height)];
label1.alpha = 1;
label1.textAlignment =NSTextAlignmentCenter;
[self addSubview:label1];

label2= [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.height, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height)];
label2.alpha = 1.0;
label2.textAlignment =NSTextAlignmentCenter;
[self addSubview:label2];

knob = [[UIView alloc] initWithFrame:CGRectMake(1, 1, self.frame.size.height - 2, self.frame.size.height - 2)];
knob.backgroundColor = self.knobColor;
knob.layer.cornerRadius = (self.frame.size.height * 0.5) - 1;
knob.layer.shadowColor = self.shadowColor.CGColor;
knob.layer.shadowRadius = 2.0;
knob.layer.shadowOpacity = 0.5;
knob.layer.shadowOffset = CGSizeMake(0, 3);
knob.layer.masksToBounds = NO;
knob.userInteractionEnabled = NO;
[self addSubview:knob];
background.layer.cornerRadius = 2;
knob.layer.cornerRadius = 2;

isAnimating = NO;
}

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {//开始触摸
[super beginTrackingWithTouch:touch withEvent:event];
startTime = [[NSDate date] timeIntervalSince1970];
CGFloat activeKnobWidth = self.bounds.size.height - 2 + 5;
isAnimating = YES;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
if (self.on) {
knob.frame = CGRectMake(self.bounds.size.width - (activeKnobWidth + 1), knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
background.backgroundColor = self.onColor;
}
else {
knob.frame = CGRectMake(knob.frame.origin.x, knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
background.backgroundColor = self.activeColor;
}
} completion:^(BOOL finished) {
isAnimating = NO;
}];
return YES;
}

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {//继续触摸
[super continueTrackingWithTouch:touch withEvent:event];

//往右滑动就是开,往左滑动就是关
CGPoint lastPoint = [touch locationInView:self];
if (lastPoint.x > self.bounds.size.width * 0.5)
[self showOn:YES];
else
[self showOff:YES];

return YES;
}

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {//结束触摸
[super endTrackingWithTouch:touch withEvent:event];
double endTime = [[NSDate date] timeIntervalSince1970];
double difference = endTime - startTime;
BOOL previousValue = self.on;
//判断用户是否轻拍开关或者保持长按很久
if (difference <= 0.2) {
CGFloat normalKnobWidth = self.bounds.size.height - 2;
knob.frame = CGRectMake(knob.frame.origin.x, knob.frame.origin.y, normalKnobWidth, knob.frame.size.height);
[self setOn:!self.on animated:YES];
}
else {
CGPoint lastPoint = [touch locationInView:self];
if (lastPoint.x > self.bounds.size.width * 0.5)
[self setOn:YES animated:YES];
else
[self setOn:NO animated:YES];
}

if (previousValue != self.on)
[self sendActionsForControlEvents:UIControlEventValueChanged];//
}

- (void)cancelTrackingWithEvent:(UIEvent *)event {//取消触摸
[super cancelTrackingWithEvent:event];
if (self.on)
[self showOn:YES];
else
[self showOff:YES];
}

- (void)setInactiveColor:(UIColor *)color {//设置不活动的颜色
inactiveColor = color;
if (!self.on && !self.isTracking)
background.backgroundColor = color;
}

- (void)setOnColor:(UIColor *)color {//设置打开开关的颜色
onColor = color;
if (self.on && !self.isTracking) {
background.backgroundColor = color;
background.layer.borderColor = color.CGColor;
}
}

- (void)setBorderColor:(UIColor *)color {//设置边框颜色
borderColor = color;
if (!self.on)
background.layer.borderColor = color.CGColor;
}

- (void)setKnobColor:(UIColor *)color {//设置开关上按钮的颜色
knobColor = color;
knob.backgroundColor = color;
}

- (void)setShadowColor:(UIColor *)color {//设置阴影的颜色
shadowColor = color;
knob.layer.shadowColor = color.CGColor;
}

- (void)setOn:(BOOL)isOn {//设置开关是开还是关
[self setOn:isOn animated:NO];
}

- (void)setOn:(BOOL)isOn animated:(BOOL)animated {//设置开关的状态,并设置过渡动画
on = isOn;

if (isOn) {
[self showOn:animated];
}
else {
[self showOff:animated];
}
}

- (BOOL)isOn {//获取开关是否为开或关的状态
return self.on;
}

- (void)showOn:(BOOL)animated {//显示开关的开位置,并带有动画
CGFloat normalKnobWidth = self.bounds.size.height - 2;
CGFloat activeKnobWidth = normalKnobWidth + 5;
if (animated) {
isAnimating = YES;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
if (self.tracking)
knob.frame = CGRectMake(self.bounds.size.width - (activeKnobWidth + 1), knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
else
knob.frame = CGRectMake(self.bounds.size.width - (normalKnobWidth + 1), knob.frame.origin.y, normalKnobWidth, knob.frame.size.height);
background.backgroundColor = self.onColor;
background.layer.borderColor = self.onColor.CGColor;
} completion:^(BOOL finished) {
isAnimating = NO;
}];
}
else {
if (self.tracking)
knob.frame = CGRectMake(self.bounds.size.width - (activeKnobWidth + 1), knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
else
knob.frame = CGRectMake(self.bounds.size.width - (normalKnobWidth + 1), knob.frame.origin.y, normalKnobWidth, knob.frame.size.height);
background.backgroundColor = self.onColor;
background.layer.borderColor = self.onColor.CGColor;
}
}

- (void)showOff:(BOOL)animated {//显示开关的关闭位置,并带有动画
CGFloat normalKnobWidth = self.bounds.size.height - 2;
CGFloat activeKnobWidth = normalKnobWidth + 5;
if (animated) {
isAnimating = YES;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
if (self.tracking) {
knob.frame = CGRectMake(1, knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
background.backgroundColor = self.activeColor;
}
else {
knob.frame = CGRectMake(1, knob.frame.origin.y, normalKnobWidth, knob.frame.size.height);
background.backgroundColor = self.inactiveColor;
}
background.layer.borderColor = self.borderColor.CGColor;
} completion:^(BOOL finished) {
isAnimating = NO;
}];
}
else {
if (self.tracking) {
knob.frame = CGRectMake(1, knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
background.backgroundColor = self.activeColor;
}
else {
knob.frame = CGRectMake(1, knob.frame.origin.y, normalKnobWidth, knob.frame.size.height);
background.backgroundColor = self.inactiveColor;
}
background.layer.borderColor = self.borderColor.CGColor;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: