UIView上下旋转时的缩放问题



我正试图用一根手指使用UIPanGestureRecognizer将内部有UIImageViewUIView旋转和缩放为subView。旋转和缩放在正常情况下都可以正常工作,但当视图上下旋转时,它会以相反的方向工作,而不是按比例放大,它会按比例缩小,反之亦然,而且当在角点旋转时,有点闪烁,无法正常工作。结果显示在这些gif中:

发布

另一个问题

以下是我尝试实现这一目标的方法:

@objc func ScaleGest(gesture: UIPanGestureRecognizer) {
guard let gestureSuperView = gesture.view?.superview else { return }
let center = frameScaleBut.center
let touchLocation = gesture.location(in: self.view)

if gesture.state == UIPanGestureRecognizer.State.began {
self.initialDistance = distance(center, touchLocation)
} else if gesture.state == UIPanGestureRecognizer.State.changed {

let dist = distance(center, touchLocation) - initialDistance

let smallScale: CGFloat = editingMode == .sticker ? 90 : 190
let nextScale: CGSize = CGSize(width: gestureSuperView.bounds.width + dist, height: gestureSuperView.bounds.height + dist)

if (nextScale.width >= (self.view.frame.width * 0.8)) || nextScale.width <= smallScale {
return
}

gestureSuperView.bounds.size = CGSize(width: gestureSuperView.bounds.width + dist, height: gestureSuperView.bounds.height + dist)
}
}

我从这个答案中得出这个方法的距离https://stackoverflow.com/a/1906659/20306199

func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
let xDist = a.x - b.x
let yDist = a.y - b.y
return CGFloat(sqrt(xDist * xDist + yDist * yDist))
}

当物体没有旋转时,这很好,但我认为当物体被倒置旋转时,它仍然使用相同的坐标,但实际上应该以相反的方式使用这些值,有人能帮助我理解或实现这一点吗,谢谢。

问题不在distance(_:_:)函数中,而在center参数中。center参数是在小图像坐标空间中确定的,但gestureLocation是在视图的坐标空间中决定的。您可以使用convert(_:to:)方法来执行此操作。你可以在这里阅读。

UPD:实际上,您不需要计算frameScaleBut.centergesture.location(in: self.view)之间的距离。为什么?因为CCD_ 12几乎总是在0左右。所以let dist = distance(center, touchLocation) - initialDistance总是正的。而是计算gestureSuperView.centergesture.location(in: self.view)之间的距离。CCD_ 16已经在CCD_ 17坐标空间中。

最新更新