@interface CusSlider : UISlider
@end
@implementation CusSlider
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
CGFloat dif = 4;
CGFloat difX = dif;
rect.origin.x = rect.origin.x - difX;
rect.size.width = rect.size.width + difX * 2;
CGFloat difY = -0;
rect.origin.y = rect.origin.y - difY;
rect.size.height = rect.size.height + difY * 2;
CGRect newRect = [super thumbRectForBounds:bounds trackRect:rect value:value];
/* Inset `rect' by `(dx, dy)' -- i.e., offset its origin by `(dx, dy)', and
decrease its size by `(2*dx, 2*dy)'. */
return CGRectInset(newRect, difX, difY);
}
@end
- (void)createUI{
// 1 自定义Slider, 重写 thumbRectForBounds 调整rect
CusSlider * slider = [CusSlider new];
// 2 新加图层
UIView * viewRed = [UIView new];
viewRed.layer.masksToBounds = YES;
viewRed.backgroundColor = [UIColor redColor];
// 3 延迟调整图层
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (slider.subviews.count > 0) {
UIView * viewParent = [self getSliderSubParentView:slider];
[viewParent addSubview:viewRed];
for (UIView * v in viewParent.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
if (v.subviews.count > 0) {
// 系统默认图 可以通过缩放更改大小
CGFloat scale = 0.85;
UIImageView * thumbImgV = v.subviews.firstObject;
[thumbImgV setTransform:CGAffineTransformMakeScale(scale, scale)];
}else{
// 自定义图片
}
[viewParent insertSubview:viewRed belowSubview:v];
}
}
}
});
}
- (UIView *)getSliderSubParentView:(UISlider *)slider{
if (slider.subviews.count == 1) {
return slider.subviews[0];
}else{
return slider;
}
}
ios13.png
ios15.png
网友评论