使用触摸点或 UIPanGestureRecognizer 调整 CAShape 层的大小

问题描述 投票:0回答:0

我已经使用 UIBezierPath 绘制了一个 CAShapeLayer,并在 ImageView 上进行了触摸,这样我就可以在指定的 ImageView 上绘制任何形状的 CAShapeLayer。 Image for reference that I'm able to draw the CAShapeLayer With touch. 我的第二个目标是平移/移动绘制的 CAShape 层,这也是我通过向图像视图添加平移手势识别器并能够将 CAShape 层的位置转换为图像视图层来实现的。 Reference for the CAShape Layer Translated using UIPanGesture Recogniser

现在我正在尝试使用相同的平移手势识别器调整 CAShape 层的大小,但这次它消失了,因为翻译值对我的假设来说太大/太小了。

我尝试翻译 CAShape Position 并实现它。

`func addPanGesture(查看:UIImageView){ 让 panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:))) self.displayImageView.addGestureRecognizer(panGesture) 打印(“添加平移到选定的多边形形状”) polygonSelectedShape.panGetureFinishedBool = true }

@objc func handlePanGesture(_ gestureRecognizer: UIPanGestureRecognizer) {
    guard polygonSelectedShape.panGetureFinishedBool else {return} //Annotation Shape Class File (PolygonSelectedShape)
    switch gestureRecognizer.state{
    case .began:
        print("Pan Gesture began : ", polygonSelectedShape.path as Any)
    case .possible:
        print("Pan Gesture began : ", polygonSelectedShape.path as Any)
    case .changed:
        let translation                  = gestureRecognizer.translation(in: displayImageView)
        let originalPath                 = polygonSelectedShape.path
        var polyTranslation              = CGAffineTransform(translationX: translation.x, y: translation.y)
        let translatePolyPath            = originalPath?.copy(using: &polyTranslation)
        polygonSelectedShape.position    = CGPoint(x: polygonSelectedShape.position.x + translation.x, y: polygonSelectedShape.position.y + translation.y)
        gestureRecognizer.setTranslation(.zero, in: displayImageView)
        polygonSelectedShape.path        = translatePolyPath
        polyCAShapeDictionary.updateValue(polygonSelectedShape, forKey: polySelectedShapekey)
        polygonSavedShape                = polygonSelectedShape
    case .ended:
        print("Ended : - Pan Gesture")
        polygonSelectedShape.panGestureBool        = false
        (panButton.backgroundColor, resizeButton.backgroundColor) = (.clear, .clear)
        self.removePanGesture(to: displayImageView)
        polygonSelectedShape.panGetureFinishedBool = false
        polygonSavedShape                          = polygonSelectedShape
        polyCAShapeDictionary.updateValue(polygonSelectedShape, forKey: polySelectedShapekey)
    case .cancelled:
        print("Cancelled - Pan Gesture")
    case .failed:
        print("Failed - Pan Gesture")
    @unknown default:
        break
    }
}`

上面的代码是从一个位置翻译到另一个位置

请提出一个在图像视图上使用触摸控制调整绘制的 CAShape 层大小的想法。

swift touch-event uibezierpath uipangesturerecognizer cashapelayer
© www.soinside.com 2019 - 2024. All rights reserved.