将现有的主动手势识别器传递给模态视图控制器

时间:2015-04-09 17:50:30

标签: ios uigesturerecognizer

我想通过长按启动模态视图,然后在取消长按时关闭模态视图。我该怎么做?

  • 我尝试将longPressRecognizer传递给模态视图和 将其设置为代表,但这不起作用。
  • 我尝试了一些更简单的东西 - 检测到touchesEnded,这意味着在模态视图中触摸结束,但也不会触发。

有没有办法告诉手势已开始的模态视图;我想让你认识到这个手势或触摸的结束/取消?


ViewController.swift

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    var buttonView:UIView!
    var longPressRecognizer:UILongPressGestureRecognizer!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
        self.longPressRecognizer.delegate = self
        self.view.addGestureRecognizer(self.longPressRecognizer)

        // Add a button
        buttonView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100))
        buttonView.userInteractionEnabled = true;
        buttonView.backgroundColor = UIColor.grayColor()
        self.view.addSubview(buttonView)
    }

    func longPressed(recognizer: UILongPressGestureRecognizer) {
        let point: CGPoint = recognizer.locationInView(self.view)
        if let pressedView = self.view.hitTest(point, withEvent: nil) {
            if pressedView == self.buttonView {
                switch recognizer.state {
                case .Began:
                    NSLog("long pressed - Began")
                    var mediaViewController = MediaViewController()
                    self.presentViewController(mediaViewController, animated: false, completion: nil)
                case .Cancelled:
                    NSLog("long pressed - Cancelled")
                case .Ended:
                    NSLog("long pressed - Ended")
                default:
                    break
                }
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

MediaViewController.swift

import UIKit

class MediaViewController: UIViewController, UIGestureRecognizerDelegate {
    var longPressRecognizer:UILongPressGestureRecognizer!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.lightGrayColor()

        self.longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
        self.longPressRecognizer.delegate = self
        self.view.addGestureRecognizer(self.longPressRecognizer)
    }

    func longPressed(recognizer: UILongPressGestureRecognizer) {
        switch recognizer.state {
        case .Began:
            NSLog("long pressed - Began")
        case .Cancelled:
            NSLog("long pressed - Cancelled")
        case .Ended:
            NSLog("long pressed - Ended")
            self.dismissViewControllerAnimated(false, completion: nil)
        default:
            break
        }
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        NSLog("touches began")
    }

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        NSLog("touches ended")
    }

}

1 个答案:

答案 0 :(得分:5)

嗯,这似乎可以在长按时同时处理轻击手势。还不错!


<强> ViewController.swift

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {
    var buttonView:UIView!
    var longPressRecognizer:UILongPressGestureRecognizer?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // Add a button
        buttonView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 100))
        buttonView.userInteractionEnabled = true;
        buttonView.backgroundColor = UIColor.grayColor()
        self.view.addSubview(buttonView)
    }

    override func viewDidAppear(animated: Bool) {
        if let recognizer  = self.longPressRecognizer {
            // Reuse existing recognizer
            self.longPressRecognizer = recognizer
            recognizer.removeTarget(nil, action: nil)
            recognizer.addTarget(self, action: "longPressed:")
            recognizer.delegate = self
            self.view.addGestureRecognizer(recognizer)
        } else {
            // Create a new new recognizer
            self.longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
            self.longPressRecognizer!.delegate = self
            self.view.addGestureRecognizer(self.longPressRecognizer!)
        }
    }

    override func viewDidDisappear(animated: Bool) {
        // Remove the recognizer
        if let recognizer = self.longPressRecognizer {
            self.view.removeGestureRecognizer(recognizer)
        }
    }

    func longPressed(recognizer: UILongPressGestureRecognizer) {
        let point: CGPoint = recognizer.locationInView(self.view)
        if let pressedView = self.view.hitTest(point, withEvent: nil) {
            if pressedView == self.buttonView {
                switch recognizer.state {
                case .Began:
                    NSLog("ViewController: long pressed - Began")
                    var mediaViewController = MediaViewController()
                    mediaViewController.addRecognizer(recognizer)
                    self.presentViewController(mediaViewController, animated: false, completion: nil)
                case .Cancelled:
                    NSLog("ViewController: long pressed - Cancelled")
                case .Ended:
                    NSLog("ViewController: long pressed - Ended")
                case .Changed:
                    NSLog("ViewController: long pressed - Changed")
                default:
                    break
                }
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

<强> MediaViewController.swift

class MediaViewController: UIViewController, UIGestureRecognizerDelegate {
    var longPressRecognizer: UILongPressGestureRecognizer!
    var tapRecognizer: UITapGestureRecognizer!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.lightGrayColor()

        self.tapRecognizer = UITapGestureRecognizer(target: self, action: "tap:")
        self.tapRecognizer.cancelsTouchesInView = false
        self.tapRecognizer.delegate = self
        self.view.addGestureRecognizer(self.tapRecognizer)

        self.longPressRecognizer.removeTarget(nil, action: nil)
        self.longPressRecognizer.addTarget(self, action: "longPressed:")
        self.longPressRecognizer.delegate = self
        self.view.addGestureRecognizer(self.longPressRecognizer)
    }

    override func viewWillDisappear(animated: Bool) {
        // Remove gesture recognizers
        self.view.removeGestureRecognizer(self.longPressRecognizer)
        self.longPressRecognizer.delegate = nil
        self.view.removeGestureRecognizer(self.tapRecognizer)
        self.tapRecognizer.delegate = nil
    }

    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true;
    }

    func longPressed(recognizer: UILongPressGestureRecognizer) {
        switch recognizer.state {
        case .Began:
            NSLog("MediaViewController: long pressed - Began")
        case .Cancelled:
            NSLog("MediaViewController: long pressed - Cancelled")
        case .Ended:
            NSLog("MediaViewController: long pressed - Ended")
            self.dismissViewControllerAnimated(false, completion: nil)
        case .Changed:
            NSLog("MediaViewController: long pressed - Changed")
        default:
            break
        }
    }

    func tap(recongizer: UITapGestureRecognizer) {
        if (self.view.backgroundColor == UIColor.lightGrayColor()) {
            self.view.backgroundColor = UIColor.greenColor()
        } else {
            self.view.backgroundColor = UIColor.lightGrayColor()
        }
    }

    func addRecognizer(recognizer: UILongPressGestureRecognizer) {
        self.longPressRecognizer = recognizer
    }

}