Search code examples
iphoneiosxcodeuilabeluislider

how to move UILabel with slider in vertical


i will like to move my slider together with my thumb image in vertical. However the moving was wrong.

in horizontal it works perfectly but not vertically

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

the label shifting from left to right instead of botton to up

enter image description here

enter image description here


Solution

  • Got is working by changing the center.y value

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