2

我在swift中发布了iOS应用程序,其主要功能是:1)添加照片/拍照2)在照片上添加表情符号3)缩放,旋转,拖动表情符号来装饰照片4)在Instagram上分享。

表情符号可以旋转、缩放和拖动。我已经使用 UIGestureRecognizer 实现了这些功能,例如 UIRoationGestrueRecognizer、UIPinchGestureRecognizer 和 UIPanGestureRecognizer。

现在我正在尝试使用类似 snapchat 的捏缩放功能来更新应用程序,用户可以在其中将两个手指之间的表情符号放大/缩小到极致。当前的捏合手势仅在用户的手指在 imageView(表情符号)上时才有效。

任何想法/示例代码如何进行类似 snapchat 的捏缩放?下面的代码是我如何处理旋转、捏合和拖动。提前致谢。

// UI Gesture Recognizers
@IBAction func handlePinch(recognizer : UIPinchGestureRecognizer) {
    if(deleteMode) {
        return
    }
    if let view = recognizer.view {
        view.transform = CGAffineTransformScale(view.transform,
            recognizer.scale, recognizer.scale)
        recognizer.scale = 1
    }
}

@IBAction func handleRotate(recognizer : UIRotationGestureRecognizer) {
    if(deleteMode) {
        return
    }
    if let view = recognizer.view {
        view.transform = CGAffineTransformRotate(view.transform, recognizer.rotation)
        recognizer.rotation = 0
    }
}

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
    if(deleteMode) {
        return
    }
    let translation = recognizer.translationInView(self.view)
    var centerX: CGFloat!
    var centerY: CGFloat!

    if let view = recognizer.view {

        // limit the boundary - using backgroundPanel.frame.width, height, origin.x, origin.y
        if(view.center.x + translation.x < panelBackground.frame.origin.x) {
            centerX = view.center.x + translation.x + 10
        } else if(view.center.x > panelBackground.frame.size.width){
            centerX = view.center.x + translation.x - 10
        } else {
            centerX = view.center.x + translation.x
        }

        if(view.center.y < panelBackground.frame.origin.y - 60){
            // set y that I can use below
            centerY = view.center.y + translation.y + 10
        } else if(view.center.y > panelBackground.frame.size.height){
            centerY = view.center.y + translation.y - 10
        } else {
            centerY = view.center.y + translation.y
        }

        // set final position
        view.center = CGPoint(x:centerX,
            y:centerY)

        recognizer.setTranslation(CGPointZero, inView: self.view)

    }
}

@IBAction func handleLongPress(recognizer: UILongPressGestureRecognizer) {
    if(recognizer.state == UIGestureRecognizerState.Began) {
        if(!deleteMode) {
            print("LongPress - Delete Shows")
            for (_, stickers) in self.backgroundImage.subviews.enumerate() {
                for (_, deleteButtons) in stickers.subviews.enumerate() {
                    if let delete:UIImageView = deleteButtons as? UIImageView{
                        if(delete.accessibilityIdentifier == "delete") {
                            delete.alpha = 0.5
                        }
                    }
                }
            }
            deleteMode = true
        } else {
            deleteButtonHides()
        }
    }
}
4

2 回答 2

1

我也在像 snapchat 一样同时寻找拖动、平移和缩放,但如果你只是在寻找缩放。我正在使用以下功能让标签通过捏合进行缩放。它不流畅,但可以进行缩放工作。

  func handlePinch(recognizer: UIPinchGestureRecognizer) {
    if let view = recognizer.view as? UILabel {
        let pinchScale: CGFloat = recognizer.scale
        view.transform = view.transform.scaledBy(x: pinchScale, y: pinchScale)
        recognizer.scale = 1.0
    }
}

要同时进行拖动、平移和缩放,请查看我的以下帖子:

同时捏合、拖动和平移

于 2016-12-02T14:39:32.963 回答
0

要实现像捏缩放这样的 snapchat,还要在父视图上添加捏手势,而不是识别器转换选定的贴纸,如下所示:

@objc func mainImgPinchGesture(_ recognizer: UIPinchGestureRecognizer) {
       print("----pinchGestureAction")
       if let view = recognizer.view {
        if selectedSubView != nil{
            self.selectedSubView.transform = view.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
            self.selectedSubView.contentScaleFactor = 1
         }
       }
   }
于 2020-01-06T08:20:43.220 回答