touchesBegan没有在UIView子类中调用

La *_*sse 9 touchesbegan ios swift

我知道已经有一些问题,但我找不到解决方案.有一个非常简单的代码可以在我的一个项目上正常工作,但不能在另一个项目上工作:

这是UIView的子类的代码.

import UIKit

class test : UIView {

    init(frame: CGRect, color: UIColor) {
        super.init(frame: frame)
        self.backgroundColor = color
        self.userInteractionEnabled = true
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        super.touchesBegan(touches, withEvent: event)
        var alert = UIAlertView()
        alert.title = "lol"
        alert.message = "lol"
        alert.addButtonWithTitle("Ok")
        alert.show()
    }
}
Run Code Online (Sandbox Code Playgroud)

我确信解决方案很简单,但我找不到它.

Mic*_*lum 13

您需要确保userInteractionEnabled在视图的所有超级视图中启用了该功能.如果任何超级视图将此属性设置为false,则不会为他们或他们的任何子视图处理触摸事件.

  • 没有得到想要的结果,虽然我有view-> scrollview->视图布局,并且在两个视图中,我已将userInteractionEnabled设置为true (5认同)