在 Xcode 6 中触摸位置和 touchesBegan [Obj-C --> Swift]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25474493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Touch Location and touchesBegan in Xcode 6 [Obj-C --> Swift]
提问by Arvin Guy
I am having trouble creating a function "touchesBegan" and then creating a UIPoint and UITouch constant or variable that holds an x and y coordinate. I have the exact code I want in Objective-C but I do not know what it's equivalent is in Swift. Here is the Objective-C code which I want to basically translate into Swift code... NOTE: This is a Single View Application, NOT a game... Thanks in advance.
我在创建一个函数“touchesBegan”,然后创建一个 UIPoint 和 UITouch 常量或保存 x 和 y 坐标的变量时遇到了问题。我在 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;
}
}
回答by MendyK
As of Swift 1.2, use this
从 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;
}
}
回答by idmean
Where's the problem?
问题出在哪里?
var touch = touches.anyObject() as! UITouch
var point = touch.locationInView(self.view)
if point.x < 160 {
var variableName = 10;
}
else{
var variableName = 20;
}
回答by Valerie
Swift 1.2 changed the syntax for touchesBegan. See the UIResponder Reference.
Swift 1.2 更改了 touchesBegan 的语法。请参阅UIResponder 参考。
func touchesBegan(_ touches: Set<NSObject>, withEvent event: UIEvent)
And don't forget to reference the super.
并且不要忘记引用超级。
super.touchesBegan(touches, withEvent: event)
Here is an edited example of implementing this code from techotopia.comwhich might give you more information on the other three touches functions.
以下是来自techotopia.com的实现此代码的编辑示例,它可能会为您提供有关其他三个触摸功能的更多信息。
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches.anyObject() as! UITouch
var point = touch.locationInView(self.view)
// Insert if statements
super.touchesBegan(touches, withEvent: event)
}