我正在试着旋转和zoom UIView,里面有UIImageView,就像subView,用一个手指使用UIPanGestureRecognizer.旋转和zoom 在正常情况下都能很好地工作,但当视图被倒转时,它会朝着相反的方向工作,而不是放大,反之亦然,当在角点旋转时,它也会闪烁,不能正常工作.结果显示在以下gif中:

issue

another issue

以下是我试图实现这一点的方法:

   @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))
}

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

推荐答案

问题不在于distance(_:_:)函数,而在于center参数.您的center参数是在小图像坐标空间中确定的,但gestureLocation参数是在视图的坐标空间中确定的.您可以使用convert(_:to:) 方法来做到这一点.你可以读到关于它的here.

更新: 实际上,你不需要计算frameScaleBut.centergesture.location(in: self.view)之间的距离.为什么?因为self.initialDistance几乎总是在0左右.因此,let dist = distance(center, touchLocation) - initialDistance永远是积极的.相反,计算gestureSuperView.centergesture.location(in: self.view)之间的距离.gestureSuperView已经在view个坐标空间中.

Ios相关问答推荐

如果条件不满足,S从@ViewBuilder返回什么?

Xcode 15中的版本控制发生了什么?

拖动手势导致 sim 崩溃 Swift UI

发生错误.请稍后重试.在 App Store 连接中

RFID scanner 的蓝牙服务 UUID?

如何在Swift/Combine中从timer.publish属性传递两个参数到任意方法

创建 Flutter 项目时,如何从我的系统中删除特定的开发者身份?

应用程序被杀死时如何在 flutter ios 应用程序中播放音频?

帧过渡动画有条件地工作

SwiftUI - 使用切换switch 切换键盘类型(默认/数字)?

无法初始化 FirebaseApp - Flutter

SensorKit - 获取数据不调用结果委托函数

按钮在 SwiftUI 中没有采用给定的高度和宽度

如何将私钥添加到分发证书?

如何快速设置条形按钮的图像?

如何阻止图像在 UIImageView 中拉伸?

是否可以在没有 Mac 的情况下为 iOS 制作 PhoneGap 应用程序?

iOS WKWebView 不显示 javascript alert() 对话框

我可以在 UIScrollView 中使用 UIRefreshControl 吗?

为什么我需要@1x、@2x 和@3x iOS 图像?