I have a SKSpriteNode and I need to detect how long it is pressed. Is there any possible way to do this?
Asked
Active
Viewed 66 times
1 Answers
1
There is an easy way to do this:
//Declare a new NSTimeInterval in interface
@property (nonatomic) NSTimeInterval touchLength;
In touchesBegan:
self.touchLength = 0; //sets/starts timer
In Update method:
self.touchLength+= timeSinceLast;
In touchesEnded:
//Access touch length via self.touchLength
When you initially touch the screen, the timer is set to 0. In the update method, the "touch timer" is updated. When you release the touch, you have access to the touch timer which is effectively how long you have touched the screen.
I allow the timer to keep updating as it is set to 0 before each use anyway.

meisenman
- 1,818
- 1
- 15
- 25
-
How do I get the timeSinceLast? – zbz.lvlv Nov 05 '14 at 02:09
-
timeSinceLast is the time since the last update. RayWenderlich has a great beginner tutorial that shows how to properly use the update method. http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners The code is found in the Moving Monsters section. Basically, every frame or so the update method runs and passes in the time since the last update. – meisenman Nov 05 '14 at 03:01