RomanHouse
RomanHouse

Reputation: 2552

How to change rotation point of frame

I'm trying to animate my frame with rotation:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];

self.toNextButton.transform = CGAffineTransformMakeRotation(180.0f);

[UIView commitAnimations];

But this code rotate frame around his center. How can I make rotation around right bottom corner of frame?

Upvotes: 0

Views: 206

Answers (2)

Romit M.
Romit M.

Reputation: 898

you need to change view.layer's anchorPoints

-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
{

    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y);
    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y);

    newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);

    CGPoint position = view.layer.position;

    position.x -= oldPoint.x;
    position.x += newPoint.x;

    position.y -= oldPoint.y;
    position.y += newPoint.y;

    view.layer.position = position;
    view.layer.anchorPoint = anchorPoint;
}

and you should implement like this.

[self setAnchorPoint:CGPointMake(1,1)];
UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];

self.toNextButton.transform = CGAffineTransformMakeRotation(180.0f);

[UIView commitAnimations];

Upvotes: 2

S.P.
S.P.

Reputation: 3054

Add the following lines before you start animation

self.toNextButton.layer.anchorPoint = // right bottom point on the button

Upvotes: 2

Related Questions