【问题标题】:touchesBegan not called in UIView subclass在 UIView 子类中未调用 touchesBegan
【发布时间】:2015-05-25 17:20:47
【问题描述】:

我知道已经有一些关于此的问题,但我找不到解决方案。 有一个非常简单的代码在我的一个项目上运行良好,但在另一个项目上却不行:

这是 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()
    }
}

我确定解决方案很简单,但我找不到。

【问题讨论】:

  • “不工作”是什么意思?
  • 我的意思是当我触摸视图时,没有 AlertView 出现
  • 尝试在函数中使用简单的 println() 来查看是否不是 alertview 的错误?
  • 其实我在函数中使用了断点,但应用程序一直没有停止
  • 您的视图的超级视图是否启用了userInteractionEnabled

标签: ios swift touchesbegan


【解决方案1】:

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

【讨论】:

  • 没有得到想要的结果,虽然我有 view->scrollview->view 布局,并且在两个视图中,我都将 userInteractionEnabled 设置为 true
【解决方案2】:

也许某些超级视图的 userInteractionEnabled 设置为“false”?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多