创建一个Slider 继承于UISlider
//!< 重写 此方法 是为了 避免因添加图片 滑块在左边或者右边 时候漏出一点控件 滑块儿不靠边
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
{
rect.origin.x = rect.origin.x - 5 ;
rect.size.width = rect.size.width +10;
return CGRectInset ([super thumbRectForBounds:bounds trackRect:rect value:value], 5 , 5);
}
//!< 可以改变高度
-(CGRect)trackRectForBounds:(CGRect)bounds
{
bounds.size.height=6;
self.layer.cornerRadius = 2.5;
return bounds;
}
然后再设置滑块的图片
//设置左右轨迹的图片
UIImage *stetchLeftTrack= [UIImage imageNamed:@"font_jindu"];
UIImage *stetchRightTrack = [UIImage imageNamed:@"font_jindu_hui"];;
_slider.backgroundColor = [UIColor clearColor];
[_slider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[_slider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
//滑块图片
UIImage *thumbImage = [UIImage imageNamed:@"font_size"];
//注意这里要加UIControlStateHightlighted的状态,否则当拖动滑块时滑块将变成原生的控件
[_slider setThumbImage:thumbImage forState:UIControlStateHighlighted];
[_slider setThumbImage:thumbImage forState:UIControlStateNormal];
效果图:
14B7A3F54F48D8718C8226D333451937.jpg
网友评论