Jessesiu
Jessesiu

Reputation: 151

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

Upvotes: 1

Views: 4959

Answers (4)

Rupesh R Menon
Rupesh R Menon

Reputation: 429

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

Upvotes: 0

daveMac
daveMac

Reputation: 3037

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>

Upvotes: 2

Bersaelor
Bersaelor

Reputation: 2577

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.

Upvotes: 3

Tozar
Tozar

Reputation: 986

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.

Upvotes: 0

Related Questions