【问题标题】:Resize Rotated UIView from Corners从角落调整旋转的 UIView
【发布时间】:2014-03-30 01:42:09
【问题描述】:

我有一个 UIView selectFrame,它有四个角手柄作为子视图。每个角手柄都有一个 UIPanGestureRecognizer 调用此方法:

这是我的代码(其中 self 是 selectFrame UIView):

-(void)handleDrag:(UIPanGestureRecognizer *)gesture

{
    UIView *view = gesture.view;
    CGPoint translation = [gesture translationInView:view.superview];

    CGRect frame = self.bounds;

    switch (view.tag) {
        case (DRAG_HANDLE_TAG):  //upper left
            frame.size.width -= translation.x;
            frame.size.height -= translation.y;
            break;
        case (DRAG_HANDLE_TAG+1): //upper right
            frame.size.width += translation.x;
            frame.size.height -= translation.y;
            break;
        case (DRAG_HANDLE_TAG+2): //bottom left
            frame.size.width -= translation.x;
            frame.size.height += translation.y;
            break;
        case (DRAG_HANDLE_TAG+3): //bottom right
            frame.size.width += translation.x;
            frame.size.height += translation.y;
            break;
    }
    self.bounds = CGRectIntegral(frame);

    CGAffineTransform transform = self.transform;
    transform = CGAffineTransformTranslate(transform, translation.x/2, translation.y/2);
    self.transform = transform;

    [gesture setTranslation:CGPointZero inView:view.superview];

}

这段代码在没有旋转的情况下工作得很好,但是随着 UIView 的旋转,它会漂移。

但是,如果我在手势结束时进行整个计算,则将

if (gesture.state != UIGestureRecognizerStateEnded)
    return;

在方法的顶部,即使旋转也能很好地工作,所以看起来代码可以工作,但会不会是舍入错误?

这个问题似乎以许多不同的形式被问过,但经过数小时的寻找,我找不到一个确切的答案。有许多可以从中心调整大小的 StickerView 示例。这个问题Resize UIView While It Is Rotated with Transform 得到了一个非常完整的答案,但它并没有真正回答这个问题。

编辑:有很多建议使用三角函数,我已经尝试过,但在我看来,应该可以简单地使用 CGAffineTransforms。我已经尝试过这个,但没有好的结果。

【问题讨论】:

  • 你能解决这个问题吗?
  • 还没有 - 我被转移了,但最终会回到它。希望学习一些线性代数会有所帮助!

标签: ios uiview cgaffinetransform


【解决方案1】:

我在 github 上建立了一个项目来移动、旋转和缩放。它仅使用 CGAffineTransforms 进行旋转。这个项目在视图的角落有拖动手柄,并从对角调整大小和一个旋转手柄。

它使用了 Erica Sadun 的文章 Taking Charge of UIView Transforms in iOS ProgrammingBrad Larson's / Magnus's anchor point code

链接到我的 Resizable 项目:https://github.com/carolight/Resizable

【讨论】:

    【解决方案2】:

    我们在累积不同的转换时也遇到了问题。仅平移或仅缩放或仅旋转都可以正常工作。但是,如果我在平移后旋转,反之亦然,那么旋转会使视图围绕父视图的 0、0 旋转,并且缩放会使图像漂移,如您的问题中所述。

    以下两件事解决了我们的问题:

    1. 对于每个手势,我们将锚点设置为 0.5,如果状态为 UIGestureRecognizerStateBegan,则为我们正在旋转的视图设置 0.5。

      view.layer.anchorPoint = CGPointMake(0.5, 0.5)

    2. 当我们平移或缩放时,我们会得到视图的当前变换,将它与新的(缩放/平移)变换连接起来,如下所示: CGAffineTransformConcat(currentTransformation, newTransformation)

      但是,在旋转的情况下,我们首先使用 newTransformation 并将 currentTransformation 作为第二个参数,如下所示: CGAffineTransformConcat(newTransformation, currentTransformation)

    请注意,我们传递要连接的转换的顺序很重要,如documentation of CGAffineTransformConcat 中所述

    另外,阅读this 以更好地了解anchorPoint,并阅读this 了解它为什么可能与您的问题有关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多