我如何将UILabel放在UISlider的拇指上...这样当我移动拇指时...... UILabel也会移动....因为它在拇指上......
任何想法??
答案 0 :(得分:29)
试试这个
yourLabel = [[UILabel alloc]initWithFrame:....];
//Call this method on Slider value change event
-(void)sliderValueChanged{
CGRect trackRect = [self.slider trackRectForBounds:self.slider.bounds];
CGRect thumbRect = [self.slider thumbRectForBounds:self.slider.bounds
trackRect:trackRect
value:self.slider.value];
yourLabel.center = CGPointMake(thumbRect.origin.x + self.slider.frame.origin.x, self.slider.frame.origin.y - 20);
}
通过使用此代码段,我可以获得最准确的价值。
答案 1 :(得分:5)
每个公共API都无法使用“旋钮”,因此很难将其挂起 - 如果它完全是子视图而不是直接绘制。
因此,您应该将标签添加到与滑块相同的视图中(请确保稍后添加它以便显示它)。然后,您可以监听价值变化事件并相应地放置您的标签。它是您最初需要弄清楚的端点之间的线性缩放,但它不应该太难。
使用代码进行编辑:
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)];
}
注意:未经测试的代码; - )
答案 2 :(得分:1)
只需在滑块的拇指上添加一个imageview,在imageview上添加一个标签
- (IBAction)valueChangedSlider:(id)sender {
handleView = [_slider.subviews lastObject];
label = [[UILabel alloc] initWithFrame:handleView.bounds];
label = (UILabel*)[handleView viewWithTag:1000];
if (label==nil) {
label = [[UILabel alloc] initWithFrame:handleView.bounds];
label.tag = 1000;
[label setFont:[UIFont systemFontOfSize:12]];
label.textColor = [UIColor redColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
[handleView addSubview:label];
}
label.text = [NSString stringWithFormat:@"%0.2f", self.slider.value];
}
答案 3 :(得分:1)
extension UIImage {
class func imageWithLabel(_ label: UILabel) -> UIImage {
UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0)
defer { UIGraphicsEndImageContext() }
label.layer.render(in: UIGraphicsGetCurrentContext()!)
return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
}
}
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UIScrollViewDelegate
{
func maskRoundedImage(image: UIImage, radius: CGFloat) -> UIImage {
let imageView: UIImageView = UIImageView(image: image)
let layer = imageView.layer
layer.masksToBounds = true
layer.cornerRadius = radius
UIGraphicsBeginImageContext(imageView.bounds.size)
layer.render(in: UIGraphicsGetCurrentContext()!)
let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return roundedImage!
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 28, height: 28))
label.backgroundColor = .black
label.textAlignment = .center
label.font = label.font.withSize(12)
label.text = String(Int(round( backlightSlider.value * 100 )))
label.textColor = .white
var image = UIImage.imageWithLabel(label)
image = maskRoundedImage(image: image, radius: 14.0)
backlightSlider.setThumbImage(image, for: .normal)
}
答案 4 :(得分:0)
与 swift3 相同的答案:
var array20171204 = [{"dt":1512388800,"main":{"temp":301.9,"temp_min":299.858,"temp_max":301.9,"pressure":1017.53,"sea_level":1020.58,"grnd_level":1017.53,"humidity":98,"temp_kf":2.04},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":80},"wind":{"speed":2.36,"deg":161.003},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12-04 12:00:00"},{"dt":1512399600,"main":{"temp":300.75,"temp_min":299.389,"temp_max":300.75,"pressure":1019.04,"sea_level":1022.09,"grnd_level":1019.04,"humidity":100,"temp_kf":1.36},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":68},"wind":{"speed":2.04,"deg":133.002},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12-04 15:00:00"},{"dt":1512410400,"main":{"temp":299.41,"temp_min":298.726,"temp_max":299.41,"pressure":1017.89,"sea_level":1020.93,"grnd_level":1017.89,"humidity":100,"temp_kf":0.68},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":24},"wind":{"speed":2.08,"deg":108.001},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12-04 18:00:00"},{"dt":1512421200,"main":{"temp":298.19,"temp_min":298.19,"temp_max":298.19,"pressure":1017.39,"sea_level":1020.45,"grnd_level":1017.39,"humidity":100,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":24},"wind":{"speed":2.26,"deg":94.0002},"rain":{},"sys":{"pod":"n"},"dt_txt":"2017-12-04 21:00:00"}];
var array20171205 = [{"dt":1512432000,"main":{"temp":298.444,"temp_min":298.444,"temp_max":298.444,"pressure":1019.32,"sea_level":1022.39,"grnd_level":1019.32,"humidity":100,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":20},"wind":{"speed":2.5,"deg":89.0016},"rain":{},"sys":{"pod":"d"},"dt_txt":"2017-12-05 00:00:00"}];
答案 5 :(得分:0)
如果有人在 Swift 中寻找答案,请在此处查看我的答案:- Put Label over UISlider Thumb
它会像魅力一样工作:)
答案 6 :(得分:-1)
这可能非常有帮助...... How to get the center of the thumb image of UISlider