UILabel在UISlider拇指上 [英] UILabel Over UISlider Thumb

查看:133
本文介绍了UILabel在UISlider拇指上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将UILabel放在UISlider的拇指上...以便当我移动拇指时.... UILabel也将移动....就像在拇指上一样...

How can i put an UILabel over the thumb of UISlider...so that when i move the thumb....UILabel will also move....as it is on the thumb...

有什么想法吗?

推荐答案

每个公共API均不提供旋钮",因此连接它的可能性很小- if 它是以下位置的子视图全部而不仅仅是直接绘制.

The "knob" isn't available per public API, so bad chances for hooking it up - if it is a subview at all and not just drawn directly.

因此,您应该将标签添加到与滑块相同的视图中(请确保稍后添加它,以使其在出现).然后,您可以侦听值更改事件并相应地放置标签.首先需要确定端点之间的线性比例关系,但这并不难.

So you should add you label to the same view as the slider (make sure you add it later so that appears over it). You can then listen for the value change events and place your label accordingly. It is linear scaling between the endpoints that you need to figure out at first, but it shouldn't be too difficult.

使用代码进行

yourLabel = [[UILabel alloc]initWithFrame:....];
// .. configure label
[[yourSlider superview] addSubview:yourLabel];
[yourSlider addTarget:self action:@selector(adjustLabelForSlider:) forControlEvents:UIControlEventValueChanged];


-(void)adjustLabelForSlider:(id)slider
{
    float value = slider.value;
    float min = slider.minimumValue;
    float max = slider.maximumValue;

    CGFloat newX = ...; // Calculate based on yourSlider.frame and value, min, and max
    CGFloat newY = ...;

    [yourLabel setCenter:CGPointMake(newX,newY)];
}

注意:未经测试的代码;-)

Note: untested code ;-)

这篇关于UILabel在UISlider拇指上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆