0

我有 TagView,它是 UIView 的子类。我像这样围绕锚点旋转它:

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

我正在使用此处提出的方法设置锚点

- (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 并将其设置为中心来解决这个问题。但是,当我更改视图中标签的内容时,它会跳转。

在此处输入图像描述

4

1 回答 1

0

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

在此处输入图像描述

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

于 2018-08-01T21:11:15.340 回答