开发者

UIimageView rotate around a point

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


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.


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>


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜