Search code examples
iosuislidercgaffinetransformscale

Scale UIView using UISlider


I'm trying to scale an UIView using a UISlider but the result is not good with this approach:

- (void)setScale:(float)scale {
   CGAffineTransform transform = CGAffineTransformScale(myView.transform, scale, scale);
            myView.transform = transform;

}

thanks


Solution

  • You want to change the scale based on the identity transform (which represents the object without any changes). This code works:

    - (void)sliderDidChangeValue:(id)sender
    {
        // the slider
        UISlider *slider = sender;
    
        // get your view or use the ivar if you have it in one
        UIView *view = [self.view viewWithTag:12];
    
        CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity, slider.value, slider.value);
        view.transform = transform;
    }
    

    When you create the slider you may want to start the value at 1 if your view is starting at full size.