使用CGAffineTransform在视图上应用缩放和旋转 [英] Applying scaling and rotation on a view using CGAffineTransform

查看:69
本文介绍了使用CGAffineTransform在视图上应用缩放和旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我需要对视图应用一定的缩放比例和旋转度(我使用手势来完成此操作),因此对于每个手势,我都会使用以下内容更新当前的缩放比例和旋转度值:

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 like:

self.scaleWidth *= gesture.scale; //When I detect an horizontal pinch
self.scaleHeight *= gesture.scale; //When I detect a vertical pinch
self.rotationAngle += gesture.rotationAngle; //When I detect a rotation

然后我执行以下操作以变换视图:

Then I do the following to transform the view:

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

当我仅进行缩放时,它可以正常工作.当我进行缩放然后旋转时,它旋转得很好.旋转后,我尝试再次缩放时,效果不佳:旋转后好像应用缩放一样,会使图像结构变形.

When I only do scaling, it works fine. When I do scaling then rotate, it rotates fine. When after rotating I try to scale again, it doesn't work fine: the scale is applied as if done after the rotation, deforming my image structure.

我想我做了避免此情况的措施:每次从身份转换开始,先缩放然后旋转,但是显然我错了...

I thought I did what needed to be done to avoid this: starting each time from an identity transform, scaling first then rotating, but obviously I was wrong...

有人知道我的实现有什么问题吗?

Does any one know what's wrong with my implementation ?

谢谢

推荐答案

如果每次都从身份变换开始,则将子视图的变换设置为的最终结果将仅包括当前手势的缩放和旋转.从视图的当前转换开始,而不是从标识开始.

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.

这篇关于使用CGAffineTransform在视图上应用缩放和旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆