swift2 添加手势识别器

rur96b6h  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(170)

由于某些原因,手势识别器不工作,它不能使背景再次正常。我如何修复它,使选择器(self.dissmissMenu)工作?
在视图控制器中:

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

pokxtpni1#

尝试将对addGestureRecognizer的调用重写为如下所示:

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

相关问题