如何使用滑块垂直移动UILabel

时间:2013-04-26 14:10:19

标签: iphone ios xcode uilabel uislider

我想将我的滑块与我的拇指图像一起垂直移动。然而,移动是错误的。

在横向上它完美但不垂直

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

标签从左向右移动而不是向上移动

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

通过更改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);

}

答案 1 :(得分:0)

首先在viewdidLoad中将标签的初始中心设置为

pandoc

注意:此代码将标签从左向右水平移动。