touchesBegan not called in UIView subclass

◇◆丶佛笑我妖孽 提交于 2019-12-04 02:59:46

问题


I know there already are some questions about this but I couldn't find a solution. There is a very simple code that works just fine on one of my projects but not on an another one:

Here is the code for subclass of 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()
    }
}

I'm sure the solution is quite simple but I can't find it.


回答1:


You need to make sure that userInteractionEnabled is enabled on all of the superviews of your view. If any of the superviews have this property set to false, the touch events won't be processed for them or any of their subviews.




回答2:


Maybe some of the superviews has the userInteractionEnabled set to "false"?



来源:https://stackoverflow.com/questions/30443177/touchesbegan-not-called-in-uiview-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!