我有一个CGPath,我正在尝试旋转,缩放和翻译。我还有一个“可调整大小的”UIView,它可以作为用户的帮助,允许他/她应用转换,因此每当此视图的帧发生更改时,新的转换将应用于所选的CGPath。 此外,我将变换锚点设置为左上角。它可以缩放和旋转。但是,如果我将旋转设置为不同于0然后缩放,则锚点不再位于左上角。看起来它在旋转过程中发生了变化,所以我们假设我们从0开始旋转并一直向下旋转到360,然后将锚点设置回左上角,如我所料。
这是我用来创建转换的代码:
CGPoint anchorPointInPixels = CGPointMake(self.boundingBox.origin.x, self.boundingBox.origin.y);
CGAffineTransform t = CGAffineTransformIdentity;
t = CGAffineTransformTranslate(t, self.translation.x + anchorPointInPixels.x, self.translation.y + anchorPointInPixels.y);
t = CGAffineTransformRotate(t, self.rotation);
t = CGAffineTransformScale(t, self.scale.x, self.scale.y);
t = CGAffineTransformTranslate(t, -anchorPointInPixels.x, -anchorPointInPixels.y);
self.transform = t;
让我解释一下这段代码: 1.路径的点是绝对坐标 2.边界框仅计算一次,并将其设置为包围路径中所有点的矩形。边界框也是绝对坐标 3.翻译指定了边界框原点的偏移量,因此当创建路径时,平移等于0并且在用户移动之前它仍然是这样的
那么,如何让它旋转,而不影响锚点?
感谢阅读!
马里亚诺
答案 0 :(得分:0)
所以我能够通过连接矩阵来解决这个问题。这是代码的样子:
CGPoint anchorPointForScalingInPixels = CGPointMake(origin.x + size.width * self.anchorPointForScaling.x,
origin.y + size.height * self.anchorPointForScaling.y);
CGPoint anchorPointForRotationInPixels = CGPointMake(origin.x + size.width * self.anchorPointForRotation.x,
origin.y + size.height * self.anchorPointForRotation.y);
CGAffineTransform rotation = CGAffineTransformIdentity;
rotation = CGAffineTransformTranslate(rotation, anchorPointForRotationInPixels.x, anchorPointForRotationInPixels.y);
rotation = CGAffineTransformRotate(rotation, self.rotation);
rotation = CGAffineTransformTranslate(rotation, -anchorPointForRotationInPixels.x, -anchorPointForRotationInPixels.y);
CGAffineTransform scale = CGAffineTransformIdentity;
scale = CGAffineTransformTranslate(scale, anchorPointForScalingInPixels.x, anchorPointForScalingInPixels.y);
scale = CGAffineTransformScale(scale, self.scale.x, self.scale.y);
scale = CGAffineTransformTranslate(scale, -anchorPointForScalingInPixels.x, -anchorPointForScalingInPixels.y);
CGAffineTransform translate = CGAffineTransformMakeTranslation(self.translation.x, self.translation.y);
CGAffineTransform t = CGAffineTransformConcat(rotation, CGAffineTransformConcat(scale, translate));
这样我可以处理两个锚点,一个用于旋转,另一个用于缩放。