0

我刚刚开始学习 Swift,我一直在尝试移动 UIView 以响应触摸。我收到一个exc_bad_instruction错误。

var location = CGPoint(x: 0, y:0)
@IBOutlet weak var person: UIView!

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch : UITouch! = touches.first

    location = touch.location(in: self.view)

    person.center = location
}
4

2 回答 2

0

exc_bad_instruction通常是空指针的提示。确保Person不为零。

还要确保将您的属性命名为以小写字母开头 - 在本例中为person. 使用时,Person您冒着调用 Type 而不是 Property 的风险。

如果您需要进一步的帮助,请编辑您的帖子并添加您项目的更多代码示例 - 特别是 Class和您使用它Person的整个地方。UIViewController

于 2016-10-01T07:08:46.487 回答
0

变量 location 应该明确声明为 CGPoint:

var location : CGPoint = CGPoint(x:0, y:0)
于 2016-10-01T09:57:22.820 回答