SpriteKit Objective-C – Detect how much finger has moved in -touchesMoved - ios

I'd simply like to know if there is a way to detect how many pixels the finger has moved during the -touchesMoved function?
EDIT:
This is what I've tried. I made two instance variables called _previousPosition and _currentPosition. In -touchesBegan, I set them both to be the current finger location in the scene. In -touchesMoved, I set _currentPosition to be the current finger location once again. Keep in mind that during -touchesMoved, when I'm updating _currentPosition, _currentPosition is being constantly updated, while _previousPosition is not. Finally, in touchesEnded, I create another variable (not global, but private) called pixelsMoved, and set that equal to _currentPosition - _previousPosition. Right after that, in -touchesEnded, I reset _previousLocation to be the current finger location. It's all very complicated, so I'm almost positive I've made some mistake somewhere. Any help would be appreciated.

I'd simply like to know if there is a way to detect how many pixels the finger has moved during the -touchesMoved function?
-touchesMoved:withEvent: provides an event, and from the event you can get individual touch objects, each of which have an associated location that you get with -[UITouch locationInView:]. You don't get information about how far the touch has moved since the last time you looked, but you can keep track of the location of each touch and do the comparison yourself.

Related

SpriteKit prevent more than one touch at a time

I am making a SpriteKit game I was wondering if there was a way to prevent more than one touch at a time.
in my game an object gets added at every touch and I kinda don't want that. (even though its hilarious) if there is a way how do I do it? what would I use? and could you point me in the right direction? and I know that there are ways to do it as I have seen multiple games with that feature.
would I put something into 'appdelegate.swift' to prevent that or would it have something to do with the 'touches began' function I have tried several methods but none seem to work also I have searched all over google but to no avail.
if somebody could help me with this I would appreciate it but its not really that important as it doesn't upset the balance of the game at all.
You can use multipleTouchEnabled property of a UIView:
When set to YES, the view receives all touches associated with a
multi-touch sequence and starting within the view's bounds. When set
to NO, the view receives only the first touch event in a multi-touch
sequence that start within the view's bounds. The default value of
this property is NO.
Use it like this self.view.multipleTouchEnabled = false, where self is a scene.

How to terminate a SCNTransaction ongoing?

I use SCNTransaction to move game objects. More specifically, when the player taps somewhere on the screen, the object will move towards that destination. But sometimes the player may make a wrong move, so I want to create a button which can terminate all SCNTransactions.
However, unlike SKAction, which can be terminated with a simple line - self.removeAllActions(), SCNTransaction cannot be terminated or even paused from the outside according to the Apple Developer Documentation. Even worse, I find that before the object reaches its destination, its position has already changed to the destination's position, so I cannot simply use another SCNTransaction to counteract the ongoing one after knowing the object's current position.
Can anybody give me some hints? Thanks a lot.
SCNTransaction and its animation principles follow the one of Core Animation and CATransaction. To stop an animation you will have to set the model value to the current presentation value. For instance:
node.position = node.presentation.position
But if you are familiar with SKAction and would like to implement the same logic in your SceneKit app, you might want to have a look at SCNAction. They work identically.

Recognize long press in swift without UILongPressGestureRecognizer

I'm building an app around gesture recognition.
I've already built my code with recognition of taps, swipes (even with multiples fingers), pinches.
Now I'd like to recognize long press gesture without using UILongPressGestureRecognizer because it enters in conflit with my recognition of other gesture after (I tried).
What I'm currently doing is that I get the time in touchesBegan, in touchesMoved i calculate the time difference, and if it's greater than 400ms (for exemple), i call a function.
The thing is that this function is only called when the finger moved a bit and not when it's perfectly static.
Another option is to set a kind of delay in the touchesBegan and check if the finger is still on the screen after 400ms and then call the function.
How could I do that without blocking the rest of the gesture recognition ?
The aim of this long press would be to do a variation of intensity of a light or something like that (from 0 to 1s, light increase until max is reach, and then lower until minimum etc).
Next, I'll try to recognize a rotation gesture (with only one finger), so if you also have an answer for this, that'd be perfect.
Thanks !
Do not set delay. Start a timer that will fire after 400ms. In touchesEnded invalidate the timer, in case it was called before 400ms. When the timer fires, call the desired function.
As to your second question, probably you will need to calculate the trajectory of the points in touchesMoved method. If somehow the moves resemble rotation (you will need some kind of threshold for that), call the appropriate function.

Game in Swift 2 - "touchesBegan"?

I want to build a game that when I touch and drag, in the place of the touch it creates a line that goes in the same direction as the drag, and ends in the boundaries of the game (in this case my frame).
Any tips of how could I approach this problem?
Your search term in UIPanGestureRecognizer.
You'd create a pan gesture recognizer and attach it to your view. In the method that it calls, look for the state UIGestureRecognizerStateBegan. Record the start position, then respond to calls to your action method with the state UIGestureRecognizerStateChanged.
That should be enough to get you started. As Matt says, this is a fairly common use case, so you should be able to find sample code online if you can't work this out from the docs.

Move a Sprite Kit Physics Body to where the user touched without going through

I have a sprite kit physics body that I want to be controlled by touching by moving it towards the location where the user touched in touchesBegan and touchesMoved. I have tried the SKAction move and let's just saw it didn't suit my purposes. I have tried applyImpulse and it suited my purpose, but each time it overshot cause the velocity cause it to go through the location every time, and if I made new locations the object went flying around back and forth. I would like it to follow the current touch somewhat fast, and not slowly crawl toward the user's finger. Is there an easy way to make the object follow the touch when the user is touching and stop moving when the user lets go that will go well with the psrite kit physics engine? I want the object to bounce other objects off it but not bounce itself.
Add a few things:
a Boolean flag to set whether or not the Sprite has been assigned a new location
a CGPoint storing the new desired location
a method that checks if the location of the sprite is close enough to the CGPoint to be considered as arrived
When the touch is issued, store the point and set the Boolean to YES then initiate the force on the sprite.
Inside the update method put a call to that checker method that is called if the Boolean you made is YES.
If that check returns true, set the velocity to zero (or otherwise stop the sprite how you see fit) and set the Boolean back to NO so that check method stops firing.

Resources