iOS

时间:2016-03-02 19:33:50

标签: ios swift pinch snapchat

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

可以旋转,缩放和拖动表情符号。我使用UIGestureRecognizer(如UIRoationGestrueRecognizer,UIPinchGestureRecognizer和UIPanGesstureRecognizer)实现了这些功能。

现在我正在尝试使用类似于类似兴趣的缩放功能更新应用,用户可以将两个手指之间的表情符号放大/缩小到极致。当前捏合手势仅在用户'手指在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()
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我同时也喜欢拖拽,平移和缩放,但是如果你只是想寻找缩放。我使用以下功能让标签通过捏缩放。它不顺畅,但做缩放工作。

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

答案 1 :(得分:0)

要实现诸如捏缩放之类的功能,请在“父”视图上也添加捏手势,而不是像识别器那样按如下所示变换所选标签:

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