gpt4 book ai didi

ios - 在一个点上进行变换旋转的 CAAnimation

转载 作者:行者123 更新时间:2023-11-29 03:10:55 24 4
gpt4 key购买 nike

我正在尝试为 View 图层在任意点上的旋转设置动画。动画的开始位置将从结束和最终位置旋转 90º。该 View 占据了除状态栏之外的所有屏幕。

我正在尝试使用以下代码(带有平移,然后是旋转和反平移)但是,无论如何开始和结束,动画并不以点为中心,而是围绕它摆动。

CABasicAnimation *animation = [CABasicAnimation   animationWithKeyPath:@"transform"];
animation.duration = 1;
animation.additive = YES;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeRemoved;

//The point that should be used as the center of rotation
CGPoint cursorCenter=self.angleCenter;
//The center of the view
CGPoint linesCenter=lines.center;

//The transformation: translate->rotate->anti-translate
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, cursorCenter.x-linesCenter.x, cursorCenter.y-linesCenter.y, 0.0);
transform = CATransform3DRotate(transform, degreesToRadians(-90), 0.0, 0.0, -1.0);
transform = CATransform3DTranslate(transform, linesCenter.x-cursorCenter.x, linesCenter.y-cursorCenter.y, 0.0);

animation.fromValue =[NSValue valueWithCATransform3D:transform];
animation.toValue =[NSValue valueWithCATransform3D:CATransform3DIdentity];

[lines.layer addAnimation:animation forKey:@"90rotation"];

我做错了什么?

最佳答案

您无需进行平移即可在特定点上旋转。只需更改图层的anchorPoint以调整旋转中心

The origin of the transform is the value of the center property, or the layer’s anchorPoint property if it was changed.

关于 anchor 以及如何指定它:

You specify the value for this property using the unit coordinate space. The default value of this property is (0.5, 0.5), which represents the center of the layer’s bounds rectangle. All geometric manipulations to the view occur about the specified point. For example, applying a rotation transform to a layer with the default anchor point causes the layer to rotate around its center. Changing the anchor point to a different location would cause the layer to rotate around that new point.

enter image description here

基于CALayer Class ReferenceCore Animation Programming Guide

关于ios - 在一个点上进行变换旋转的 CAAnimation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22260318/

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