如何垂直移动带有滑块的UILabel
Posted
技术标签:
【中文标题】如何垂直移动带有滑块的UILabel【英文标题】:how to move UILabel with slider in vertical 【发布时间】:2013-04-26 14:10:19 【问题描述】:我想将我的滑块与我的拇指图像一起垂直移动。但是移动是错误的。
在水平方向上它可以完美地工作,但在垂直方向上却不行
- (void)viewDidLoad
CGAffineTransform trans = CGAffineTransformMakeRotation(-M_PI * 0.5);
self.SDSlider.transform = trans;
- (IBAction)sliderValueChanged:(UISlider *)sender
self.SDlabel.text = [NSString stringWithFormat:@"%d", (int)sender.value];
CGRect trackRect = [self.SDSlider trackRectForBounds:self.SDSlider.bounds];
CGRect thumbRect = [self.SDSlider thumbRectForBounds:self.SDSlider.bounds
trackRect:trackRect
value:self.SDSlider.value];
self.testSDlabel.center = CGPointMake(thumbRect.origin.x + self.SDSlider.frame.origin.x, self.SDSlider.frame.origin.y - 20);
self.testSDlabel.text = [NSString stringWithFormat:@"%.0f", self.SDSlider.value];
标签从左向右移动,而不是从底部向上移动
【问题讨论】:
怎么了?你看到了什么? @rdelmar 请看我附加的图片 【参考方案1】:Got 通过改变 center.y 的值来工作
- (IBAction)sliderValueChanged:(UISlider *)sender
self.SDlabel.text = [NSString stringWithFormat:@"%d", (int)sender.value];
CGRect trackRect = [self.SDSlider trackRectForBounds:self.SDSlider.bounds];
CGRect thumbRect = [self.SDSlider thumbRectForBounds:self.SDSlider.bounds
trackRect:trackRect
value:self.SDSlider.value];
self.SDlabel.text = [NSString stringWithFormat:@"%.0f", self.SDSlider.value];
NSLog(@"thumbRect.origin.x %f",thumbRect.origin.x);
NSLog(@"self.testSDlabel %f",self.SDlabel.center.x);
self.SDlabel.center = CGPointMake(self.SDlabel.center.x, 423 - thumbRect.origin.x);
【讨论】:
这基本上把我带到了我需要附加到水平滑块的地方【参考方案2】:首先将viewdidLoad中label的初始中心设置为
override func viewDidLoad()
super.viewDidLoad()
initialLabelCenter = labelSliderValue.center.x
//Set value of slider into label from 100 to 1000
@IBAction func sliderActionValueChanged(_ sender: UISlider)
let currentValue: CGFloat = CGFloat(sender.value)
labelSliderValue.text = String(describing: currentValue)
//Returns the drawing rectangle for the slider’s track
let trackRect = sliderInstance.trackRect(forBounds: sliderInstance.bounds)
//Returns the drawing rectangle for the slider’s thumb image.
let sliderThumbRect = sliderInstance.thumbRect(forBounds: sliderInstance.bounds, trackRect: trackRect, value: sliderInstance.value)
//Set label's center to move in a particular direction. In this case it will move left to right horizontally
labelSliderValue.center = CGPoint(x: initialLabelCenter + sliderThumbRect.origin.x, y: self.labelSliderValue.center.y)
注意:此代码从左到右水平移动标签。
【讨论】:
以上是关于如何垂直移动带有滑块的UILabel的主要内容,如果未能解决你的问题,请参考以下文章