UIimageView围绕一个点旋转

发布于 2024-10-01 14:33:51 字数 279 浏览 6 评论 0原文

我有一个图像(一条直线),并尝试从特定点以某个角度旋转它。发生的情况是图像从其自身的中心点旋转。我不想要一种使图像的底部保持不变并且以我想要的角度旋转的方式,就像时钟一样。 我使用

Compass2.layer.anchorPoint= CGPointMake(0.5,1);
[Compass2 setTransform:CGAffineTransformMakeRotation(XXXX)];

但是它显示访问属性的未知“achorPoint”组件。 谁能给我一些解决方案

I've got an image (a straight line) and am trying to rotate it in some angle from a specific point. What happens is that the image is rotated from the center point of itself. I wan't a way through which the base of the image remains the same and it rotates in the angle I want to, just as in case of a clock.
I use

Compass2.layer.anchorPoint= CGPointMake(0.5,1);
[Compass2 setTransform:CGAffineTransformMakeRotation(XXXX)];

However it shows Accessing unknown 'achorPoint' component of a property.
Can anyone give me some solutions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月流歌 2024-10-08 14:33:51

CGAffineTransform 只是一个描述旋转、平移和缩放的矩阵。

请记住,您可以使用

CGAffineTransform CGAffineTransformConcat ( CGAffineTransform t1, CGAffineTransform t2 );

链接变换。这基本上意味着您正在乘以变换矩阵。

所以既然你知道,标准旋转只是围绕 UIImageView 的中心旋转,你可以将你的任务分成 3 部分,

  1. 移动到旋转点
  2. 旋转
  3. 向后移动

并将它们链接起来。

CGAffineTransform t = imageView.transform;
CGPoint p = rotationPoint - imageView.center;
imageView.transform = CGAffineTransformTranslate(
CGAffineTransformRotate( CGAffineTransformTranslate(t, p.x, p.y), angle) , -p.x, -p.y);

我没有测试这段代码,但你应该通过这种方式得到一个解决方案。

编辑:我还意识到我没有使用串联。如果使用“CGAffineTransformMake...”,则需要使用串联。我只是将这些功能相互放入。

Well a CGAffineTransform is just a Matrix describing rotation, translation and scaling.

Remember you can use

CGAffineTransform CGAffineTransformConcat ( CGAffineTransform t1, CGAffineTransform t2 );

to chain up transforms. This basically just means you are multiplying the transformation matrizes.

So since you know, the standard Rotation just rotates around the center of the UIImageView, you could break up your task into 3 parts,

  1. moving into the rotation point
  2. rotate
  3. move back

and chain them up.

CGAffineTransform t = imageView.transform;
CGPoint p = rotationPoint - imageView.center;
imageView.transform = CGAffineTransformTranslate(
CGAffineTransformRotate( CGAffineTransformTranslate(t, p.x, p.y), angle) , -p.x, -p.y);

I didn't test this code, but you should get a solution along this way.

EDIT: I also realized I didn't use the Concatenation. You need to use the concatenation if you use "CGAffineTransformMake...". I just put the functions into each other.

旧伤慢歌 2024-10-08 14:33:51

我也有同样的问题。正如“aBitObvious”所指出的,您必须导入QuartzCore框架才能访问CALayer类的anchorPoint属性。

#import <QuartzCore/QuartzCore.h>

I had this same problem. As "aBitObvious" pointed out, you must import the QuartzCore framework to access the anchorPoint property of the CALayer class.

#import <QuartzCore/QuartzCore.h>
晚风撩人 2024-10-08 14:33:51

它给您的错误意味着您正在尝试设置一个不存在的属性。您是否拼错了“anchorPoint”?如果情况并非如此,并且您的问题只是有一个拼写错误,请仔细检查您的类型。您的“Compass2”应该是某种 UIView,其“图层”应该是 CALayer 对象。

The error it is giving you implies you are trying to set a property that doesn't exist. Did you misspell 'anchorPoint'? If that isn't the case and your question merely has a typo, double check your types. Your 'Compass2' should be a UIView of some sort and its 'layer' should be a CALayer object.

反话 2024-10-08 14:33:51

您可能无法仅通过导入

#import <QuartzCore/QuartzCore.h>

ie 来访问视图的属性anchorPoint;

Compass2.layer.anchorPoint= CGPointMake(0.5,1);

为此,您必须在实现文件的开头编写以下导入语句。

#import <QuartzCore/CALayer.h> 

现在您将能够编写以下代码而不会产生任何错误。

Compass2.layer.anchorPoint= CGPointMake(0.5,1);

You may not be able to access the property anchorPoint for your view by just importing

#import <QuartzCore/QuartzCore.h>

ie;

Compass2.layer.anchorPoint= CGPointMake(0.5,1);

For that you have to write the following import statement in the beginning of the implementation file.

#import <QuartzCore/CALayer.h> 

Now you will be able to write the following code without generating any errors.

Compass2.layer.anchorPoint= CGPointMake(0.5,1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文