iPhone:编程UISlider以定位点击位置

时间:2010-05-24 06:21:31

标签: iphone uislider

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

6 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:11)

我这样做的方法是将滑块子类化并检入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;
}

答案 2 :(得分:7)

您可以简单地将UITapGestureRecognizer添加到滑块,然后拉出与其关联的UIEvent和Touches,以确定UISlider沿着UISlider发生的位置。然后将滑块的值设置为此计算值。

UPDATE:

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

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*>
}

*提示:Read the docs弄清楚如何推断locationInView并找出滑块应该是什么

答案 3 :(得分:2)

它似乎只是对UISlider进行子类化并且返回始终为true的beginTracking会产生所需的效果。

iOS 10和Swift 3

class CustomSlider: UISlider {
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        return true
    }
}

答案 4 :(得分:1)

我正在使用子类UISlider代码来监听ios6中的ValueChanged事件&gt;为了实现对齐网格类型的功能。

升级到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;
}

希望它可以帮助某人:)

答案 5 :(得分: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];
    }

希望这可以帮到你。