Translating UIViews when orientation changes - SWIFT - ios

I am building a simple Breakout game (game with a paddle, ball, and bricks and you have to break all the bricks) for Stanford's 193p course. Right now, I have a UIView (that serves as the paddle) added as a subview to an another UIView that serves as the game's view. I did not add any UIViews in storyboard except the game view - everything else is in code.
This paddle uiview is added to a dynamic animator.
It is added when the orientation of the iPhone is vertical.
But, when I rotate the phone, the Paddle UIView repositions to another location and cannot be seen.
What is the best way to and how do I translate this view so that it is in the exact same position in respect to where to was in the vertical orientation to the horizontal orientation?
I do not want it to be removed from the dynamic animator so I do not want to remove the paddle, then find the new location and then add it back.

Related

UICollisionBehavior collide with background view only

In my ViewController, I have a background view and a bunch of collision views. I have added gravity behavior to those collision views, so that they will fall from their original positions. But I want to let these views stop falling when they reach the bottom of the screen. So I add collision behavior to each collision view. Meanwhile I don't want them to collide with each other. In another word, each collision view can overlap with other collision views. I have tried
[_collision setTranslatesReferenceBoundsIntoBoundaryWithInsets:NO]
But it does't work, and I guess I'm using the wrong method. So how can I achieve this?

How to make view follow my image?

Im making a scene for my game. Right now i`m working on the view for IPhone 5-5c-5s the orientation is in landscape. But the view was not big enough for my scene, so i made the simulated size to freeform so i could choose the width for the scene. (Im using a normal Viewcontroller).
In the game you are supposed to move an image to another image without colliding with obstacles. The users playing the app are going to move an image from one side of the screen all the way over to the other side to the other image to win.
But when my image moves, the view doesnt follow with my image. How do you do that?
I would be so thankful for any answear! Thanks.
I am assuming you are using SpriteKit yeah? The UIView size is not related to the scene size. So changing those will not make your scene any different.
To do what you are describing, you need to read the Apple's Official Documentation on SpriteKit.
https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Actions/Actions.html
Scroll down to Example: Centering the Scene on a Node
Basically Apple tells you to add a dummy SKNode to put all your world sprites into the scene, and then add another SKNode as "camera" into the dummy node so you can move it around.
If you are targetting only iOS9 and above, you can use SKCameraNode and set:
SKCameraNode *cameraNode = [SKCameraNode new];
cameraNode.name = #"camNode";
self.camera = cameraNode;
Then you add your "character" (that is to be moved together with the camera) into the cameraNode. Then any movement, you need to move the cameraNode instead of the "character".

Scroll in spritekit game

I am building a spritekit game with 2 screens. Inside the 1st screen the player should pick one Hangar out of 6-7, by horizontal scrolling. When one picked a new SKScene will appear with the actual game play. For scrolling - One Hangar should be centered, while two others are partly shown from the sides.
Can it be done with UIScrollView, on top of SKScene? Or better use sprite nodes for it?
I am just not sure about the best way to handle user interface with sprite kit.
I would implement this by putting the hangars as children of a SKNode. Swiping would move this this SKNode move around with all it's children.
If you wanted the positioning you described; when the swiping has stopped I would use an SKAction to center a the hangar closest to the middle of the screen.
I would do it like this because I think you should only mix in UIKit when necessary because :
It is easier to port to OSX
You don't have to convert between different types of coordinate systems

Some help regarding UIScrollView and changing ownership of a UIView

Basically, I'm working on an app on the iPad that involves a line which the user can scroll horizontally through on the screen. It's set up so that there is a UIScrollView and a UIView on top that uses drawRect. I also have a UIView that's basically a ball which I can move around via touch, and also detects when it is within the UIScrollView and when it is on the line that is painted in the UIView.
Here's my main problem, though. Both the UIScrollView and the ball are subviews to the same view controller, and I want to make it so that, when the ball is dragged into the UIScrollView, that it will scroll along with it and even disappear if need be. How should I do this? should I just make the ball a subview of the UIScrollView when this happens, or should I do it some other way?
Use a container view. Put the ball in the transparent container subview and that is the view that moves around ( taking the ball with it). When you want the scroll view or line to move with the ball, insert them into the container at position 0 (under the ball).

UITableView portion loses interaction after device rotation

I have a UIViewController with a UIView subclass as a subview, and in here is a UITableView added as a subview of that UIView, as follows:
UIViewController
-UIView
--UITableView (plucked from a UITableViewController)
On an iPad, if I load the view controller in either orientation I can interact with the table view perfectly. However, if I rotate the device to a new orientation - I have code that alters the frames to make it fit the new resolution, but a small area of the table does not respond to touches and whatnot.
eg, we rotate portrait to landscape: if you imagine a portrait shape placed over a landscape screen - this area remains responsive, but a small strip to the right hand side does not respond.
Does anyone have any ideas what I may have missed? Thanks in advance.
EDIT: I have successfully verified the CGRect frames of all objects from the tableview cells through to the top UIViewController.
The area that looses its interaction must have fallen outside its superview's frame after rotation.
you can check that by setting tableView.clipSubviews = YES;
using this, will crop the tableview portion falling outside the superview.

Resources