原理:使用图更换滑块。进而修改图片的大小来修改滑块的大小,然后给图添加阴影效果
.m文件
#import "HWSlider.h"
#define kSliderHeight 10//slider高
@interface HWSlider ()
/// 是否设置过layer
@property (nonatomic , readwrite , assign) BOOL didSetLayer;
@end
@implementation HWSlider
- (instancetype)initWithFrame:(CGRect)frame{
if(self= [superinitWithFrame:frame]) {
[self setThumbImage:[UIImage imageNamed:@"圆"] forState:UIControlStateNormal];//通过直接修改图片的大小
}
return self;
}
/// 设置track(滑条)尺寸
- (CGRect)trackRectForBounds:(CGRect)bounds{
returnCGRectMake(0, (bounds.size.height-kSliderHeight) /2.0, bounds.size.width,kSliderHeight);//滑杆高改为10
}
#pragma mark- 布局
- (void)layoutSubviews{
[super layoutSubviews];
if (self.didSetLayer) {
return;
}
BOOLdidSetLayer =NO;
for(UIView*vinself.subviews) {
if ([v isKindOfClass:[UIImageView class]] && v.frame.size.height != kSliderHeight) {//给滑块图片添加阴影
v.layer.shadowColor = [UIColor blackColor].CGColor;
v.layer.shadowOffset = CGSizeMake(1, 1);
v.layer.shadowOpacity=0.5;
didSetLayer =YES;
}
}
self.didSetLayer= didSetLayer;
}
@end
网友评论