The Meaning of "Withevent" in Swift, and Parameter Modifiers in General

resignFirstResponder in Swift

You could probably go with:

self.view.endEditing(true)

How to send touch events to nextResponder in Swift

The nextResponder method returns an optional UIResponder?, so you can simply call touchesBegan on the returned object:

self.nextResponder()?.touchesBegan(touches, withEvent: event)

Update: Swift 4.2

self.next?.touchesBegan(touches, with: event)

Method Explanation - Swift

This is just the name of this parameter from the call site. So when you call the method (which in this case you shouldn't ;)) you would do it like this:

 dataSource.tableView(myTableView, didSelectRowAtIndexPath: theIndexPath)

But within the method the parameter is accessed with the name indexPath.

touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event doesn't get called

When using cocos2d with touches you should use the following methods instead

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

touchesbegan/moved/ended not working with xcode 6.3

So I found the answer to my own question ... this was caused by a class I had already defined in my project called "Set". (that was from a tutorial from Ray W about making a candy crush game). Anyway, in Swift 1.2 they introduced their own class called "Set" and it was causing name collision problems. So I just renamed the old Set class and it all started working. UGH!

Swift 2.1: how to override touchesBegan & touchesMoved

I think the method signatures you are looking for are:

class XXX : UIResponder {

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

}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

}

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {

}
}

UIPanGestureRecognizer does not have a member named touchesBegan

You don't have to override the functions, you can just define them like below. But I am not exactly sure why that is. In a custom UIView these functions have to be overridden.

func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
println("here")
}

func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {

}

func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {

}

func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {

}

using touchesbegan and touchesmoved to distinguish flick and drag

Drag and Flick are usually distinguished by speed - one solution would be to create an algorithm based on the distance formula.

One rough example:

CGPoint pointOld = CGPointMake(0, 0); // Not sure if this is valid
CGPoint pointNew = CGPointMate(0, 0); // Just making holders for the
// location of the current and previous touches

float timeInterval = 0.2f;
// However long you think it will take to have enough of a difference in
// distance to distinguish a flick from a drag

float minFlickDist = 100.0f;
// Minimum distance traveled in timeInterval to be considered a flick

- (void)callMeEveryTimeInterval
{
// Distance formula
float distBtwnPoints = sqrt( (pointNew.x - pointOld.x) * (pointNew.x - pointOld.x) - (pointNew.y - pointOld.y) * (pointNew.y - pointOld.y) );
if (distBtwnPoints >= minFlickDist)
{
// Flick
} else {
// Drag
}
}

Really rough sketch of something I think might work - hope that helps.



Related Topics



Leave a reply



Submit