【问题标题】:CABasicAnimation rotate view on x axisCABasicAnimation 在 x 轴上旋转视图
【发布时间】:2017-04-06 09:26:59
【问题描述】:
我需要将我的视图在 x 轴上旋转 180 度。旋转完成,但当它开始旋转时,它会折叠我的半视图。这是我所做的代码可以帮助任何人。
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"];
【问题讨论】:
标签:
ios
animation
rotation
cabasicanimation
【解决方案1】:
您必须最好在 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;
}