UIView perpetual linear animation with collision detection - ios

I'm trying to make a circular UIView animate in a particular direction until it collides with the borders of the view (which is full screen, so basically the borders of the device), at which point it will reflect off it and continue on its way infinitely. However, I'm not really sure how to pose my question, so I'm having trouble finding any information on it. I already have the view, the direction it will move in, and its velocity. I'm just not sure how to handle an animation like that.
Any advice is much appreciated! Thanks!

Use a CADisplayLink to continuously update the position of the view, synced with the refresh rate of the screen. The update method that that display link triggers will take into account the current x and y velocity of the view and update the frame based on it. If at ever point the x- or y-coordinate goes under or over a limit (0 or screen width/height), reverse the appropriate velocity value and recalculate the position.

Related

Swift/Xcode "Snake Game"

I am a student learning how to code in Xcode using swift. I am currently trying to make an app similar to the snake game that hopefully everyone knows.
I am currently on a windows computer so I can't show my code at the moment. I doubt you will need it for what I'm asking.
I am trying to make my Sprite Node (Just a while square) move with a swipe of the screen in the up, down, left, right fashion just like the original snake game. I have the Gesture Recognizer set up so it prints out what direction your swiping.
How can I make the sprite node move with a swipe? I want it to move infinitely in that direction until you either 1.) hit a will and you respawn/game over or 2.) swipe in a different direction.
I assume you add velocity to the node when you swipe so you just add the code into that swipe snippet but how do I make it apply ONLY to the "snake"?
You can use an SKAction to move the snakeHead toward the wall that corresponds to the direction you want to move. This can be done in the action parameter when you declare your gestures.
I suggest using .move(to: duration:) and to maintain a constant speed toward each wall you can use the distance, speed, time formula to calculate the duration for the .move(to: duration:) action.
Check out https://developer.apple.com/documentation/spritekit/skaction
https://developer.apple.com/documentation/spritekit/skaction/1417768-move

Falling objects in SpriteKit?

I have 20 different sprites. I essentially want a "flow" of them to be constantly falling from the top and disappearing when they reach the bottom. Essentially, imagine rain where each drop is a random sprite. I want each sprite to fall with a random rotation and each "drop" to be a random sprite from my selection of 20.
Can someone please point me in the right direction? I've never made a game before and this is my first time working with SpriteKit. I'm using Swift.
Make use of arc4random to get a random sprite from your selection of 20 sprites by adding those sprites to a group. arc4random will also be able to get a random rotation for you.
For them to fall down from the top to the bottom, you can apply -gravity to them or apply a -impulse.
To check whether they have reached the bottom, get the y value when they disappear off the bottom of the screen and use that as a comparision value and then use removeFromParent().
Alternatively the third method could be to use the movement of SKAction and simply adjust their Y coordinate. At the end of the completion block just use removeFromParent().

Rotation around center point with velocity - iOS

Hi fellow programmers!
What I'm trying to do:
I have an image placed in the center of the screen. When I put my finger on this image, and rotate my finger, the image should rotate as well. When i release the finger, i would like the image to decelerate for x period of time. (Let's say that it should decelerate slowly). Basically I'm trying to develop a Spin-the-bottle application, but I can't figure out the velocity part.
What I've tried:
I've used this to get the one finger rotation, and it works great so far, but it doesn't include the velocity part. And honestly, I have no idea, how to calculate the velocity.
What I'm asking of you:
A "simple" way of doing one-finger-rotation with velocity, of an UIImageView in a normal UIViewController. (Or just using this method to the rotation part, and somehow implement velocity.) Examples are appreciated!
If you have any questions, please comment!
Thanks in advance. :)

Putting hard bounds on an animating view in iOS

I've been exploring iOS animations and I'm trying to find out if there is a simple way to restrict animation movement to within a certain area. For example, lets say you are using a pan gesture recognizer to drag a UIView around the screen. Is there a simple way to enforce that the frame of the UIView does not move beyond a specified location?
The way i've currently been approaching it is to take the UIView, calculate the location of the edges, and within my handlePan method, simply return (ie don't adjust the center point) if the frame is touching the boundary. Is there a more elegant way to do this? Even if only along a single axis?
Thanks!
I actually am doing that in one project and basically well, I am using the "non elegant" way. I have a set of coordinates "boundaries" from which the view I am dragging should not pass. Although there better ways to do this than others. For instance for a smoother experience:
Compare the X axis independently for the Y axis. What I was doing in the beginning was to compare the X and the Y in the same if sentence.
Extra: Check this project to get some ideas.

CATransform3D rotation with Label as subview

I am having one issue about CATransform3D.
I have a view A, which contains a label B. B's layer position is the center of A. B is used to display notification messages. I want to rotate A by 180 degree, of course the text in B will be upside down. So I have to rotate B by 180 degree too. Everything seems straight forward and actually in the simulator it works fine.
But when I loaded on the device, A and B did rotate, but B's position was changed. Now I can only see half of B, the other half is out of A.
My guess is that, when rotating A, because B is related to A's coordinate system, which was flipped 180 degree, B's location is changed.
But I want to know if anybody has ideas how to fix this problem or if you guys have better ways to approach.
Thank you very much.
UPDATE
I still can't figure out why B's position gets shifted, but I came up with another method to implement the same animation. A little bit tricky.
The key is animation.autoreverse, we know that when you rotate the label's super view by Pi, the text in the label will be rotated upside down. So what I did was rotate the super view by Pi/2, keep the same duration and set autoreverse = YES, what it will do is that it will rotate the super view by Pi/2, then rotate it back to the initial state. The result turns out that the view isn't get rotated at all, but for the user's vision, it is rotated.
I've found that subLayers and subviews of views don't always respond well to doubled animations (esp. alpha and center changes). If you must (as it is a little more expensive than normal), use UIViewAnimationOptionAllowsAnimatedContent, which will force a redraw instead of a 'snapshot' that is animated.

Resources