Search code examples
iosobjective-crotationuislidercgaffinetransform

CGAffineTransform rotation and resize on slider value


I am using Slider to Resize and Rotate-

For the Rotate -

 CGAffineTransform transform = editingView.transform;
  transform =  CGAffineTransformMakeRotation(sliderVal * 2*M_PI / 30);
  editingView.transform = transform;

For the Resize-

CGAffineTransform t = CGAffineTransformMakeScale(sliderVal/30, sliderVal/30);
  CGPoint center = editingView.center; 
  [UIView animateWithDuration:0.5
                   animations:^{
                      editingView.transform = t;
                     editingView.center = center;
                   }
                   completion:^(BOOL finished) {

                   }];

Using the above code,Both working fine separately.

But I have to resize the rotated view,Or rotate the resized view.

I saw many suggestions coming separate behavior because i am using CGAffineTransformMakeRotation,CGAffineTransformMakeScale,If i use the CGAffineTransformScale,CGAffineTransformRotation then my problem will be solve.

The problem is when I am using CGAffineTransform then scaling is not proper,View disappears from the screen.


Solution

  • You're setting transformation matrix of the view with editingView.transform line.

    You should change your code for rotate:

    CGAffineTransform transform = editingView.transform;
    transform =  CGAffineTransformMakeRotation(sliderVal * 2*M_PI / 30);
    editingView.transform = CGAffineTransformConcat(editingView.transform, transform);
    

    and for resize:

    CGAffineTransform t = CGAffineTransformMakeScale(sliderVal/30, sliderVal/30);
    CGPoint center = editingView.center; 
    [UIView animateWithDuration:0.5
                       animations:^{
                          editingView.transform = CGAffineTransformConcat(editingView.transform,t);
                          editingView.center = center;
                       }
                       completion:^(BOOL finished) {
    
                       }];
    

    With CGAffineTransformConcat you add 2 transform matrixes together so you won't lose older transforms. You can use CGAffineTransformIdentity to reset the transform.