Snapchat-like text on image

后端 未结 4 971
独厮守ぢ
独厮守ぢ 2021-02-06 13:51

I have been trying to implement a Snapchat-like edit text on an image. What I did so far is implement a UILabel in the center of the UIImageView and I added 3 gestures to this U

4条回答
  •  遇见更好的自我
    2021-02-06 14:38

    Found a solution, apparently all I needed to do in the Rotation & Pinch is to always reset the view's rotation / scale. For instance, setting my recognizer.scale to 1.0 and recognizer.rotation to 0.0.

    Here is an example of my code:

    func handlePan(recognizer: UIPanGestureRecognizer) {
        let translation = recognizer.translationInView(view)
        if let view = recognizer.view {
            view.center = CGPoint(x:view.center.x + translation.x,
                y:view.center.y + translation.y)
        }
        recognizer.setTranslation(CGPointZero, inView: view)
    }
    
    func handlePinch(recognizer: UIPinchGestureRecognizer) {
        if let view = recognizer.view as? UILabel {
            let pinchScale: CGFloat = recognizer.scale
            view.transform = CGAffineTransformScale(view.transform, pinchScale, pinchScale)
            recognizer.scale = 1.0
        }
    }
    
    func handleRotate(recognizer: UIRotationGestureRecognizer) {
        if let view = recognizer.view as? UILabel {
            let rotation: CGFloat = recognizer.rotation
            view.transform = CGAffineTransformRotate(view.transform, rotation)
            recognizer.rotation = 0.0
        }
    }
    

提交回复
热议问题