关于Swift中的UITapGestrueRecognizer

时间:2016-03-02 05:40:02

标签: swift uitapgesturerecognizer

我已经创建了UIView的视图子类。然后,我添加了UITapGestureRecognizer,但是当我点击视图时,它不响应它。

代码:

    let tapGesture = UITapGestureRecognizer(target: self, action: "singleTapHandler:")

    self.maskView!.addGestureRecognizer(tapGesture)

选择器:

    func singleTapHandler(recognizer: UITapGestureRecognizer) {

    print("xxx")

    dismiss()
    }

3 个答案:

答案 0 :(得分:1)

确保您已启用用户互动:

self.maskView!.userInteractionEnabled = true

答案 1 :(得分:0)

确保视图顶部的maskView和框架不等于CGRectZero

答案 2 :(得分:-1)

检查您是否在视图中添加了UIGestureRecognizerDelegate,然后使用tapGesture.delegate = self

您的tapGesture应该与let tapGesture = UITapGestureRecognizer(target: self, action: "singleTapHandler")类似,不需要:

然后你的功能可以

func singleTapHandler(){
    //action
}

最后,请务必点按添加了点按手势的视图。