CABasicAnimation在x轴上旋转视图
问题描述:
我需要在180度的x轴上旋转我的视图。旋转完成,但当它开始旋转时,它会折叠我的半视图。这是我所做的任何帮助的代码。CABasicAnimation在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"];
答
您必须在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;
}
您可以发布折叠视图的图像吗? – shallowThought
[使用CABasicAnimation多次旋转UIImageView]的可能重复(http://stackoverflow.com/questions/17426802/using-cabasicanimation-to-rotate-a-uiimageview-more-than-once) –