添加手势识别器

时间:2018-10-23 14:07:36

标签: swift xcode swift2

由于某种原因,手势识别器不起作用,因此也无法使背景正常。如何解决它,以便选择器(self.dissmissMenu)起作用?

在View控制器中:

let menu = Menu()
@IBAction func menuButton(sender: AnyObject) {   
     menu.runMenu()  
}

在菜单中:

import UIKit

class Menu: NSObject {
    let dimming = UIView()

    public func runMenu(){
        if let window = UIApplication.sharedApplication().keyWindow{
            dimming.frame = window.frame
            dimming.backgroundColor = UIColor(white: 0, alpha: 0.5)
            dimming.addGestureRecognizer(UITapGestureRecognizer(target: self, action: Selector(self.dissmissMenu())))

            window.addSubview(dimming)

            UIView.animateWithDuration(0.5, animations: {
                self.dimming.alpha = 1
            })
        }
    }

    public func dissmissMenu(){
        UIView.animateWithDuration(0.5, animations: {
            self.dimming.alpha = 0
        })
    }
}

1 个答案:

答案 0 :(得分:1)

尝试像这样重写对addGestureRecognizer的呼叫:

dimming.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dissmissMenu())))