GiGaMaskTime/GIGA/Modules/Mask/Exercises/View/GIGaQuestionSlider.m

175 lines
4.7 KiB
Objective-C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// GIGaQuestionSlider.m
// GIGA
//
// Created by lianxiang on 2018/9/11.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GIGaQuestionSlider.h"
#import "UIColor+HexColor.h"
#define thumbBound_x 10
#define thumbBound_y 20
@interface GIGaQuestionSlider()
{
CGRect _lastBounds;
}
@end
@implementation GIGaQuestionSlider
- (instancetype)initWithFrame:(CGRect)frame type:(GIGaQuestionSliderType)type
{
self = [super initWithFrame:frame];
if (self) {
self.minimumValue = 0.0;
self.maximumValue = 1.0;
[self creatSubView:type];
}
return self;
}
-(void)creatSubView:(GIGaQuestionSliderType)type{
[self thumbimageView];
switch (type) {
case GIGaQuestionSliderTypeGray:
{
[self createUI];
}
break;
case GIGaQuestionSliderTypeGradient:
{
[self creatGradient];
}
break;
default:
break;
}
}
-(void)creatGradient{
self.minimumTrackTintColor=[UIColor clearColor];
self.maximumTrackTintColor=[UIColor clearColor];
NSArray *colorArr = @[(id)[[UIColor colorWithHex:0xFFFFFF] CGColor],
(id)[[UIColor colorWithHex:0xFFF3E4] CGColor],
(id)[[UIColor colorWithHex:0xFBC37B] CGColor]];
NSArray *colorLocationArray = @[@0.0, @0.5, @1.0];
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.bounds;
gradientLayer.masksToBounds = YES;
gradientLayer.cornerRadius = self.frame.size.height /2 ;
gradientLayer.borderWidth = 1;
gradientLayer.borderColor =GIGARGB(215, 215, 215, 1).CGColor;
[gradientLayer setLocations:colorLocationArray];
[gradientLayer setColors:colorArr];
[gradientLayer setStartPoint:CGPointMake(0, 0)];
[gradientLayer setEndPoint:CGPointMake(1, 0)];
[self.layer addSublayer:gradientLayer];
}
-(void)createUI{
self.minimumValue = 0.0;
self.maximumValue = 1.0;
// self.minimumTrackTintColor = GIGARGB(216,216, 216, 1);
// self.maximumTrackTintColor = GIGARGB(216,216, 216, 1);
self.minimumTrackTintColor = GIGAUIColorFromRGBA(0xEA4D4D);
self.maximumTrackTintColor = GIGAUIColorFromRGBA(0xEA4D4D);
}
-(CGRect)trackRectForBounds:(CGRect)bounds
{
bounds.origin.x = bounds.origin.x;
bounds.origin.y = bounds.origin.y;
bounds.size.height = bounds.size.height;
bounds.size.width = bounds.size.width;
return bounds;
}
-(void)thumbimageView{
[self setThumbImage:[self OriginImage:[UIImage imageNamed:@"btn_slider_thumb"] scaleToSize:CGSizeMake(40, 40)] forState:UIControlStateNormal];
[self setThumbImage:[self OriginImage:[UIImage imageNamed:@"btn_slider_thumb"] scaleToSize:CGSizeMake(40, 40)] forState:UIControlStateHighlighted];
}
-(UIImage*) OriginImage:(UIImage*)image scaleToSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);//size为CGSize类型即你所需要的图片尺寸
[image drawInRect:CGRectMake(0,0, size.width, size.height)];
UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
// 扩大thumb 触控区域
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
{
rect.origin.x = rect.origin.x;
rect.size.width = rect.size.width ;
CGRect result = [super thumbRectForBounds:bounds trackRect:rect value:value];
_lastBounds = result;
return result;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView *result = [super hitTest:point withEvent:event];
if (point.x < 0 || point.x > self.bounds.size.width){
return result;
}
if ((point.y >= -thumbBound_y) && (point.y < _lastBounds.size.height + thumbBound_y)) {
float value = 0.0;
value = point.x - self.bounds.origin.x;
value = value/self.bounds.size.width;
value = value < 0? 0 : value;
value = value > 1? 1: value;
value = value * (self.maximumValue - self.minimumValue) + self.minimumValue;
[self setValue:value animated:YES];
}
return result;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
BOOL result = [super pointInside:point withEvent:event];
if (!result && point.y > -10) {
if ((point.x >= _lastBounds.origin.x - thumbBound_x) && (point.x <= (_lastBounds.origin.x + _lastBounds.size.width + thumbBound_x)) && (point.y < (_lastBounds.size.height + thumbBound_y))) {
result = YES;
}
}
return result;
}
@end