7

iPhone编程中如何在UISlider上设置滑块到点击位置并获取点击位置上的滑块值。我知道我们可以将滑块拖动到那个位置,但我不想这样做。你能告诉我如何将滑块设置到点击位置吗?这可能吗?

4

6 回答 6

19

这是“留作用户练习”的部分:

- (void) tapped: (UITapGestureRecognizer*) g {
    UISlider* s = (UISlider*)g.view;
    if (s.highlighted)
        return; // tap on thumb, let slider deal with it
    CGPoint pt = [g locationInView: s];
    CGFloat percentage = pt.x / s.bounds.size.width;
    CGFloat delta = percentage * (s.maximumValue - s.minimumValue);
    CGFloat value = s.minimumValue + delta;
    [s setValue:value animated:YES];
}
于 2011-01-01T04:35:27.073 回答
12

我这样做的方法是对滑块进行子类化并签入touchesBegan. 如果用户点击拇指按钮区域(我们跟踪)然后忽略点击,但我们会在轨迹栏上的任何其他位置::

-(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);
    }

    [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;
}
于 2011-01-20T02:37:35.510 回答
7

您可以简单地向滑块添加一个 UITapGestureRecognizer,然后拉出与之关联的 UIEvent 和 Touches 以找出沿着 UISlider 发生点击的位置。然后将滑块的值设置为此计算值。

更新:

首先,设置您的滑块并向其添加手势识别器。

UISlider *slider = [[[UISlider alloc] init] autorelease];
…
<slider setup>
…
UITapGestureRecognizer *gr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)] autorelease];
[slider addGestureRecognizer:gr];

然后实现选择器

- (void)sliderTapped:(UIGestureRecognizer *)gestureRecognizer {
    <left as a user excercise*>
}

*提示:阅读文档以了解如何推断 locationInView 并确定滑块应该是什么

于 2010-05-24T06:28:10.893 回答
3

似乎只是继承 UISlider 并始终返回 true 到 beginTracking 会产生所需的效果。

iOS 10 和 Swift 3

class CustomSlider: UISlider {
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        return true
    }
}
于 2017-07-14T20:09:29.050 回答
1

我使用子类 UISlider 代码来监听 ios6> 中的 ValueChanged 事件,以实现对齐网格类型的函数。

这在升级到 iOS7 时被破坏,因为它不再触发 UIControlEventsValueChanged。对于遇到相同问题的任何人,可以通过在 if() 中添加一行来解决,如下所示:

-(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;
}

希望它可以帮助某人:)

于 2013-09-12T17:04:08.937 回答
0

我用过这段代码。获取自:http: //imagineric.ericd.net/2012/11/15/uislider-touch-to-set-value/

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
[slider addGestureRecognizer:gr];



- (void)sliderTapped:(UIGestureRecognizer *)g {
        UISlider* s = (UISlider*)g.view;
        if (s.highlighted)
            return; // tap on thumb, let slider deal with it
        CGPoint pt = [g locationInView: s];
        CGFloat percentage = pt.x / s.bounds.size.width;
        CGFloat delta = percentage * (s.maximumValue - s.minimumValue);
        CGFloat value = s.minimumValue + delta;
        [s setValue:value animated:YES];
    }

希望这可以帮到你。

于 2014-10-31T09:58:12.820 回答