SwiftUI: Preload View without displaying - uiview

I'd like to "preload" a view (including a SKScene) to gather the bounds of the display area of this scene.
The size information is needed to precompute additional data, the computation should be done in the background while displaying the home screen (different to the screen containing the SKScene).
Q: It it possible to preload a view and if so, how?
Thanks,
J.

OK, found a solution. I switch to the screen directly after application start, gather the needed information and immediately switch to the home screen.
I use the method onAppear() and a global variable as a flag to avoid a second automatic return to the home screen.

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.

iOS: How do I know if user left the current screen?

I want to build some analytics into my app and I would like to send some data when user leaves current screen, though there are multiple ways he can do so (back button, other button, sidebar menu, etc). Is there any efficient way to do this? I really don't feel like implementing it to every possible button that can lead the user to different screen.
You should call your function inside viewWillDisappear, which is called every time the current view controller is about to disappear from screen. See the documentation of viewWillDisappear
Also see the view controller life cycle (thanks #Paolo for the tip) below (documentation).

Xcode Display a loading view until the second view loads (on a segue show)

I have an app that switch views using a segue when a button is clicked.
The second view loads data from the internet and it can take a couple of seconds.
I would like to know how can i display a loading view/splash screen in the meantime so the view could finish the loading and the app wont appear like it's doing nothing.
Thanks!
Check this library SwiftSpinner. It serves the purpose of your needs. It's really brilliant.
Call the necessary function from the library in the viewDidLoad method of your ViewController which loads the data from the internet. Remove this view in DidFinishLoading method of the NSURLProtocol (It's an optional func declared in that class which detects when the request to that URL is complete). The documentation is given in that library itself.
Sounds like you're looking for an activity indicator. I've used the custom class posted https://stackoverflow.com/a/32661590/3516923 with success. Just a note of warning, in his class he blocks all input while the indicator is in view. If you want to make it so your users can back out before things finish you need to remove UIApplication.sharedApplication().beginIgnoringInteractionEvents() and UIApplication.sharedApplication().endIgnoringInteractionEvents() from the start and stop animating functions.
If what you want is really a splash screen, have a UIImageView underneath the view that you're loading. Set the image to your splash screen image. Set the loading view to hidden=YES before it's shown, then set hidden to NO after it finishes loading. You could even set the opacity of the frontmost view to give you a fading effect.
1.You need to find a kind of indicator, suck like an activity indicator or something else to show the loading UI to the user.
2.Set the user interaction unable, so that the user won`t touchup inside repeatedly.
3.Start the indicator, set the user interaction unable when you load the server data, and stop the indicator animation when you finish, hide the indicator, enabled the user interaction.

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

UIViewController viewDidLoad vs. viewWillAppear: What is the proper division of labor?

I have always been a bit unclear on the type of tasks that should be assigned to viewDidLoad vs. viewWillAppear: in a UIViewController subclass.
e.g. I am doing an app where I have a UIViewController subclass hitting a server, getting data, feeding it to a view and then displaying that view. What are the pros and cons of doing this in viewDidLoad vs. viewWillAppear?
viewDidLoad is things you have to do once. viewWillAppear gets called every time the view appears. You should do things that you only have to do once in viewDidLoad - like setting your UILabel texts. However, you may want to modify a specific part of the view every time the user gets to view it, e.g. the iPod application scrolls the lyrics back to the top every time you go to the "Now Playing" view.
However, when you are loading things from a server, you also have to think about latency. If you pack all of your network communication into viewDidLoad or viewWillAppear, they will be executed before the user gets to see the view - possibly resulting a short freeze of your app. It may be good idea to first show the user an unpopulated view with an activity indicator of some sort. When you are done with your networking, which may take a second or two (or may even fail - who knows?), you can populate the view with your data. Good examples on how this could be done can be seen in various twitter clients. For example, when you view the author detail page in Twitterrific, the view only says "Loading..." until the network queries have completed.
It's important to note that using viewDidLoad for positioning is a bit risky and should be avoided since the bounds are not set. this may cause unexpected results (I had a variety of issues...)
This post describes quite well the different methods and what happens in each of them.
currently for one-time init and positioning I'm thinking of using viewDidAppear with a flag, if anyone has any other recommendation please let me know.
Initially used only ViewDidLoad with tableView. On testing with loss of Wifi, by setting device to airplane mode, realized that the table did not refresh with return of Wifi. In fact, there appears to be no way to refresh tableView on the device even by hitting the home button with background mode set to YES in -Info.plist.
My solution:
-(void) viewWillAppear: (BOOL) animated { [self.tableView reloadData];}
Depends, Do you need the data to be loaded each time you open the view? or only once?
Red : They don't require to change every time. Once they are loaded they stay as how they were.
Purple: They need to change over time or after you load each time. You don't want to see the same 3 suggested users to follow, it needs to be reloaded every time you come back to the screen. Their photos may get updated... you don't want to see a photo from 5 years ago...
viewDidLoad: Whatever processing you have that needs to be done once.
viewWilLAppear: Whatever processing that needs to change every time the page is loaded.
Labels, icons, button titles or most dataInputedByDeveloper usually don't change.
Names, photos, links, button status, lists (input Arrays for your tableViews or collectionView) or most dataInputedByUser usually do change.

Resources