如何获得旋转视图的完美位置?

我有一个视图,我正在使用CABasicAnimation旋转该视图。 现在我的问题是如何在旋转时获得该视图的完美位置。 我尝试了很多类型的代码,但在旋转视图时我无法获得完美的位置。

 CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; NSNumber *currentAngle = [CircleView.layer.presentationLayer valueForKeyPath:@"transform.rotation"]; rotationAnimation.fromValue = currentAngle; rotationAnimation.toValue = @(50*M_PI); rotationAnimation.duration = 50.0f; // this might be too fast rotationAnimation.repeatCount = HUGE_VALF; // HUGE_VALF is defined in math.h so import it [CircleView.layer addAnimation:rotationAnimation forKey:@"rotationAnimationleft"]; 

我正在使用此代码来旋转我的视图。

我还附上了我的一张照片。

谢谢你提前请帮助你知道。

在此处输入图像描述

要在动画期间获取视图的参数,您应该使用view.layer.presentationLayer

添加:

要获取视图左上角的坐标,请使用以下代码:

 - (CGPoint)currentTopLeftPointOfTheView:(UIView *)view { CGRect rect = view.bounds; rect.origin.x = view.center.x - 0.5 * CGRectGetWidth(rect); rect.origin.y = view.center.y - 0.5 * CGRectGetHeight(rect); CGPoint originalTopLeftCorner = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect)); CGPoint rectCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); CGFloat radius = sqrt(pow(rectCenter.x - originalTopLeftCorner.x, 2.0) + pow(rectCenter.y - originalTopLeftCorner.y, 2.0)); CGFloat originalAngle = M_PI - acos((rectCenter.x - originalTopLeftCorner.x) / radius); CATransform3D currentTransform = ((CALayer *)view.layer.presentationLayer).transform; CGFloat rotation = atan2(currentTransform.m12, currentTransform.m11); CGFloat resultAngle = originalAngle - rotation; CGPoint currentTopLeftCorner = CGPointMake(round(rectCenter.x + cos(resultAngle) * radius), round(rectCenter.y - sin(resultAngle) * radius)); return currentTopLeftCorner; } 

生成的CGPoint将是(旋转)视图左上角相对于其超视图的坐标。

设置你所喜欢的图层的位置

 [layer setPosition:endpoint]; 

或者您也可以将此CABasicAnimation旋转返回到原始位置