Applying scaling and rotation on a view using CGAffineTransform

前端 未结 4 725
轮回少年
轮回少年 2021-01-11 10:45

So I need to apply some scaling and some rotation to a view (I do this using gestures), so for each gesture I update the current scalling and rotation values with something

相关标签:
4条回答
  • 2021-01-11 11:15

    For swift 3:

    view.transform = CGAffineTransform(rotationAngle:  CGFloat.pi).concatenating(CGAffineTransform(scaleX: 0.4, y: 0.4))
    
    0 讨论(0)
  • 2021-01-11 11:18

    If you start with the identity transform every time, the end result you are setting the subview's transform to will only include the scaling and rotation from the current gesture. Instead of starting with the identity, start with the current transform of the view.

    CGAffineTransform transform = self.theSubViewToTransform.transform;
    transform = CGAffineTransformScale(transform, self.scaleWidth, self.scaleHeight);
    transform = CGAffineTransformRotate(transform, self.rotationAngle);
    self.theSubViewToTransform.transform = transform;
    

    The first time this executes, the transform will start out as the identity transform. On subsequent executions, it will make the new changes on top of the old ones.

    0 讨论(0)
  • 2021-01-11 11:28

    Try applying the transformations to the identity transform, e.g.

    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformScale(transform, scaleWidth, scaleHeight);
    transform = CGAffineTransformRotate(transform, angle);
    viewToTransform.transform = transform;
    
    0 讨论(0)
  • 2021-01-11 11:29

    You should start from your current transformed state and apply transformation which is expected. Also you can have a look at CGAffineTransformConcat, it will make it a single transform before applying.

    CGAffineTransform transform = yourView.transform;
    transform = CGAffineTransformConcat(CGAffineTransformScale(transform,  self.scaleWidth, self.scaleHeight),
                                        CGAffineTransformRotate(transform, self.rotationAngle));
    yourView.transform = transform;
    

    Hope it helps!

    0 讨论(0)
提交回复
热议问题