如何增加UISlider最大轨道的大小?

时间:2014-08-10 15:51:45

标签: ios objective-c uikit uislider

我想添加一个大小不同于最小值的UISlider。
这是我需要的一个例子:

enter image description here

我尝试过一些方法,比如:

- (CGRect)minimumValueImageRectForBounds:(CGRect)bounds;
- (CGRect)maximumValueImageRectForBounds:(CGRect)bounds;
- (CGRect)trackRectForBounds:(CGRect)bounds;
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value;

但我无法让它发挥作用...... 关于你如何实现这一点的任何建议或可能的链接?

1 个答案:

答案 0 :(得分:0)

您需要子类化UISlider。如果您在运行时创建UISlider,则应使用initWithFrame,否则initWithCoder

@implementation slider

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        // At certain point images swap, that is why you need images fit to your UISlider size.
        UIImage *sliderLeftImg = [[UIImage imageNamed: @"slider2"] stretchableImageWithLeftCapWidth: 10 topCapHeight: 0];
        UIImage *sliderRightImg = [[UIImage imageNamed: @"slider"] stretchableImageWithLeftCapWidth: 10 topCapHeight: 0];
        [self setMinimumTrackImage: sliderLeftImg forState: UIControlStateNormal];
        [self setMaximumTrackImage: sliderRightImg forState: UIControlStateNormal];

    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

我绘制的样本图像循环邋,,你需要更好的图像。

UISlider Class Reference

enter image description here enter image description here

最终结果:

enter image description here