" touchesBegan"出错

时间:2015-09-23 11:09:04

标签: ios swift swift2 uitouch cgpoint

我的代码:

var location = CGPoint(x:0,y:0) 
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch = touches.anyObject() as? UITouch 
    location = touch.locationInView(self.view) 
    Button.center = location 
}

在这一行上出错:

let touch = touches.anyObject() as? UITouch 

如何解决?

2 个答案:

答案 0 :(得分:0)

Try this : - 

  override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch in touches {
    if let touch = touches.first as? UITouch {
                           if touch.tapCount == 2{
                                //Do Something here
                            }
                                             }
                          }
        }

答案 1 :(得分:0)

试试这个

if let touch = touches.first as? UITouch
{
}