hazem
hazem

Reputation: 289

Rotation UIImageView around An anchorPoint

i want to rotate multiple UIImageView around anchor point and give them a little rotation so that could be something like this

https://www.google.com.eg/search?q=cards.&um=1&ie=UTF-8&hl=en&tbm=isch&source=og&sa=N&tab=wi&ei=zzkjUoXFL5GWswbrrIGgBw&biw=1024&bih=615&sei=0TkjUu2wHcmntAbC54Bo#facrc=&imgdii=&imgrc=BdArJtkexKgyQM%3A%3BnKsOa4ZSkOOFcM%3Bhttp%253A%252F%252Fdailywicca.com%252Fwp-content%252Fuploads%252F2011%252F10%252Fplaying-cards1.jpg%3Bhttp%253A%252F%252Fdailywicca.com%252F2011%252F10%252F08%252Freading-with-a-plain-deck-of-cards%252F%3B1920%3B1200

this is my code i think it should work but wear thing happened

int angel=0;

for (int i=0; i<6; i++)
{
    UIImage *image=[UIImage imageNamed:@"images.jpeg"];
    UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
    [self.view addSubview:imageView];
    [imageView setFrame:CGRectMake(0, 0, 80, 80)];
    [imageView setCenter:CGPointMake(150, 150)];
    imageView.layer.anchorPoint = CGPointMake(1.0, 1.0);
    imageView.transform = CGAffineTransformMakeRotation(angel);

    angel+=3;
}

enter image description here

i think they all should be in the same side as iam increasing angle with 3 , how to achieve this ?

Upvotes: 1

Views: 1081

Answers (2)

Mick MacCallum
Mick MacCallum

Reputation: 130222

This is due to the fact that CGAffineTransformMakeRotation() takes radians as input, not degrees. Now, you could easily convert from degrees to radians using a macro like this:

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

Or you could just use smaller input values. In radians, 360 degrees is equal to 2 * π ~ 6.28, so instead of ranging your values assuming 0-360, you should assume 0-2π. When you increase the value by 3, you're effectively increasing it by just under 180 degrees, which is why you're seeing what you are.

Overall, try using angel += 0.052539;. You'll need to change angel from an int to a float as @Kyle W. pointed out

Upvotes: 1

JWK
JWK

Reputation: 3790

I'm not entirely sure I understood your question, but try this:

Change the angle from an int to a float.

float angel = 0; 

Change the increment for each iteration of the loop.

angel += 0.3;

Also, I think you mean "angle" not "angel" ;)

Upvotes: 1

Related Questions