0

I'd like to have a UISlider respond to tapping on the minimumValueImage and/or maximumValueImage, setting the value to either minimum or maximum. I can't seem to find a 'normal' approach for this scenario, so I came up with this solution. I'm subclassing a UISlider and keep register of where the user started a touch. By comparing the location I can figure out if it was on one of the images. Works okay, but is there a less custom way to achieve the same goal?

@interface FGSlider ()

@property (nonatomic) CGRect minimumValueImageRect;
@property (nonatomic) CGRect maximumValueImageRect;

@property (nonatomic) BOOL touchesBeganInMinimumValueImageRect;
@property (nonatomic) BOOL touchesBeganInMaximumValueImageRect;

@end

@implementation FGSlider

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInView:self];
    if(CGRectContainsPoint(self.minimumValueImageRect, location)) {
        self.touchesBeganInMinimumValueImageRect = YES;
    }
    else if(CGRectContainsPoint(self.maximumValueImageRect, location)) {
        self.touchesBeganInMaximumValueImageRect = YES;
    }
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];

    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInView:self];
    if(self.touchesBeganInMinimumValueImageRect && CGRectContainsPoint(self.minimumValueImageRect, location)) {
        [self setValue:self.minimumValue animated:YES];
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
    else if(self.touchesBeganInMaximumValueImageRect && CGRectContainsPoint(self.maximumValueImageRect, location)) {
        [self setValue:self.maximumValue animated:YES];
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }

    // reset state
    self.touchesBeganInMinimumValueImageRect = NO;
    self.touchesBeganInMinimumValueImageRect = NO;
}


-(CGRect)minimumValueImageRectForBounds:(CGRect)bounds {
    self.minimumValueImageRect = [super minimumValueImageRectForBounds:bounds];
    return self.minimumValueImageRect;
}

-(CGRect)maximumValueImageRectForBounds:(CGRect)bounds {
    self.maximumValueImageRect = [super maximumValueImageRectForBounds:bounds];
    return self.maximumValueImageRect;
}
@end
fguchelaar
  • 4,779
  • 23
  • 36

1 Answers1

0

I made a simple category that seems to work and doesn't rely on calculating bounds and is MUCH simpler than your implementation. Try it and let me know what you think.

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    for (UIView *view in self.subviews) {
        CGPoint locationPoint = [touch locationInView:view];
        if ([view pointInside:locationPoint withEvent:event]) {
            UIImageView *imageView = (UIImageView*)view;
            if (imageView.image == self.maximumValueImage) {
                [self setValue:self.maximumValue animated:YES];
                [self sendActionsForControlEvents:UIControlEventValueChanged];
            } else if (imageView.image == self.minimumValueImage) {
                [self setValue:self.minimumValue animated:YES];
                [self sendActionsForControlEvents:UIControlEventValueChanged];
            }
        }
    }
    [super touchesBegan:touches withEvent:event];
}
Kudit
  • 4,212
  • 2
  • 26
  • 32