简体   繁体   中英

UISlider custom images and thumb height?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM