iOS Swift, cannot get pinch gesture to work
Asked Answered
G

5

5

i have a test project that takes text from a file, adds it to a textview and displays it. i want to add some gestures but cannot seem to make it work... here is the relevant code:

class ViewController2: UIViewController, UIGestureRecognizerDelegate {

@IBOutlet var textview1: UITextView!

var pinchGesture = UIPinchGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()

    self.textview1.userInteractionEnabled = true
    self.textview1.multipleTouchEnabled = true

    self.pinchGesture.delegate = self
    self.pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(ViewController2.pinchRecognized(_:)))
    self.view.addGestureRecognizer(self.pinchGesture)
}


@IBAction func pinchRecognized(pinch: UIPinchGestureRecognizer) {
    self.textview1.addGestureRecognizer(pinchGesture)
    self.textview1.transform = CGAffineTransformScale(self.textview1.transform, pinch.scale, pinch.scale)
    pinch.scale = 1.0
}

any ideas? followed several tutorials but none seem to help. code is tested on actual iPhone...

thanks a lot

Edit for Solution:

@IBAction func pinchRecognized(pinch: UIPinchGestureRecognizer) {
    var pinchScale = pinchGesture.scale
    pinchScale = round(pinchScale * 1000) / 1000.0
    if (pinchScale < 1) {
        self.textview1.font = UIFont(name: self.textview1.font!.fontName, size: self.textview1.font!.pointSize - pinchScale)
        pinchScale = pinchGesture.scale
    } else {
        self.textview1.font = UIFont(name: self.textview1.font!.fontName, size: self.textview1.font!.pointSize + pinchScale)
        pinchScale = pinchGesture.scale
    }
}

thanks to nishith Singh

Glycine answered 2/5, 2016 at 8:32 Comment(3)
Can you set a breakpoint, and see if the method is called?Theosophy
which one? the addGestureRecognizer?Glycine
the pinchRecognized oneTheosophy
A
8

Try adding the gesture recogniser to your textview in viewDidLoad instead of adding it in pinchRecognized. Currently you are adding the pinchGesture to your view which is behind your text view and hence will not receive the touch

var pinchGesture = UIPinchGestureRecognizer()

Use this code:

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.textview1.userInteractionEnabled = true
    self.textview1.multipleTouchEnabled = true
    
    self.pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(pinchRecognized(_:)))
    self.textview1.addGestureRecognizer(self.pinchGesture)

    // Do any additional setup after loading the view.
}

@IBAction func pinchRecognized(_ pinch: UIPinchGestureRecognizer) {
    let fontSize = self.textview1.font!.pointSize*(pinch.scale)/2
    if fontSize > 12 && fontSize < 32{
        textview1.font = UIFont(name: self.textview1.font!.fontName, size:fontSize)
    }
}

You might have to hit and trial with the minimum and maximum font sizes as you want, right now the minimum font size is 12 and the maximum font size is 32.

Apologia answered 2/5, 2016 at 8:44 Comment(5)
that seem to work. however, the text doesn't grow, just the windows itself. any ideas?Glycine
Try increasing the font of UITextView instead of transform refer this link #13669957Apologia
put this code in your pinchRecognized - textview1.font = UIFont(name: self.textview1.font!.fontName, size:self.textview1.font!.pointSize*pinch.scale)Apologia
this will increase the font of your textview instead of varying the transformApologia
ok so this worked. had to add a few stuff. will update first post. thank you so much for all the help!Glycine
A
2
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.pinchGesture))
func pinchGesture(sender: UIPinchGestureRecognizer){
sender.view?.transform = (sender.view?.transform)!.scaledBy(x:   sender.scale, y: sender.scale)
    sender.scale = 1
    print("pinch gesture")
}
Andonis answered 27/3, 2017 at 8:5 Comment(0)
W
0

You set the delegate of self.pinchGesture before initialising.

  • Initialise the self.pinchGesture first.
  • Set the delegate.
  • Add self.pinchGesture to self.view

    self.pinchGesture.delegate = self
    self.pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(ViewController2.pinchRecognized(_:)))
    self.view.addGestureRecognizer(self.pinchGesture)
    
Weitzel answered 2/5, 2016 at 8:44 Comment(0)
C
0
class ViewController2: UIViewController, UIGestureRecognizerDelegate {

@IBOutlet var textview1: UITextView!

var pinchGesture = UIPinchGestureRecognizer()

override func viewDidLoad() {
    super.viewDidLoad()

    self.textview1.userInteractionEnabled = true
    self.textview1.multipleTouchEnabled = true

    self.pinchGesture.delegate = self
    self.pinchGesture = UIPinchGestureRecognizer(target: self, action: "pinchRecognized:")
    self.view.addGestureRecognizer(self.pinchGesture)
}


func pinchRecognized(pinch: UIPinchGestureRecognizer) {
    self.textview1.addGestureRecognizer(pinchGesture)
    self.textview1.transform = CGAffineTransformScale(self.textview1.transform, pinch.scale, pinch.scale)
    pinch.scale = 1.0
}
Countertenor answered 2/5, 2016 at 8:44 Comment(1)
This won't call the function and will give the warning "No method declared with Objective-C selector 'pinchRecognized:'"Averyaveryl
V
0

Your code is actually working. But not the way you want probably.

Initially you assigned the gesture recogniser to the view of view controller.

But then inside the method you added same gesture recogniser to the UITextView.

So it should be working on UITextView. And the gesture recogniser is removed from the view controller's view. Gesture recogniser can only have one target. Pick view controller's view or textview.

Ventris answered 2/5, 2016 at 9:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.