CABasicAnimation在x轴上旋转视图

时间:2017-04-06 09:26:59

标签: ios animation rotation cabasicanimation

我需要在180度的x轴上旋转我的视图。旋转完成但是当它开始旋转时它会折叠我的半视图。这是我所做的代码可以任何一个帮助。

        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"];

1 个答案:

答案 0 :(得分:0)

您必须首先在viewDidLoad或动画开始前的某个位置设置定位点。默认情况下,它将anchorPoint视为(0.5,0.5),因此您可以看到从中心开始的旋转。

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

然后调用基本的动画方法。

详细了解http://ronnqvi.st/about-the-anchorpoint/

当您更改定位点时,它会移动图层的位置。 要重新定位它,请使用以下

-(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;
}