How to put buttons on SpriteKit with Swift code? - swift

I tried to make a game on Xcode 7 Beta, I almost finished it but the problem is I don't know how to put buttons with Swift code, I tried to put them on Main.storyboard but they don't arrange very well. some of them become bigger and some smaller etc... So I want to know how to put these buttons with code as in picture below.
Blue button first from the left is Leaderboard, then Facebook share, twitter share, and another default button (maybe it will be Remove Ads)

Your buttons should be .png files in the images.xcassets (you can add a new image set by clicking the + at the bottom). It will automatically choose the right size depending on the device/simulator. Then you create a variable to store your node like this:
let myNode = SKSpriteNode(imageNamed: "yourImage")
Then you can position the node multiple ways but i'll show you what I think will work best for you. Use myNode.position = CGPointMake(CGRectGetMaxX(self.frame) *0.40, CGRectGetMaxY(self.frame) * 0.25) This will get the max x and y values of the screen, multiply them by .4 and .25 respectively and then you can add the node to the scene will this line:
self.addChild(myNode)
Oh, and you can multiply the values by whatever decimal you want to get the nodes in the exact spot. IF you want them to be side by side you can even do something like this: myNode.position = CGPointMake(otherNode.position.x + myNode.frame.size.width/2, CGRectGetMaxY(self.frame) * 0.25)

Related

How to ask appium to drag an element?

I am taking the first steps in mobile automation.
I have a view such as below:
As you can see, I have a android.view.ViewGroup which I should drag an element over it.
My question is:
How can drag the button and take it to the right?
You can use the touchAction method :
Just provide the x , y coordinates upto to which you want to drag
new TouchAction(driver)
.press(PointOption.point(256, 1115))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(2000)))
.perform()
.moveTo(PointOption.point(256, 600))
.release()
.perform();
or if you wanna do it more precisely then you can get the device's x ,y width and drag it accordingly.

Hide UI/Image partially in Unity 5

Does Unity 5 support partial hiding of a UI/Image?
For example, the UI/Image I have in my scene has 100 width and 100 height.
At time = 0, the UI/Image is hidden. When time = 5, the UI/Image only shows the first 50 pixels. When time = 10, the UI/Image is fully drawn.
The answer to the question is in this link
Set the image type to Filled
Set the fill method to horizontal
Set the fill origin to left
From the script, update the fill amount from 0 to 1 over the timespan
On first thought, I can come up with two workarounds for this.
If the background of the image-in-question is a solid color, you can use another image with the same color as background that covers the actual image, so that it looks like the actual image is partially revealed. Then, just reduce the length of this covering image over time to achieve a revealing effect using Coroutines.
You make multiple image files with alpha channels and change the textures of the UI/Image over time. Each image will act like an iteration of revealing effect. Say you have 11 images, the 6th image will have first half revealed, and second half as alpha=0. In this case, if you want smoothness, you will need a higher number of images.

Trying to position a button programmatically, however not in right position according to X,Y

I'm trying to position a button.
Here is the code for the positioning..
[btnAbs setFrame:CGRectMake(57, 50, 106, 99)];
The coordinates I got are from here:
As you can see the xib stats the x & y to be at 57 and 192, which is where I want the button to be.
However when I run it in simulator, here is where its placed:
Obviously i could keep guessing and guessing the x and y coordinates, but this is very time consuming. So how come it's doing this?
Please join the links together when looking at the pics as i need more than 10 reps to post images, or a mod fix this please?
The problem is here:
The “origin” in Interface Builder doesn’t actually affect how the view gets positioned programmatically—it’s just a visual aid. If you click the dot in the top left of that box, the X and Y coordinates will change to the top-left of the view, which are the coordinates you want to pass to -setFrame:.
It looks to me as if you have the GUI designer aligning base upon the center center of your image view. When you do it in code, it is going to align based upon the top left of the image view.
Further, your code places it at a y of 50, where your GUI designer is showing a y coord. of 192.

How to Create Customized Time Bar in iPhone?

I need to create a time bar like the image attached.The blue lines are the dates indicating some actions.The big Red Arrow mark is used to slide for selecting any of the blue line. At background the bar is divided into years.The small red circles are to indicate the years.
Any idea ,how do i start?
Thanks
If the blue bars don't move a lot, and you don't need to resize this view very often (which is usually the case on an iPhone), then I'd recommend subclassing UIControl (or UIView) and implement it yourself.
Have an NSArray property storing NSDate objects and methods like addDate:, removeDate:, removeDateAtIndex: to change it's content from an other object (like your view controller). In these methods you add or remove the passed date and call [self setNeedsDisplay]; to redraw the lines.
You will need a few methods to calculate the position (in pixels) of a date on the slider and to calculate the date at a specific coordinate. This should be easy to to (basic linear interpolation).
And assuming that the red slider is only able to point to blue lines (but not between them) add a NSDate pointer variable to the currently selected date. In the
In the drawRect: method, you need to draw all the lines using CoreGraphics functions. Look up the documentation if you need help with this. Apple has some great sample apps too.
To show the red slider, you could either add a custom UIImageView, with a UIPanGestureRecognizer and make sure it only moves vertically in the gesture handler. Or it might be possible to add a UISlider without track images (I'm not 100% sure about that). In either case, you need to adjust the slider's position once the user let's go and make it jump to the nearest blue line.
A rough idea,
1. Get the length of the entire TimeBar.Let it be t.
2. Divide by x=numberofYears*365.let it be t_eachday_pos=t/x
3. get the position of each day,calculate its offset as daycount*t_eachday_pos;
4. Add your blueLine as a customButton.
5. To do some action, pass the x daynumber(eg. day 245 of year 2 etc.)
Try this, I have used the same thing in one of my app, and it is working

How to layout items "relatively" instead of absolute position?

What is the best way to layout iPhone UI items? Right now I am using CGRectMake and specifying the exact x and y coordinates of every UI item (label, view, button, etc) and adding every UI item to the current view.
One problem I see with this is that if one of the items change height or width, I will have to adjust the x and y coordinates of other UI items, so maintenance will be a challenge.
Is there an alternative to doing this? in HTML/CSS, I am used to just placing items relative to each other using margins, not absolute positioning. What do you recommend I do to keep maintenance easy and stress-free?
What I have been doing is using global constants to set the height, width, xoffset and yoffsets. Eg. I know I want 2 buttons next to each other, A and B where A is 20, 20 from origin and B is 20 right of A:
Constants.m:
CGFloat buttonHeight = 50, buttonWidth = 100;
CGFloat buttonAXoffset = 20, buttonAYoffset = 0;
CGFloat buttonBXoffset = 20, buttonBYoffset = 0;
CGFloat initialXoffset = 0, initialYoffset = 20;
(And use Constants.h to declare them with extern CGFloat blah)
To draw the buttons, set CGFloat creatorCursorX = initialXoffset, creatorCursorY = initialYoffset; which will be your relative cursor.
Then, just above creating the button A, do creatorCursorX += buttonAXoffset; creatorCursorY += buttonAYOffset;, and use creatorCursorX and creatorCursorY to set its position and buttonHeight and buttonWidth
After creating button A, do creatorCursorX += buttonWidth;
So when you get to making button B, you already have a cursor which you can use to place button B relative to button A just like above. Add offset before, add size after. And this makes it easy to switch buttons (and other layout items) around.
You can edit this method to place items down the screen as well.
Btw, to start the cursor on a new line, just set creatorCursorX = initialXoffset again and add the Y distance to creatorCursorY
I'm not an expert in this area, but I've found this method to be the most efficient and make it easy to adjust later. No need to sift through code, just shuffle chunks around for order, and go to the Constants.m to change the sizes.
I don't think there is an automatic way to achieve this.
I think your only option for this is to write a method, but even this is going to be tricky to work with, you'll need to determine the affected element and know which elements to reposition (see below for an extremely trivial example).
-(void)adjustUIElements:(CGSize)newSize{
//for example
button1.frame = CGRectMake(
(button1.frame.origin.x + newSize.width),
(button1.frame.origin.y + newSize.height),
button1.frame.size.width,
button1.frame.size.height);
//etc.
}
The great thing about this example is that you can run it any time there's a size change. It'll take a little while to get right but then you can reuse it in any function you see fit.
No. As for as I know, Relative Layout is not possible in iOS. You can use Interface builder to design the UI, which will be easier to position the UI elements.
You can also consider using the autoResizingMask property of UIView. Which is,
An integer bit mask that determines how the receiver resizes itself when its superview’s bounds change.
I couldn't find anything on the public-facing Apple site about it, but I read on one of the rumor sites that Apple will be introducing a new layout system in Lion called Cocoa Autolayout. Unfortunately, it won't be available for the next iOS release.
I don't want to link to any specific documents, but a search for "Cocoa Autolayout" on your favorite search engine will give you some general information how autolayout works.
If you're a registered iOS developer you should have access to one of the WWDC sessions on the topic.