2013-10-24 154 views
0

我遇到了需要将视图重新定位到预定义位置的问题。计算定位点的旋转角度

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

一切工作正常,但只要其中一个手势识别器是活动的,因此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旋转时明显不同,但我不知道如何补偿这一点。

回答

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]; 
    } 
}