简体   繁体   English

应用平移手势后如何在捏合手势上定位视图?

[英]how to position a view on pinch gesture after pan gesture applied?

I stuck in a problem while working with gestures.我在使用手势时遇到了问题。 Actually I want to increase the font of the label on pinch gesture so that I can maintain the quality of the text, I am doing that easily.实际上,我想在捏合手势上增加标签的字体,以便我可以保持文本的质量,我很容易做到这一点。 The problem is if I drag the view anywhere else and then apply the pinch gesture, it automatically places the view on its actual position.问题是,如果我将视图拖动到其他任何地方,然后应用捏合手势,它会自动将视图放置在其实际位置。

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    @IBOutlet weak var myLabel: UILabel!
    @IBOutlet weak var myTextView: UITextView!

    var initialCenter = CGPoint()
    var pointSize: CGFloat = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchRecoginze(_:)))
        pinchGesture.delegate = self
        myLabel.addGestureRecognizer(pinchGesture)
        let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(self.rotationGestureRecognizer(_:)))
        rotationGesture.delegate = self
        myLabel.addGestureRecognizer(rotationGesture)
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGestureRecognizer(_:)))
        panGesture.maximumNumberOfTouches = 1
        panGesture.delegate = self
        myLabel.addGestureRecognizer(panGesture)
    }

    @objc func panGestureRecognizer(_ recognizer: UIPanGestureRecognizer) {
        guard recognizer.view != nil else {return}
        let piece = recognizer.view!
        let translation = recognizer.translation(in: piece.superview)
        if recognizer.state == .began {
            self.initialCenter = piece.center
        }
        // Update the position for the .began, .changed, and .ended states
        if recognizer.state != .cancelled {
            // Add the X and Y translation to the view's original position.
            let newCenter = CGPoint(x: initialCenter.x + translation.x, y: initialCenter.y + translation.y)
            piece.center = newCenter
        } else {
            // On cancellation, return the piece to its original location.
            piece.center = initialCenter
        }
        print("2--", myLabel.frame)
    }

    @objc func rotationGestureRecognizer(_ recognizer: UIRotationGestureRecognizer) {
        if let view = recognizer.view {
           view.transform = view.transform.rotated(by: recognizer.rotation)
            recognizer.rotation = 0.0
        }
    }

    @IBAction func changeTextAction(_ sender: Any) {
        myLabel.text = myTextView.text
        self.myTextView.resignFirstResponder()
    }

    @objc func pinchRecoginze(_ pinchGesture: UIPinchGestureRecognizer) {
        guard pinchGesture.view != nil else {return}
        let view = pinchGesture.view!
        if (pinchGesture.view is UILabel) {
            let textLabel = view as! UILabel
            if textLabel.text?.count == 0 {
                return
            }
            if pinchGesture.state == .began {
                let font = textLabel.font
                pointSize = font!.pointSize
                pinchGesture.scale = textLabel.font!.pointSize * 0.1
                print("pinch began")
            }
            if 2 <= pinchGesture.scale, pinchGesture.scale <= 30 {// upto 30
                textLabel.font = UIFont(name: textLabel.font!.fontName, size: pinchGesture.scale * 10)
            }
        }
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

You can get the point where the pinch gesture is taking place and zoom there :您可以获得捏合手势发生的点并在那里进行缩放:

 @objc func pinchRecoginze(_ pinchGesture: UIPinchGestureRecognizer) {

    guard pinchGesture.view != nil else {return}

    let view = pinchGesture.view!      
    let location = sender.location(in: view.superview)
    view.center = location
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM