Iphone 如何在旋转的圆轨迹上移动uiview?

Iphone 如何在旋转的圆轨迹上移动uiview?,iphone,objective-c,ios,core-animation,Iphone,Objective C,Ios,Core Animation,就像图中的路径一样 (新用户不允许发布图片,抱歉,我只能提供链接) 非常感谢您的帮助请查看“围绕任意点旋转标签”,了解有关旋转时使用定位点的更详细说明(忽略关键帧动画答案,因为它们更复杂,并且不是“此作业的正确工具”,即使答案如此) 简而言之:将锚定点设置为视图以外的点(在视图层的单位坐标系中),然后只应用法线旋转动画 编辑:要记住一件事 层的框架(也是视图的框架)是使用位置、边界和锚定点计算的。更改主播点将更改视图在屏幕上的显示位置。您可以通过在更改定位点后重新设置帧来应对此问题(这将为您

就像图中的路径一样

(新用户不允许发布图片,抱歉,我只能提供链接)

非常感谢您的帮助

请查看“围绕任意点旋转标签”,了解有关旋转时使用定位点的更详细说明(忽略关键帧动画答案,因为它们更复杂,并且不是“此作业的正确工具”,即使答案如此)

简而言之:将锚定点设置为视图以外的点(在视图层的单位坐标系中),然后只应用法线旋转动画


编辑:要记住一件事 层的框架(也是视图的框架)是使用位置、边界和锚定点计算的。更改主播点将更改视图在屏幕上的显示位置。您可以通过在更改定位点后重新设置帧来应对此问题(这将为您设置位置)。否则,您可以将位置设置为您自己旋转的点。(另一个问题中的链接)也提到了这一点


我在回答中提供的代码(概括为UIView和核心动画,而不是UIView动画):


在触摸事件代理上尝试此操作(如果您希望使用触摸事件旋转视图)——


使用transform rotation,并告诉我它是否有效..

实际上,我认为这对OP的特定目标不起作用。anchorPoint特性在(0,0)到(1,1)的范围内,并且必须位于正在旋转的图像/层内。除非你的图像/层比你正在旋转的可见内容大,而视图/层的其余部分是透明的,否则你不能使旋转点位于图像之外。我认为OP必须创建一个变换矩阵来移动,然后围绕图像外的一个点进行旋转(或者是旋转,然后移位?我在将移位与旋转连接起来时总是错误的顺序。)@Duncac文档只提到锚定点是“在单位坐标空间中描述的”,但这并不意味着不能将其设置在范围(0,0)和范围之外(1,1)。事实上,如果锚定点在该范围之外,它似乎可以很好地改变视图的位置。您可以通过将位置设置为正在旋转的点或进行平移-旋转变换来抵消这一点(尽管动画将沿直线移动,而不是与旋转点保持距离)。我将用这些信息更新我的答案。@DavidRönnqvist,我如何以动画方式将其反转以回到初始状态?@Rizon使用负角度进行旋转。这将导致另一个方向的旋转。
#define DEGREES_TO_RADIANS(angle) (angle/180.0*M_PI)

- (void)rotateView:(UIView *)view 
       aroundPoint:(CGPoint)rotationPoint 
          duration:(NSTimeInterval)duration 
           degrees:(CGFloat)degrees {

    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    [rotationAnimation setDuration:duration];
    // Additional animation configurations here...
    
    // The anchor point is expressed in the unit coordinate
    // system ((0,0) to (1,1)) of the label. Therefore the 
    // x and y difference must be divided by the width and 
    // height of the view (divide x difference by width and 
    // y difference by height).
    CGPoint anchorPoint = CGPointMake((rotationPoint.x - CGRectGetMinX(view.frame))/CGRectGetWidth(view.bounds),
                                      (rotationPoint.y - CGRectGetMinY(view.frame))/CGRectGetHeight(view.bounds));

    [[view layer] setAnchorPoint:anchorPoint];
    [[view layer] setPosition:rotationPoint]; // change the position here to keep the frame
    CATransform3D rotationTransform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(degrees), 0, 0, 1);
    [rotationAnimation setToValue:[NSValue valueWithCATransform3D:rotationTransform]];
    
    // Add the animation to the views layer
    [[view layer] addAnimation:rotationAnimation
                        forKey:@"rotateAroundAnchorPoint"]  
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  {
    touches = [event allTouches];
    UITouch *touch = [[event allTouches] anyObject];

    CGPoint Pageposition = [touch locationInView:self];

    CGPoint prevPoint = [[[touches allObjects] objectAtIndex:0] previousLocationInView:self];
    CGPoint curPoint = [[[touches allObjects] objectAtIndex:0] locationInView:self];

    float prevAngle = atan2(prevPoint.x, prevPoint.y);
    float curAngle= atan2(curPoint.x, curPoint.y);
    float angleDifference = curAngle - prevAngle;

    CGAffineTransform newTransform3 = CGAffineTransformRotate(self.transform, angleDifference);
    self.transform = newTransform3;
  }