rotating a coordinate cross in SCNScene to mirror camera movements - ios

I am new to SceneKit and I could use your help with the following:
I have two SCNViews
- a large one (skView) showing the scene for the user which can be manipulated by the user with the standard allowsCameraControl option enabled
- another small one (coordinateCrossView) in the left corner which shows a coordinate cross that I would like to mirror any camera navigation the user performs in the large SCNView
I implemented it by making the ViewController which holds both SCNViews a SCNRendererDelegate and by updating the cross' orientation in the renderer: updateAtTime: method like so:
- (void)renderer:(id<SCNSceneRenderer>)renderer updateAtTime:(NSTimeInterval)time {
[self.coordinateCrossView.scene.rootNode childNodeWithName:#"cross" recursively:YES].orientation = self.skView.pointOfView.orientation;
[self.coordinateCrossView setNeedsDisplay];
}
So far so good. However, this only works when the user is manipulating the large SCNView with a finger on the screen. After as swipe gesture when the finger leaves the screen the object in the large SCNView continues to rotate a bit - but the coordinate cross is not updated.
Any ideas why this behavior is not captured by the pointOfView.orientation changes?

I found out what the problem was: the small coordinateCrossView was not continuously updated and therefore even though the coordinate cross' orientation was changed, the view didn't show it - even though setNeedsDisplay was called.
I fixed the issue by setting the isPlaying property of the coordinateCrossView which forces it to be updated.

Related

How to make two screens show simultaneously like in snapchat?

I am creating a camera roll feature similar to snapchat where the camera is the bottom layer and then after tapping a button the camera roll appears. The camera roll does not occupy the whole screen the search bar on the top is still maintained and upon dismissal with a swipe gesture down the camera is still running. I am not sure how this affect is achieved. Is it done by using a scroll view or a segue of some kind? Thank you for your help.
Below is the video in question
Snap Chat Camera Roll
Firstly, this type of effect cannot be achieved using the standard camera UIImagePickerController. You will need to create your own camera view.
Here is a good guide to get you started: https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Camera-View
You could also try using a custom library that can be easily customized such as: https://github.com/omergul/LLSimpleCamera/
Now, in terms of the actual visual, I do not believe there is any actual segue/change of viewcontroller involved. The camera view is probably always on screen (except perhaps when it is fully covered by the Memories screen), it is simply overlayed by other things.
The Memories screen is most likely 'presented' in a custom manner. The show/dismissal logic can be achieved by attaching a UIPanGestureRecognizer to the UIView and translating the view up and down on pan event. If the pan's y value passes a certain threshold up or down, it automatically continues its animation to show or hide the view.

How is the Instagram Stories cube transition done in iOS?

When swiping between stories in Instagrams new feature "Stories" (you know that cube-like transition when going from one story to another) I can't manage to understand how they do it!
First of all, if you dig deeper into the functionality you find that it works exactly like the UIPageViewControllers transition:
- It bounces when swiping fast from one view to another.
- You can pause the swipe in the middle of the transition by touching the screen.
The developing team couldn't have used a solution based on the more known workarounds out there, e.g:
https://www.appcoda.com/custom-view-controller-transitions-tutorial/
Because as far as I know my two statement above is not possible to achieve with anything else than the PageViewController.
This leaves me thinking that the Instagram Developer Team gained access to a new transition style for the PageViewController, also known as Cube-scroll, or is it a workaround that I'm not aware of?
Any ideas?
I took a shot at recreating this functionality a while back. You can check the source code on GitHub: https://github.com/oyvind-hauge/OHCubeView
I'm using a scroll view (with paging enabled) and, for each subview I'm manipulating these as a function of the given view's current x-offset in the scroll view. The actual animations are done on each subview's layer using Core Animation (more specifically, transforming an identity matrix, given by CATransform3DIdentity, using the method CATransform3DRotate).
The shadow effects are also applied to the subview's layers (view.layer.opacity), with the amount of shadow determined by how much of the view is showing on screen.
My implementation solves both of your concerns (bounces when swiping, can pause swipes). I'm sure this could have also been implemented using the a UIPageViewController, but I hate working with those.
I think you are overthinking the controller's part here. The effect can easily be achieved using a CATransformLayer and three-sided cube-like view structure, where there is one view which aligns with the screen plane, and two others rotated -90 and 90 degrees on their y axis. Then, getting a pan gesture to rotate the scene. After a successful 90 degree turn (in either direction), you can either quickly reset the scene (so that keeping on rotating appears as if continues, but actually the camera shifted back to initial position) or you can have a full 360 degree rotation, and just update previous and next "pages". A single controller can handle this scene. If you prefer to have each page as a controller, it is possible, you can still use one controller for the scene, and then use the page controllers as child controllers, and setting their views as described above.
See this article for more information on CATransformLayer. Their example already creates something that is quite close to what you need.

buttons appear on screen before SCNView

In storyboard, I have buttons attached to main view as siblings to SCNView(both buttons and SCNView are subviews of main view). At launch time, the buttons appear on screen before the SCNView does. This is quite unfortunate. Do you have any idea how to fix this?
//edit: added button programatically in viedDidLoad: as a subview of SCNView..Still the same problem.
SettingsButton and View appear before SceneView. This happens on iPhone 5,5c and lower and on iPads.(3 for sure, don't know for others)
Code for adding view programatically:
let uv = UIView(frame:myFrame)
uv.backgroundColor = UIColor.blackColor
sceneView.addSubview(uv)// scene view from storyboard
Thats it. How can iOS take 5-10s to load an app, but than rushes with adding views to the screen in an unpredictable order.. Couldn't it just take 0.2s more and present view properly? Really stupid problem
I imagine if you check via debugger breakpoints or logging in viewDidLoad, you'll find that the SCNView instance is indeed in the view hierarchy immediately.
What you're seeing is that said view's SceneKit content does not appear immediately. Depending on what you're doing with SceneKit, it may take some time to load content and prepare for rendering, and during that time the rest of the view hierarchy remains visible. That delay is probably unavoidable — you might be able to minimize it by optimizing asset formats, reducing the amount of stuff in your scene, etc. So it sounds like the key issue here is getting your app launch experience to look the way you want while accounting for the delay.
A good way to do that might be to show a splash image of some sort until SceneKit is ready to display. You could put a UIImageView in your storyboard, set up to obscure everything with a splash screen (possibly the same launch image iOS displays before your app takes over), then hide that image view (with a fade out even?) when SceneKit starts rendering.
To find out when SceneKit starts rendering, make one of your objects the scene renderer delegate and implement renderer:didRenderScene:atTime:. That method gets called on every frame, so you can use it to catch when the first frame has been rendered (and then ignore it for subsequent frames).

Scenekit orientation in IOS

I have imported a Collada file into my scene and I am using allowsCameraControl to rotate / pan the object / pan the object
I now need functionality to jump instantly to the front or side view of the object.
Does anybody know how I can get the camera that Scenekit implements in my scene and how to rotate this camera to show the front and back view ?
"Front view" and "side view" are notions that are defined in terms of your content, so you have to define them yourself.
Create a node (let's call it "frontViewNode" for example) with an attached SCNCamera and position/orient it so that it's looking at whatever you call the "front" of your object. See this answer if you need help with that.
Create another node ("sideViewNode") that's looking at whatever you call the "side" of your object.
When you want to switch views, set the pointOfView property on the SCNView displaying your scene to either your "frontViewNode" or your "sideViewNode". You can even do this inside an animation (see SCNTransaction) to make the camera view swoop around between the two viewpoints.

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