效果圖如下:
實現(xiàn)方案
1、采用系統(tǒng)UISlider實現(xiàn)
2奔浅、自己使用貝塞爾曲線繪制
方案一(不可行馆纳,有bug)
遇到的問題:當將UISlider的寬度加寬的一定寬度后,拖動效果不是漸變汹桦,會突然發(fā)送改變鲁驶,此處簡單記錄下UISlider的基礎用法
1、基礎設置
// 初始化
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 200, 100, 20)];
// 添加到俯視圖
[self.view addSubview:slider];
// 設置最小值
slider.minimumValue = 9;
// 設置最大值
slider.maximumValue = 11;
// 設置初始值
slider.value = (slider.minimumValue + slider.maximumValue) / 2;
// 設置可連續(xù)變化
slider.continuous = YES;
//滑輪左邊顏色舞骆,如果設置了左邊的圖片就不會顯示
slider.minimumTrackTintColor = [UIColor greenColor];
//滑輪右邊顏色钥弯,如果設置了右邊的圖片就不會顯示
slider.maximumTrackTintColor = [UIColor redColor];
/// 也可設置為圖片
///[slider setMinimumTrackImage:[UIImageimageNamed:@"max.png"] forState:UIControlStateNormal];
//[slider setMaximumTrackImage:[UIImageimageNamed:@"min.png"] forState:UIControlStateNormal];
//設置了滑輪的顏色,如果設置了滑輪的樣式圖片就不會顯示 slider.thumbTintColor = [UIColor redColor];
// 針對值變化添加響應方法
[slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
2督禽、旋轉(zhuǎn)90度脆霎,豎直放置
slider.transform = CGAffineTransformMakeRotation(1.57079633);
3、改變slider的寬度(方法3)狈惫,需要重寫一個繼承與UISlider的類了,然后重寫里面的實現(xiàn)方法來控制:
/// 設置最小值
- (CGRect)minimumValueImageRectForBounds:(CGRect)bounds;
/// 設置最大值
- (CGRect)maximumValueImageRectForBounds:(CGRect)bounds;
/// 控制slider的寬高
- (CGRect)trackRectForBounds:(CGRect)bounds;
/// 改變滑塊的觸摸范圍
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value;
方案二(自己繪制)
提供實現(xiàn)代碼如下
.h文件
#import <UIKit/UIKit.h>
typedef void (^sliderBlock)(float);
NS_ASSUME_NONNULL_BEGIN
@interface HYLightSlider : UIView
@property (nonatomic, copy) sliderBlock clickBlock;
-(instancetype)initWithFrame:(CGRect)frame defaultValue:(float)defaultValue;
@end
NS_ASSUME_NONNULL_END
.m文件
#import "HYLightSlider.h"
@interface HYLightSlider()
@property (nonatomic, assign) float defaultValue;
@property (nonatomic, assign) float offSet;
@property (nonatomic, strong) UIBezierPath *path;
@property (nonatomic, strong) CAShapeLayer *tubeShape;
@end
@implementation HYLightSlider
-(instancetype)initWithFrame:(CGRect)frame defaultValue:(float)defaultValue{
self = [super initWithFrame:frame];
if(self){
self.backgroundColor = [UIColor clearColor];
self.defaultValue = defaultValue;
UIPanGestureRecognizer *imageViewPanGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
[self addGestureRecognizer:imageViewPanGesture];
[self initNewPath];
[self drawWholeShape];
}
return self;
}
- (void)initNewPath{
self.path = [UIBezierPath bezierPath];
self.tubeShape = [[CAShapeLayer alloc]init];
self.tubeShape.strokeColor = [UIColor colorWithHexValue:HY_COR3].CGColor;
self.tubeShape.fillColor = [UIColor colorWithHexValue:HY_COR3].CGColor;
}
- (void)drawWholeShape{
// 管道
for (CALayer *object in self.layer.sublayers) {
[object removeFromSuperlayer];
}
[self.path removeAllPoints];
self.path.lineWidth = self.frame.size.width;
[self.path moveToPoint:CGPointMake(self.frame.size.width/2.0, self.frame.size.height)];
[self.path addLineToPoint:CGPointMake(self.frame.size.width/2.0, self.frame.size.height*(1.0-self.defaultValue))];//添加一條子路徑
[self.path stroke];
self.tubeShape = [[CAShapeLayer alloc]init];
self.tubeShape.path = self.path.CGPath;
self.tubeShape.lineWidth = self.frame.size.width;
self.tubeShape.strokeColor = [UIColor colorWithHexValue:HY_COR3].CGColor;
self.tubeShape.fillColor = [UIColor colorWithHexValue:HY_COR3].CGColor;
[self.layer addSublayer:self.tubeShape];
}
#pragma mark --- targetView的拖拽手勢
- (void)panGesture:(UIPanGestureRecognizer *)gesture{
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
{
NSLog(@"滑動開始");
}
break;
case UIGestureRecognizerStateChanged:
{
// 獲得添加手勢的對象
// 獲得滑動的距離 包含 x y 移動的數(shù)值
CGPoint point =[gesture translationInView:gesture.view];
//向下滑動
if(point.y >= self.frame.size.height*self.defaultValue){
self.offSet = self.frame.size.height*self.defaultValue;
//向上滑動
}else if(-point.y >= self.frame.size.height - self.frame.size.height*self.defaultValue){
self.offSet = -self.frame.size.height + self.frame.size.height*self.defaultValue;
}else{
self.offSet = point.y;
}
self.offSet = self.offSet*0.1;
self.defaultValue = (-self.offSet+self.frame.size.height*self.defaultValue)/self.frame.size.height;
if(self.defaultValue < 0.0 || self.defaultValue > 1.0){
return;
}
[self.tubeShape setNeedsDisplay];
[self drawWholeShape];
NSLog(@"滑動中");
NSLog(@"%f",self.offSet);
}
break;
case UIGestureRecognizerStateEnded:
{
NSLog(@"%f",self.offSet);
NSLog(@"滑動結(jié)束");
self.clickBlock ? self.clickBlock(self.defaultValue) : nil;
}
break;
default:
break;
}
}
@end