手势识别器没有响应

时间:2017-02-06 21:26:45

标签: ios swift swift3 uitapgesturerecognizer

我的视图控制器上有一个按钮,它带有一个菜单和一个稍微透明的视图blackView - 菜单类包括一个轻击手势识别器,可以在点击blackView时关闭菜单。然而,它没有回应,我不知道我的问题在哪里。这是菜单的代码:

class MenuController: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

let blackView = UIView()

let collectionView: UICollectionView = {

    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = UIColor.white
    return cv
}()

let cellId = "cellId"


func presentMenu() {

    if let window = UIApplication.shared.keyWindow {

        blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
        blackView.isUserInteractionEnabled = true
        // Tap outside to dismiss
        blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))

        window.addSubview(blackView)
        window.addSubview(collectionView)

        let height: CGFloat = 200
        let y = window.frame.height - height
        collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)

        blackView.frame = window.frame
        blackView.alpha = 0

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            self.blackView.alpha = 1

            self.collectionView.frame = CGRect(x: 0, y: y, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

            }, completion: nil)
    }
}


func handleDismiss() {
    print("working")

    UIView.animate(withDuration: 0.5) {
        self.blackView.alpha = 0
        if let window = UIApplication.shared.keyWindow {

            self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

        }
    }
}


// Collection view data sources
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
    return cell
}



override init() {
    super.init()

    collectionView.dataSource = self
    collectionView.delegate = self

    collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)
}



}
调用

presentMenu,当我点击视图控制器上的必要按钮时,会出现菜单。但是handleDismiss没有被调用。感谢任何帮助,希望这只是一个初学者的错误!

解决方案后编辑:我在视图控制器中初始化menuController(),如下所示:

func showMenu() {
    let menuController = MenuController()

    menuController.presentMenu()

}

将其改为@ ronatory的

解决方案
lazy var menuController: MenuController = {
  let menuController = MenuController()
  return menuController
}()

func showMenu() {
  menuController.presentMenu()
}

解决了它。

2 个答案:

答案 0 :(得分:2)

所以我发现的唯一方法是更改​​menuController的初始化。而不是在showMenu()方法中初始化它(问题是你在方法中初始化它并且在执行方法之后menuController是nil):

func showMenu() {
    let menuController = MenuController()
    menuController.presentMenu()
}

使用延迟存储属性替换它(在执行方法后也可以访问实例),也不需要设置blackView.isUserInteractionEnabled = true。你可以删除它:

lazy var menuController: MenuController = {
  let menuController = MenuController()
  return menuController
}()

func showMenu() {
  menuController.presentMenu()
}

这就是全部。在此更改后,您的点击手势识别器应该工作。我用你的项目测试了它。如果您将项目的这一部分作为代码添加到问题中会很好,这样其他人就可以更好地理解问题而无需下载项目

答案 1 :(得分:1)

所以,它花了我一点,但现在是全班(工作和所有):)快乐编码!

import UIKit


class ViewController: UIViewController {

    let menuController = MenuController()


    // ******************    // ******************    // ******************    // ******************    // ******************


    func showMenu() {



        menuController.presentMenu()
        let tap = UITapGestureRecognizer(target: self, action: #selector(MenuController.handleDismiss))

        MenuController.blackView.addGestureRecognizer(tap)
      self.view.addSubview(MenuController.blackView)


    }

    // ******************    // ******************    // ******************    // ******************    // ******************


    func handleDismiss() {
        menuController.handleDismiss()
        if MenuController.blackView.alpha != 0 {

        }
    }




}


class MenuController: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    static let blackView = UIView()



    func presentMenu() {

        if let window = UIApplication.shared.keyWindow {

            MenuController.blackView.backgroundColor = UIColor(white: 0, alpha: .5)
            MenuController.blackView.isUserInteractionEnabled = true
            // Tap outside to dismiss


            window.addSubview(MenuController.blackView)

            let height: CGFloat = 200
            let y = window.frame.height - height
            collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)

            MenuController.blackView.frame = window.frame
            MenuController.blackView.alpha = 0

            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                MenuController.blackView.alpha = 1

                self.collectionView.frame = CGRect(x: 0, y: y, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

                }, completion: nil)
        }
    }


    func handleDismiss() {
        print("working")

        UIView.animate(withDuration: 0.5) {
            MenuController.blackView.alpha = 0
            if let window = UIApplication.shared.keyWindow {

                self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)

            }
        }
    }



}