iPhone:编程 UISlider 以定位在单击的位置

发布于 2024-09-03 01:44:57 字数 99 浏览 8 评论 0原文

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

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 but i dont want to do it. Can you please tel me how to set the slider to clicked position? Is this possible to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

榕城若虚 2024-09-10 01:44:57

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

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

Here is the part "left as a user exercise":

- (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];
}
差↓一点笑了 2024-09-10 01:44:57

我的方法是对滑块进行子类化并签入 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;
}

The way I did it is to subclass the slider and check in touchesBegan. If the user taps on the thumb button area (which we track) then ignore the tap, but any where else on the trackbar we do:
:

-(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;
}
桃气十足 2024-09-10 01:44:57

您只需将 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*>
}

*提示: 阅读文档< /a> 找出如何推断 locationInView 并找出滑块应该是什么

You can simply add a UITapGestureRecognizer to the slider, then pull out the UIEvent and Touches associated with it to figure out where along the UISlider the tap took place. Then set your slider's value to this calculated value.

UPDATE:

First, setup your slider and add the gesture recognizer to it.

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

Then implement the selector

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

*Hint: Read the docs to figure out how to get the locationInView extrapolated and figure out what the slider should be

从此见与不见 2024-09-10 01:44:57

似乎只是子类化 UISlider 并始终返回 true 到 beginTracking 就可以产生所需的效果。

iOS 10 和 Swift 3

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

It seems like just subclassing UISlider and returning always true to the beginTracking produce the desired effect.

iOS 10 and Swift 3

class CustomSlider: UISlider {
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        return true
    }
}
冷情妓 2024-09-10 01:44:57

我正在使用子类 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;
}

希望它可以帮助某人:)

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 :)

冬天的雪花 2024-09-10 01:44:57

我已经使用过这段代码。获取自: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];
    }

希望这可以帮助您。

I have used this code. Get from: 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];
    }

Hope this can help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文