How can I set my view to the whole frame? - ios

I am making a game in swift. I was wondering how I could fix the problem where the view of my scene does not match of with the Scene's frame.
To be more specific, I want to be able to make an edge physics body around the screen so that nothing can escape the outside of the screen. In GameScene I put self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame).
But this has the weird effect of covering a larger area than what I view at one time. If someone could help with this, I would be very appreciative. Thanks!

If you're using scale mode .AspectFill, maybe one part of your scene is outside seeable area of view controller then SKPhysicsBody(edgeLoopFromRect: self.frame) did not work as you expected.
try self.view.frame instead of self.frame (and maybe you will have to multiply with 2 if your device comes with rentina display)

I found the answer. Since I was loading the GameScene from a file, the size was off a little bit. I changed the size of the scene in GameScene.sks and my program works beautifully!

Related

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".

iOS app seems to think its width is half of what it really is

In my iOS swift app I have an instance where I am making a circle with SKShapeNode(circleOfRadius: 15) but it makes a skinny ellipse instead, the width is only half of what it should be to make a circle.
Also, when I try to put something in the middle of the screen by setting the position.x of the object to view.bounds.width/2 it puts it in the middle of the left half of the screen instead of the middle of the whole screen. When I try to put it on the far right of the screen by using position.x to view.bounds.width it then goes to the middle instead of the right bound of the screen.
Has anyone ever seen this and know what the issue is?
Check the size of the view which you are adding the SKShapeNode to. It may be that your scene was created to half of the screen size.
Theres a few things you can checkout here first.
i didnt add the view's view controller as a child view controller before loading the view. That fixed this issue for me.
Check the frame of the superview before you're adding the view as its subview, when i seen the frame isnt what i thoguht it was number one solved my problem
the last thing i can think of is add this code to layout subviews. Layout Subviews is called once all the on screen view's bounds and frames have been updated.
If all three doesnt work. Then smoewhere in your code your not doing something right. Displaying some code that you have wrote can dramatically help us help you. Good Speed my friend God Speed.

SKView in UIScrollView, SKNodes don't scroll

So I'm trying to let the user two-finger scroll a SKView that they can paint on. I'm using a PanGestureRecognizer that is set with a minimum number of fingers as 2. I have the selector it calls shift the offset of the UIScrollView to which the SKView has been added. It scrolls the SKView perfectly... the problem is any SKNodes that have been added to that view don't scroll with it. Since a picture is worth a thousand words hopeful these will help(Thanks for any help you can give! I'm sure there is a simple solution. I just haven't found it ^^; ):
Initial state
After Scrolling the view up
After Scrolling the view down
UPDATE:
I added a contentOffset variable to the "GameScene" that, moved all SKNodes to a "root" node and then have this in the update run root.position = self.contentOffset and this does in fact get the nodes scrolling as well... however, if I start off with a screen that looks like this:
If I then scroll up and down a bit and then add a new node by touching at the very top edge in the center then I get a screen that looks like this:
So as you can see when I scroll the nodes will "slide" a bit and this also shifts the way the touches are registered as well. Also, it doesn't seem to "slide" again after that.
It may help at this point to know that I add a physicsbody to each of the added SKNodes so that I can hit them with touches but set the dynamic = false so theoretically should only be moved if explicitly told to. So any ideas on this?

iOS - Positioning of a running animation on orientation change

I have applied a CABasicAnimation to a layer. The animation modifies the "position.y" layer property to create a gentle bouncing effect; it is set to autoreverse and repeat.
I use the From and To values to position the animation in the bottom right quadrant of the screen. It works quite nicely until I change the orientation of the device. The problem is the animation is positioned relative to the top of the screen. So when the orientation changes it is no longer positioned in the correct place.
The autoresizingMask for the View itself is correctly set in interface builder, but the animation doesn't seem to take any notice of that. I guess because the animation is assigning an absolute value to the layers position.
I'm a bit stumped, thanks in advance.
Can you place your animation in a seperate view so it is self contained and then adjust that view with the rotation?

UIView coordinate transforms on rotation during keyboard appearance

iPad app; I'm trying to resize my view when the keyboard appears. It amounts to calling this code at appropriate times:
CGRect adjustedFrame = self.frame;
adjustedFrame.size.height -= keyboardFrame.size.height;
[self setFrame:adjustedFrame];
Using this technique for a view contained in a uisplitview-based app works in all 4 orientations, but I've since discovered that a vanilla uiview-based app does not work.
What happens is that apparently the uisplitview is smart enough to convert the coordinates of its subviews (their frame) such that the origin is in the "viewer's top left" regardless of the orientation. However, a uiview is not able to correctly report these coordinates. Though the origin is reported as (0,0) in all orientations, the view's effective origin is always as if the ipad were upright.
What is weird about this is that the view correctly rotates and draws, but it always originates in the literal device top left. How can I get the view to correctly make its origin the "top left" to the viewer, not the device's fixed top left? What am I missing? Please, for something so trivial I've spent about 6 hours on this already with every brute force technique and research angle I could think of.
This is the original source which doesn't work in this case:
move up UIToolbar
OK, I don't know what the ACTUAL answer is to the original question, but I can say with certainty that one way to resolve the issue is to always ensure that you don't manipulate a viewController's view directly. Always wrap your view inside a container view inside the main "view", then have that container view adjust its position etc as needed. Works exactly as the splitview does, probably because in both cases now the view in question is a subview of the main "view". What a relief!

Resources