iOS Swift,无法获得捏合手势

时间:2016-05-02 08:32:06

标签: ios swift gestures

我有一个测试项目,它从文件中获取文本,将其添加到textview并显示它。 我想添加一些手势,但似乎无法使其工作...... 这是相关的代码:

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
}

任何想法?接下来是几个教程,但似乎都没有帮助。代码在实际的iPhone上测试......

非常感谢

编辑解决方案:

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

感谢nishith Singh

5 个答案:

答案 0 :(得分:5)

尝试在viewDidLoad中将手势识别器添加到textview,而不是在pinchRecognized中添加它,目前您正在将pinchGesture添加到文本视图后面的视图中,因此无法接收触摸

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

您可能需要根据需要使用最小和最大字体大小进行试用,现在最小字体大小为12,最大字体大小为32。

答案 1 :(得分:0)

在初始化之前设置self.pinchGesture的委托。

  • 首先初始化self.pinchGesture
  • 设置delegate
  • self.pinchGesture添加到self.view

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

答案 2 :(得分: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
}

答案 3 :(得分:0)

您的代码实际上正在运行。但不是你想要的方式。

最初,您已将手势识别器分配给视图控制器的视图。

但是在方法中你为UITextView添加了相同的手势识别器。

所以它应该在UITextView上工作。并且从视图控制器的视图中移除手势识别器。手势识别器只能有一个目标。选择视图控制器的视图或文本视图。

答案 4 :(得分:0)

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