4

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

Benny
  • 695
  • 2
  • 6
  • 19

5 Answers5

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.

Robin
  • 373
  • 1
  • 3
  • 20
nishith Singh
  • 2,968
  • 1
  • 15
  • 25
  • that seem to work. however, the text doesn't grow, just the windows itself. any ideas? – Benny May 02 '16 at 08:54
  • Try increasing the font of UITextView instead of transform refer this link http://stackoverflow.com/questions/13669457/ios-scaling-uitextview-with-pinching – nishith Singh May 02 '16 at 08:59
  • put this code in your pinchRecognized - textview1.font = UIFont(name: self.textview1.font!.fontName, size:self.textview1.font!.pointSize*pinch.scale) – nishith Singh May 02 '16 at 09:15
  • this will increase the font of your textview instead of varying the transform – nishith Singh May 02 '16 at 09:16
  • ok so this worked. had to add a few stuff. will update first post. thank you so much for all the help! – Benny May 02 '16 at 10:30
1
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")
}
Sai kumar Reddy
  • 1,751
  • 20
  • 23
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)
    
PinkeshGjr
  • 8,460
  • 5
  • 41
  • 56
pkc456
  • 8,350
  • 38
  • 53
  • 109
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
}
Jayesh Miruliya
  • 3,279
  • 2
  • 25
  • 22
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.

Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63