UISlider
slider.continuous = YES;//默认YES 如果设置为NO,则每次滑块停止移动后才触发事件
[slider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
//手动设置滑块的值,并是否动画移动过去:
- (void)setValue:(float)value animated:(BOOL)animated;
//给滑动按钮设置图片
- (void)setThumbImage:(nullable UIImage *)image forState:(UIControlState)state;
//给滑道左侧设置图片
- (void)setMinimumTrackImage:(nullable UIImage *)image forState:(UIControlState)state;
//给滑道右侧设置图片
- (void)setMaximumTrackImage:(nullable UIImage *)image forState:(UIControlState)state;
@property(nullable,nonatomic,readonly) UIImage *currentThumbImage;
@property(nullable,nonatomic,readonly) UIImage *currentMinimumTrackImage;
@property(nullable,nonatomic,readonly) UIImage *currentMaximumTrackImage;
//定制UISlider
- (CGRect)minimumValueImageRectForBounds:(CGRect)bounds; //返回左边图片大小
- (CGRect)maximumValueImageRectForBounds:(CGRect)bounds; //返回右边图片大小
- (CGRect)trackRectForBounds:(CGRect)bounds; //返回滑道大小
//我们通常通过重写(而不是主动调用)这个方法来控制滑块大小来消除我们改变滑块图片导致的两端缝隙
/**
@bounds The bounding rectangle of the slider.
@rect The drawing rectangle for the slider’s track, as returned by the `trackRectForBounds:` method.
@value The current value of the slider.
@return The computed drawing rectangle for the thumb image.
*/
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value; //返回滑块大小
举个栗子:
//自定义滑块图片后这样可以消除两端间隙
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
{
rect.origin.x = rect.origin.x - 10 ;
rect.size.width = rect.size.width +20;
return CGRectInset ([super thumbRectForBounds:bounds trackRect:rect value:value], 10 , 10);
}
//还可以
-(CGRect)trackRectForBounds:(CGRect)bounds {
bounds.origin.x=15;
bounds.origin.y=bounds.size.height/3;
bounds.size.height=bounds.size.height/5;
bounds.size.width=bounds.size.width-30;
returnbounds;
}