iPhone:Programming UISlider to position at clicked location

后端 未结 6 463
北荒
北荒 2020-12-09 13:59

How to set the slider to clicked position and get the slider value on the clicked location on UISlider in iPhone programming. i know we can drag the slider to that position

6条回答
  •  独厮守ぢ
    2020-12-09 14:28

    I was using the subclassed UISlider code to listen to ValueChanged events in ios6> in order to implement a snap-to-grid type of function.

    This was broken when upgrading to iOS7 as it no longer fired the UIControlEventsValueChanged. For anyone having the same problem it is fixed by adding a line in the if() as below:

    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchLocation = [touch locationInView:self];
    
        // if we didn't tap on the thumb button then we set the value based on tap location
        if (!CGRectContainsPoint(lastKnownThumbRect, touchLocation)) {
    
            self.value = self.minimumValue + (self.maximumValue - self.minimumValue) * (touchLocation.x / self.frame.size.width);
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }
    
        [super touchesBegan:touches withEvent:event];
    }
    
    - (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {
    
        CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value];
        lastKnownThumbRect = thumbRect;
        return thumbRect;
    }
    

    Hope it helps someone :)

提交回复
热议问题