在Xcode 6中触摸位置和触摸开始[Obj-C - > Swift]

-4 xcode objective-c touchesbegan ios swift

我在创建函数"touchesBegan"时遇到问题,然后创建一个包含x和y坐标的UIPoint和UITouch常量或变量.我在Objective-C中有我想要的确切代码,但我不知道它在Swift中的含义是什么.这是我想要基本上转换成Swift代码的Objective-C代码...注意:这是一个单视图应用程序,而不是游戏...提前感谢.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];

    if (point.x < 160) {
        var = 10;
    }
    else{
        var = 20;
    }

}
Run Code Online (Sandbox Code Playgroud)

Men*_*dyK 13

从Swift 1.2开始,使用它

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

    var touch = touches.first as! UITouch
    var point = touch.locationInView(self)

    if point.x < 160 {
        var = 10;
    }
    else{
    var = 20;
    }
}
Run Code Online (Sandbox Code Playgroud)


idm*_*ean 5

哪里出了问题?

var touch = touches.anyObject() as! UITouch
var point = touch.locationInView(self.view)

if point.x < 160 {
    var variableName = 10;
}
else{
    var variableName = 20;
}
Run Code Online (Sandbox Code Playgroud)