0

我遇到了一个问题,我需要将视图重新定位到预定义的位置。

所有视图都有 aUIPanGestureRecognizer和 aUIRotationGestureRecognizer并且在控制器视图内定位/旋转。在某个事件中,视图应该以新的旋转角度移动到新位置。

一切正常,但是一旦其中一个手势识别器处于活动状态,因此anchorPoint更改后的重新定位/旋转失败。

这是我尝试使用的方法来考虑这种转变anchorPoint

  - (CGPoint)centerPointWithInVisibleAreaForPoint:(CGPoint)point  
 {
    CGPoint anchorP = self.layer.anchorPoint;
      anchorP.x    -= 0.5;
      anchorP.y    -= 0.5;

    CGRect rect = self.bounds;

    CGFloat widthDelta  = CGRectGetWidth(self.bounds)  * anchorP.x;
    CGFloat heightDelta = CGRectGetHeight(self.bounds) * anchorP.y;

    CGPoint newCenter = CGPointMake(point.x + widthDelta, point.y + heightDelta);

    return newCenter;
}

控制器要求校正中心点并设置视图的中心值。之后使用 设置旋转变换CGAffineTransformConcat(view.transform, CGAffineTransformMakeRotation(differenceAngle))

我认为问题是由于预定义的目标角度基于围绕中心的旋转,当围绕不同的旋转时明显不同anchorPoint,但我不知道如何弥补这一点。

4

1 回答 1

0

我找到的唯一解决方案(毕竟是最简单的解决方案)是将 anchorPoint 重置为 0.5/0.5 并相应地更正位置。

- (void)resetAnchorPoint
{
   if (!CGPointEqualToPoint(self.layer.anchorPoint, CGPointMake(0.5, 0.5))) {

    CGFloat width = CGRectGetWidth(self.bounds);
    CGFloat height = CGRectGetHeight(self.bounds);

    CGPoint newPoint = CGPointMake(width * 0.5, height * 0.5);
    CGPoint oldPoint = CGPointMake(width * self.layer.anchorPoint.x, height * self.layer.anchorPoint.y);

    newPoint = CGPointApplyAffineTransform(newPoint, self.transform);
    oldPoint = CGPointApplyAffineTransform(oldPoint, self.transform);

    CGPoint position = self.layer.position;
    position.x += (newPoint.x - oldPoint.x);
    position.y += (newPoint.y - oldPoint.y);

    [CATransaction setDisableActions:YES];
    self.layer.position    = position;
    self.layer.anchorPoint = CGPointMake(0.5, 0.5);
    [CATransaction setDisableActions:NO];
  }
}
于 2014-01-06T13:22:10.690 回答