ios - UISlider子类可用于自定义拇指位置和拇指图像?-LMLPHP

是否可以通过子类化UISlider来实现上述滑块?
我想将滑块的拇指移到边缘,但还有一些空间。
我已经通过重写以下方法获得了自定义位置,但现在拇指无法响应触摸事件。

-(void)awakeFromNib {
    [super awakeFromNib];
    [self setThumbImage:[UIImage imageNamed:IMG_SLIDER_THUMB] forState:UIControlStateNormal];
}

- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {
    UIImage* image = self.currentThumbImage;

    CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value];

return CGRectMake(thumbRect.origin.x, rect.origin.y+2, image.size.width, image.size.height);
}

最佳答案

我在上面通过子类化UISlider来实现,并且对于触摸事件问题,我向情节提要中的滑块添加了高度限制,以增加运行时的边界。以下是将滑块移至边缘和自定义拇指位置的代码:

//Get thumb rect for larger track rect than actual to move slider to edges
-(CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {
    UIImage *image = self.currentThumbImage;

    rect.origin.x -= SLIDER_OFFSET;
    rect.size.width += (SLIDER_OFFSET*2);
    CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value];
    return CGRectMake(thumbRect.origin.x, rect.origin.y+2, image.size.width, image.size.height);
}

//Make track rect smaller than bounds
-(CGRect)trackRectForBounds:(CGRect)bounds  {
    bounds.origin.x += SLIDER_OFFSET;
    bounds.size.width -= (SLIDER_OFFSET*2);
    CGRect trackRect = [super trackRectForBounds:bounds];

    return CGRectMake(trackRect.origin.x, trackRect.origin.y, trackRect.size.width, trackRect.size.height);
}

关于ios - UISlider子类可用于自定义拇指位置和拇指图像?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44044268/

10-13 06:35