UIView 作为 maskView- 由约束常量移动的原点

UIView as maskView- origin shifted by constraint constants

我正在尝试实现用户使用的照片裁剪工具,但卡住了。我有一个 UIView (imageCropper) 位于 UIImageView (imageToBeEdited) 的顶部(z 顺序),其中包含正在裁剪的图像。如果此时我 运行 应用程序,使用我的其他配置和逻辑,imageCropper 就在图像的顶部。但是,当我将 imageToBeEditedmaskView 设置为 imageCropper 时, imageCropper 的原点发生了变化。在弄乱了约束常量之后,我确定它正在向更大的 x 移动,增加了前导约束的精确值,并移动到更大的 y,增加了顶部约束的精确值。

另一条信息使这更令人困惑,是我在 imageCropper 的顶部(再次按 z 顺序)还有另一个 UIViewimageCropperBorder)仅作为可见边界。我将它的框架设置为与 imageCropper 完全相同的框架,但它是它们应该在的位置。

我没有在 imageCropperimageCropperBorder 上设置限制。

我的问题是 为什么 imageCropper 发生了变化,我该如何解决这个问题?

在尝试了几种不同的方法来创建用户启用的平滑移动裁剪工具后,我回到了这个解决方案,并通过以编程方式获取 viewDidLoad 中的约束值然后简单地创建了 x 和 y 偏移量在 viewDidLayoutSubviews 中调整 imageCropper 的原点并且有效。

但是,我在 ViewController 的视图上设置了 panGestureRecognizer。一旦我开始拖动照片 (imageCropper) 进行裁剪,imageCropper 原点再次向下移动。我尝试将偏移逻辑添加到处理识别器的函数中,但它似乎在混合更改并将帧发送出屏幕。 (我会在每次收到识别器时重置翻译。)

代码:

override func viewDidLayoutSubviews() {
    resizedImageFrame = AVMakeRectWithAspectRatioInsideRect(imageToEdit.image!.size, imageToEdit.bounds)
    let resizedImageCenter = imageToEdit.center
    let resizedImageX = resizedImageCenter.x - (resizedImageFrame.width / 2)
    let resizedImageY = resizedImageCenter.y - (resizedImageFrame.height / 2)
    resizedImageFrame.origin = CGPoint(x: resizedImageX, y: resizedImageY)
    currentImageFrame = resizedImageFrame
    imageCropperBorder.frame = resizedImageFrame
    imageCropper.frame = CGRectOffset(resizedImageFrame, -cropperXOffset, -cropperYOffset)
    // imageCropper.frame above is set with the offset discussed. If I set it
    // to simply resizedImageFrame it does NOT appear where imageCropperBorder is
}

一如既往,我们将不胜感激。

当您将 imageCropper 设置为 imageView 的 maskView 时。它将使用 imageView 的坐标系来定位 imageCropper,因此对于 imageCropper,点 (0,0) 不在 ViewController 的视图顶部 -左角但 imageView 的左上角。