围绕anchorPoint的旋转正在跳跃

时间:2018-07-10 13:20:25

标签: ios cgaffinetransform anchorpoint

我有TagView,它是UIView的子类。我将其围绕锚点旋转,如下所示:

double rads = DEGREES_TO_RADIANS(self.angle);
[self setAnchorPoint:self.anchor forView:self];
CGAffineTransform transform = CGAffineTransformRotate(CGAffineTransformIdentity, rads);
self.transform = transform;

我正在使用建议的here:方法设置锚点

- (void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view {
    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, 
                                   view.bounds.size.height * anchorPoint.y);
    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, 
                                   view.bounds.size.height * view.layer.anchorPoint.y);

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

    CGPoint position = view.layer.position;

    position.x -= oldPoint.x;
    position.x += newPoint.x;

    position.y -= oldPoint.y;
    position.y += newPoint.y;

    view.layer.position = position;
    view.layer.anchorPoint = anchorPoint;
}

我遇到了一个自动布局问题,该问题已通过将视图放入容器UIView并将其设置在中心位置来解决。但是,当我更改视图内的标签内容时,它会跳转。

enter image description here

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。我将蓝色视图设置为具有自动布局约束的固定大小UIView的中心。在我的drawRect方法中,我应用了上面提到的setAnchorPoint方法。问题是,当我设置新的锚点时,我的约束没有立即应用。在内容更改后应用了它们,并将蓝色的“视图”位置移到了粉红色的UIView的中心,如下图所示。

enter image description here

解决方案是强制超级视图布局其子视图。