0

I need to rotate my view on x axis with 180 degree. Rotation is done but when it start rotating it folds my half view. This is the code I have done can any one help.

        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
        rotationAnimation.fromValue = [NSNumber numberWithFloat:0]; 
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];   
        rotationAnimation.duration = 2.0;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = 0;
        [_view_topShowDate.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
Niharika
  • 1,188
  • 15
  • 37
  • Can you please post an image of the folded view? – shallowThought Apr 06 '17 at 09:38
  • Possible duplicate of [Using CABasicAnimation to rotate a UIImageView more than once](http://stackoverflow.com/questions/17426802/using-cabasicanimation-to-rotate-a-uiimageview-more-than-once) – dahiya_boy Apr 06 '17 at 09:39

1 Answers1

0

You have to set the anchor point preferably in viewDidLoad or somewhere before the animation starts. by default, it takes the anchorPoint as (0.5,0.5) and hence you see the rotation from the center.

_viewToRotate.layer.anchorPoint = CGPointMake(0.5, 1.0);

And then call the basic animation method.

Read more on http://ronnqvi.st/about-the-anchorpoint/

While you change the anchor point, it moves the layer's position. to reposition it, use the following

-(void)setNewAnchorPoint:(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;
}
Jen Jose
  • 3,995
  • 2
  • 19
  • 36