我正在试着旋转和zoom UIView
,里面有UIImageView
,就像subView
,用一个手指使用UIPanGestureRecognizer
.旋转和zoom 在正常情况下都能很好地工作,但当视图被倒转时,它会朝着相反的方向工作,而不是放大,反之亦然,当在角点旋转时,它也会闪烁,不能正常工作.结果显示在以下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)
}
}
从这个答案中,我对这种方法有了一定的了解
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))
}
当对象没有旋转时,这很好用,但我认为当对象颠倒旋转时,它仍然使用相同的坐标,但实际上它应该以相反的方式使用这些值,有人能帮助我理解或实现这一点吗,谢谢.