How do I change UILabels within my SKScene code? - swift

I'm making an app where I have a tower in the background rendered with SpriteKit. The tower is made out of different blocks where I get the data from. I want to display this data in a UIView with different UILabels, etc...
Here's an image of the UIView and UILabels I try to link
https://imgur.com/vOw5QoW
The problem is that I can't figure out how to link my labels and view to my SKScene code (which is not possible I know). I'm struggling to find a way to link these UI objects to my SKScene code (Show and hide the view when a certain action happens etc...)
Is there a way to link these 2?
(I know the existence of creating UI elements with SK code, but that's not what i'm looking for.)

If you want to display both SKView and UIView on the same screen, you can use Container View Controllers.
You define "container views" in your storyboard and assign them to separate view controllers via "embedded" segue.
Here's a good article on doing it - https://useyourloaf.com/blog/container-view-controllers/

Related

Swipe Section Page - Swift 3

I want to make the horizontal swipeable page like YouTube. Is there an object in XCode for this? How do I have to do it? I did not find a tutorial about it. Sorry for my English.
Like this
There are tons of components already made for this in the internet, you can try looking at the cocoacontrols site.
If you still want to write your own code for this, one way is writing a custom UIView subclass. The YouTube feature looks very simple and as far as I can guess, they can use two separate UIView subclasses: one for the menu and one for the pages, just as a container.
For the menu you can use UIStackViews or a single UIViews with UIButtons for the page's titles and another view for the selection effect that moves with the UIButton's touch event. This view should provide a delegate or any notification system that fits better to you in order to notify the container that needs to load the right UIViewController's view inside the container.
The container can be a UIView that loads the view property from the UIViewController subclass on demand. Make sure to add the loaded view controller as a child of the parent view controller, otherwise you will loose some important features.
I hope it can help you to start.

How to Display Custom View Modaly

I've written a small multiplayer game for the iphone. Once one of the players win, I want to display him 'You Win' image and a button so that he can play again.
How can this be done? One option is to use a segue to a new view-controller, but I think this should be shown with the game in the background. What would you suggest, as I'm pretty sure this is a common scenario for an iphone game/app.
EDIT: I ended up using both Phillip Mills's answer and Selkie's answer. Here is on How to Use UIView transitionWithView?
You can create the custom view and keep it as a separate property within your main game view controller. When someone wins, add it as a subview to the view property of the controller. It can be full screen size with transparency so that it's effectively modal, yet shows the game as background.
You can make it as a view, then show it using UIView animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
The animation block could be sliding up, fading or whatever you want.
The other way is to add it at the beginning, but set its hidden property as YES. Change it to NO when it's needed.

Multiple UIViewControllers - Best way to implement this

I'm building an iOS application and I try to determine the best way to implement this :
I have a main UIViewController (MainViewController) that displays a simple view. This view contains a button that let the user add an object (let's say a Circle) to the main view. The user can add multiple circles by pressing the button and move each of them by dragging them. The circle objects should have their own color (randomly chosen).
The question is: what is the best way to implement this?
Should I create an other UIViewController subclass (CircleViewController) for the Circle object, whose view actually draws the circle?
And then, when the user presses the button, should I create a new instance of this CircleViewController and add its view to the MainViewController?
When the user double-tap a circle, it should disappear... How can I send a message to the mainViewController to tell it to remove the concerned CircleViewController's view?
Thank you very much for your help.
If your object is really as simple as a circle you should look at Quartz in Apple's Documentation and the method drawRect: in UIView. If you are doing something more like an image you could subclass UIView and put your code in there. Either way, you do not need to create new viewControllers.

Grasping the concept of UIviews and UIViewcontrollers

A couple of weeks ago i have started my research in Iphone app development and after alot of hello world applications in different settings i am now ready for my very first application based on the MVC design pattern used in Cocoa.
This opens up alot of questions for me though and after reading the different class references about UIViews and controllers i am stuck in trying to figure out which one i should be using.
In my application i am trying to create a grid of small rectangle's with each rectangle having a different text value on them, to be more specific, i am trying to create a simple calender that will display all the days of a month in a grid.
Every rectangle is a instance of a class i named Tile, in this class i want to implement the drawRect method to draw the rectangle for me and set the text value to the day it should represent.
In order to implement this i have done some research on how this should be done.
From what i have learned so far is that UIViewcontrollers do not really display anything, they are basically sitting there waiting to respond to any events from their children.
In my application i would translate this to the Controller that will respond to each touchevent on a tile.
A UIView however is also a container but one for objects that will need drawing methodes like drawRect. This would translate to the grid that will hold all of the tiles if i'm correct.
Except, i have no clue what subclass i should use for each tile, i have the feeling i am really missing some basic knowledge here but i just can't figure it out. Would really appreciate it if anyone could point me in the right direction with this.
If there were any two apple document you should read, it is the one about UIViewControllers which can be found here and the one about UIViews which can be found here. The UIViewController, as you mentioned, is more about integrating with the iOS system than being a visible component. It has a reference to a UIView, and that UIView is the root node in the visible tree of elements which starts at that View Controller.
In iOS programming you don't really need to worry about drawing rectangles, because for the most part you will be extending elements which know how to draw themselves and then just telling them where to go. The basic visible element in this case, is the UIView. There are many different kinds of UIViews (see the graphic in the UIView programming guide link), so for your case you could use a simple UIView with a background image set to your calendar box graphic, and add a subview of type UILabel. UILabel is a subclass of UIView, so you know it will be something visible as well.
Once you grasp these concepts (which can take a long time) Interface Builder will start to make more sense and you can start doing some of these things with it - and understand how its working. In essence it will create the hierarchy of a UIViewController referencing a hierarchy of UIViews automatically, then you.
Tile should be subclass of UIView since you want to drawRect your "days". Then you can add as many Tiles as you want to your UIViewController.view and manipulate them from UIViewController code (.m file).
But you can add UILabels to your Tile view and manipulate them by setting their text property. In this case you won't need to override drawRect: at all, UILabel will do the rest for you, but you will have to programmatically add this labels to your Tile (e.g. in Tile's init method) or in Interface Builder. In the last case you will have to load them from XIB using [[NSBundle mainBundle] loadNib:owner:options:] method.

UIButton Game character selection to UIImageView animation

I am almost at the end of coding my kids educational application, woohoo!.
But... Im stuck on something.
When app loads i have my main view. It has 4 buttons for flipviews(each with ten views of content) and 4 buttons for character selection(an image that will follow you through every screen of content).
Problem is im unsure on how to link UIButton selection to UIImage display in multiple views. I want the user to choose a character button and then continue to the flipviews and in the views the image displayed should be the one that they have selected on the main view. So everytime they return to the main view they can change the character that will follow them around the app.
Any thoughts, help or code would be much appreciated!
Thank You
Alex
Make a new object, a subclass of UIImageView, which has a -setImage method. Once you set the image, then where ever you embed that object, it will display the same image. You could even have that subclass view have a score displayed next to it, or a name or other stats, so as you go from one screen to another, you have all that info follow you around with the image. No need to create labels in all the screens for global info like that.
In summary:
make a new subclass of UIView or UIImageView in Xcode using the New File... menu. You would do new UIView if you will have other items than just an image.
add methods that allow you to set the image, update text stats etc.
BONUS: you can make the class handle taps, so if a user taps the image, you could do something like provide help or run a cute little animation
embed that object in any screens you wish. Keep in mind that you can have that view be sized differently in each screen using transforms. Cool, no?
that's pretty much it!
Good luck!