gpt4 book ai didi

ios - 如何获得完美的旋转 View 位置?

转载 作者:行者123 更新时间:2023-12-01 18:47:00 25 4
gpt4 key购买 nike

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

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 。

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

提前谢谢你如果你知道请帮忙。

enter image description here

最佳答案

要在动画期间获取 View 的参数,您应该使用 view.layer.presentationLayer
已添加:

为了获取 View 左上角的坐标,使用以下代码:

- (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将是您的(旋转的) View 的左上角相对于其父 View 的坐标。

关于ios - 如何获得完美的旋转 View 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34824409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com