Search code examples
iosobjective-cxcodeswifttouchesbegan

Touch Location and touchesBegan in Xcode 6 [Obj-C --> Swift]


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.

-(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;
    }

}

Solution

  • 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;
    }