UISlider custom images and thumb height?

丶灬走出姿态 提交于 2019-12-10 11:33:17

问题


I am creating (or attempting to) a custom UISlider look, still horizontal but much taller. I have two problems.

1.This is the code I'm using to get the images onto the slider.

UIImage *minImage = [UIImage imageNamed:@"sliderMin.png"];
UIImage *maxImage = [UIImage imageNamed:@"sliderMax.png"];
UIImage *thumbImage = [UIImage imageNamed:@"sliderThumb.png"];

minImage = [minImage stretchableImageWithLeftCapWidth:33.0 topCapHeight:0.0];
maxImage = [maxImage stretchableImageWithLeftCapWidth:33.0 topCapHeight:0.0];
// thumbImage = [minImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];

[_contrastSlider setMinimumTrackImage:minImage forState:UIControlStateNormal];
[_contrastSlider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
[_contrastSlider setThumbImage:thumbImage forState:UIControlStateNormal];

minImage = nil;
maxImage = nil;
thumbImage = nil;

[_contrastSlider setMinimumValue:0.0];
[_contrastSlider setMaximumValue:100.00];
[_contrastSlider setValue:25.0];

I have three images, min, max and thumb. These are the images and how they look on the device, which isn't correct.

I can't post images yet, so here is a link to MYSlider.jpeg which shows the components.

The slider should be cream to the right of the thumb, pink to the left. But this isn't happening.

That's problem 1. Problem 2 is the thumb size default is too small. I'm using this :UISlider Height Hack to try to increase the height of the thumb, but sadly it's making it wider not taller. So I am asking for help to make the area taller, not wider, and wondering why my pink image is being ignored?

Thank you for any help.

If it helps, here is my UISlider subclass .m:

-(BOOL) pointInside:(CGPoint)point withEvent:(UIEvent*)event {
CGRect bounds = self.bounds;
bounds = CGRectInset(bounds, -10, -8);
return CGRectContainsPoint(bounds, point);

}

-(BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
  CGRect bounds = self.bounds;
  float thumbPercent = (self.value - self.minimumValue) / (self.maximumValue - self.minimumValue);
  float thumbPos = THUMB_SIZE + (thumbPercent * (bounds.size.width - (2 * THUMB_SIZE)));
  CGPoint touchPoint = [touch locationInView:self];
  return (touchPoint.x >= (thumbPos - EFFECTIVE_THUMB_SIZE) && touchPoint.x <= (thumbPos + EFFECTIVE_THUMB_SIZE));
}

Thank you.


回答1:


It turns out setting my slider values in the views did load method was causing problems. I set them in Interface Builder and that was fixed. Once set in IB I can tweak them in code if needs be with no problems.

As for my other problem, I'm wondering if this is because I'm not testing on an actual device, so I'm going to pause that question until I get it on the device in a few weeks.



来源:https://stackoverflow.com/questions/10311996/uislider-custom-images-and-thumb-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!