How can I stop a UIView movement in Swift? - ios

I'm messing around with Swift for the first time and have a square on the screen. The square starts by moving to the right. When I tap on it I want it to go up. I don't want it to continue to the right at all, I want it to just go straight up. Unfortunately the physics and gravity make it curve to the right some more before it goes up. What can I do to completely stop the gravity pull and acceleration of my object, before setting the gravity to the new value?
This turns the object in the right direction, but I need it to come to an instant stop before doing this.
if(self.gravity.gravityDirection.dx == 1){
self.gravity.gravityDirection = CGVectorMake(-1.0, 0.0)
}

The way to stop an item being affected by a behavior is to remove the item from the behavior or remove the behavior from the animator. For example, here you might remove self from the existing gravity behavior.

Related

How to get correct screen coordinate from UITapGestureRecognizer while VoiceOver is on

I'm currently working on an interactive view that relies heavily on the user's touch location. I have found that there are a few ways to interact with the UITapGestureRecognizer while VoiceOver is on, but when I tap my point the values given are very wrong. I've looked elsewhere, but my use case is outside of the norm so there is not a lot to tell me what is going on. Has anyone experienced this before?
I am aware that I can change accessibilityTrait to UIAccessibilityTraitAllowsDirectInteraction which will give me the correct screen point when used, but I would like to know what is causing this issue at the very least for the sake of knowledge. To interact with the UITapGestureRecognizer I either double tap or do a 3D touch by pressing on hard on the screen. The ladder method doesn't work for the tap gesture but will work for the pan gesture.
This is the only line I use to get my screen points. My map view is a UIImageView
CGPoint screenPoint = [tapGesture locationInView:map];
I'm using a map of a building and I try to tap the same corner or landmark for my testing. I know I can't hit the same exact point every time, but I do use a stylus and I can get pretty close.
Without VoiceOver on I would get the result: (35.500, 154.363)
With VoiceOver on and tapping in generally the same spot, I get : (187.500, 197.682)
The point I am using to test is on the left side of the screen and the result from VoiceOver being on is in the middle of the screen. I believe the y-axis value may have changed because of my tool bar's size, but I have no idea what is throwing off the x-axis value. If more information is needed let me know.
UPDATE: Upon further investigation, it turns out that the UITapGestureRecognizer will always return (187.500, 197.682) no matter where I touch in the map view when VoiceOver is on. That point seems to be the middle of the map view. Oddly enough though, the UIPanGestureRecognizer will give me the correct (x,y) for my view if I use the 3D touch while VoiceOver is on.
On a side note not relating to the problem at hand, it seems if I use the accessibility trait UIAccessibilityTraitAllowsDirectInteraction the method UIAccessibilityConvertFrameToScreenCoordinates returns a frame that is higher than my view. It works fine if I do not change the trait.
Your problem may deal with the reference point used when VoiceOver is on.
Verify what your point coordinates are referring to : view or screen coordinates ?
I suggest you take a look at the following elements :
accessibilityFrame
accessibilityFrameInContainerSpace
UIAccessibilityConvertFrameToScreenCoordinates
According to your project, the previous elements may be interesting to get your purposes.

Custom UIGestureRecognizer conflicting with UITapGestureRecognizer

So I have this project that I took from somebody else and they have implemented this OneFingerRotationGestureRecognizer (https://github.com/melle/OneFingerRotationGestureDemo/blob/master/OneFingerRotationGestureDemo/OneFingerRotationGestureRecognizer.m) for a circular slider. Additionally, they have added a UITapGestureRecognizer on top of that, so you could tap a value within that circular slider and the value would jump to that specific one. Now the problem is, when I drag that thing just a very small amount (imagine putting your thumb onto the control and tilting left/right), then the UITapGestureRecognizer also fires! And this is a problem, because I want to be able to grab the circular slider wherever I want (there is no handle or something). And when I only drag it a little, then the value just jumps to that spot where I did that small dragging. Somehow I need to cancel that tap gesture as soon as that OneFingerRotationGestureRecognizer started registering touches. I tried what is described here: https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/coordinating_multiple_gesture_recognizers/preferring_one_gesture_over_another?language=objc but didn't have any success with that :-(.
What can I do? I'm afraid the solution is so simple that I just don't see it.

How to remove or destroy SKSpriteNode once it disappears from screen

I am making a Flappy Bird kind of a game using Swift 3. I spawn pipes with the help of a function 'makePipes()' which I'm calling every 3 seconds with the a timer. The 'makePipes()' function makes SKSpriteNode for the Pipes and moves them from right of the screen to left of the screen.
What I wanna do is, destroy the Node of the Pipe after it has vanished from the screen or after it has completed its movement from one side of the screen to other.
Another solution could be to Re-use the pipes once they have moved from right to left.
pipe1.run(movePipes) {
self.pipe1.removeFromParent()
}
I tried the above solution and it does not work. It shows weird blinking lines across the screen and pipes become weirdly and randomly placed instead of uniform.
Any help is appreciated. I'm very new to iOS.
You could have done something like this:-
movePipesForward = SKAction.moveByX(-dx, y: 0 ,duration: duration)
removePipes = SKAction.removeFromParent()
moveForwardAndRemove = SKAction.sequence([movePipesForward, removePipes])
pipe1.runAction(moveForwardAndRemove)
self.addChild(pipe)

Correct way to move your character node with Touch buttons?

I'm starting with the SpriteKit, and actually i want to move my Hero in a game by a buttons on screen.
But i want the hero move as user stay pressing the button, if use take your finger off the button the Hero stop moving.
At first i think about use the touchesBegan(and End) with booleans, when user tap set the boolean true and star an action that moves Hero if bolean is true, and when user take off button set to false.
i think this method is pretty bad, further take into consideration use sprite animations ( actually i have no idea how ir works)
i know this question isn`t very good but i search for something like this and i have found nothing too specific. Thx

Preventing a sprite from rotating

I am doing this spritekit game.
I have this object that I want to move up and down when the user touches it. It is an object that has to slide in a rail, up and down or in another case left and right.
So, when the user touches the object I do something like this
CGVector force = CGVectorMake(0.0f, 5000.0f);
[object.physicsBody applyForce:force];
it is a vertical force applied up.
but when the object collide with others it rotates. Is there a way to prevent the object from rotating?
Try object.physicsBody.allowsRotation = NO. This ignores all angular impulses and forces that would make the body rotate.

Resources