This article was translated by Cocoachina Translator Group member @turtlefrommars from Appcoda, formerly Julian Engel, original: WatchKit introduction:building a simple Guess Game
Editor's note: Apple Watch will be officially released in a few months . As you know, Apple has included the WatchKit SDK on the Xcode 6.2 beta, allowing developers to create apps for Apple Watch. You may have started experimenting with WatchKit to develop your app, and if you don't have your hands on it, this introductory tutorial is for you. This week Julo Development Group Julian will introduce you to WatchKit and show you how to use it to create a simple guessing game, and you'll learn how to create a watch app, layout interface, and test your app.
Let's start the tutorial.
Apple's first built-in WatchKit in Xcode 6.2 beta, released on November 18, 2014, allows developers to create apps for Apple Watch. To develop an app for Apple Watch, first you need an Apple developer pay account to download the Xcode 6.2 beta. The sample app for this tutorial is written in swift language and requires some basic requirements for swift language programming.
Positive start.
Learn about the WatchKit App
Now that Apple doesn't allow developers to create a native Watch app, the three Apple Watch user experiences you can build today are WatchKit app,glance and actionable notifications (actionable notification). This tutorial focuses on the WatchKit app and does not involve glance and notifications.
First of all, the Watch app is not a standalone app, which means the pairing app that runs on the iphone is essential, the Watch app we're building is just an extension of the iphone app, and the Watch app is installed and managed by the iphone app. In addition, the IPhone app and watch app can communicate with each other, but this tutorial does not cover this part of the content.
Apple Watch has 38mm and 42mm two screen sizes, but without having to design the user interface separately, Xcode automatically adjusts the layout to fit the screen size. But instead of auto layout, this is WatchKit's unique layout system.
Sample App
Today we're going to create a simple guessing game for Apple Watch, with the following rules: The Watch app determines a natural number from 0 to 5, lets the player guess, and then detects if the player guesses and displays the information. The app interface is as follows:
Creating the WatchKit Sample App
I think you've already installed Xcode 6.2 Beta 4 (or later), create a new project with the single View application template named Watchkitdemo (you can name it), select language as Swift, and other settings intact. Save the project in a place you like.
As mentioned before, watch app is an extension of iphone app, to create watch app, choose Editor > Add Target on the Xcode menu, then choose Apple Watch > WatchKit app to build watch a All necessary PP can be automatically generated with the WatchKit app template.
Deselect include Notification Scene, the other options are the same, click Finish, and then pop up to ask if you want to activate the new scheme (scheme).
Click Activate to add a new scenario to your Xcode project that will test your app in the Apple Watch simulator. You can see two new folders in the Project navigation bar: WatchKit extension and the WatchKit App.
The WatchKit app contains only storyboard and user interface-related resources, while the code responsible for applying logic and updating the interface is located in WatchKit extension. In short, the Watch app provides a view of information, interacts with users, and is burdened with the iphone app.
Designing the User interface
Now let's start designing the Watch app's user interface. Select the Interface.storyboard file under the Watchkitdemo WatchKit app, similar to the iOS app, Xcode provides storyboard to design the user interface for the Watch app.
First drag a text label (label) into the interface controller, and you'll find that it automatically navigates to the top of the view controller because the Apple Watch screen size is small and the interface controller is Interface (top), Medium (CENTER) , under (BOTTOM) three areas. Now select the text label, under the position bar of the property inspector, you can set the vertical position and set the horizontal and vertical to center.
Then change the text to "Your guess Is:3",
Next we want to let the user modify the guessed number, drag a slider bar into the Interface (slider), and then modify the slider properties:
Since the guessing number is between 0 and 5, set the step size (steps) to 5 so that the sliders are divided into 5 bars, each representing a number. We want to make 3 the default number, set the value to 0.6, the minimum value (minimum) to 0, and the maximum (maximum) to 1.
Once set, drag a button from the Object Library (button), set the title to guess, and set the vertical position to center.
To build a complete interface, we also need a text label to show that the user is guessing the result is correct. Drag a label into the view, set the text as "press Guess to play", and then modify the vertical position to bottom, and set the min scale of the font to 0.5, allowing the text to fit into a narrower space. The final design is as follows:
Code time
OK, let's finish the interface and start writing the code. As stated earlier, the program logic is in WatchKit extension. Interfacecontroller.swift has been associated with the interface Controller that has just been designed.
Open the source file, create outlet for the text labels and sliders, and then add the following code to the Interfacecontroller class:
| 123 |
@IBOutlet varguessSlider: WKInterfaceSlider! // the slider@IBOutlet var guessLabel: WKInterfaceLabel! // the label displaying the guess number@IBOutlet varresultLabel: WKInterfaceLabel! // Wrong/Correct Label |
Then there is this line of code that creates a variable that guesses the number:
The project infrastructure is created and the interface elements are now connected to the corresponding outlet. Cut back to Interface.storyboard, open the auxiliary editor, display the code file that you just wrote, and then connect the outlet and the corresponding controls separately.
Now hold control from the slider to the code, this time not iboutlet, choose Ibaction, named Updateguess.
The following functions are added automatically:
| 1 |
@IBAction func updateGuess(value: Float) { } |
This method will be used to get the number that the user guesses and update the text hint where the two lines of code are inserted:
| 12 |
guessNumber = Int(value * 5)guessLabel.setText("Your guess: \(guessNumber)") |
Since the previous slider value range is set to 0 to 1, the value multiplied by 5 is the number we actually need, and then the text label is updated.
Control-Drag from the button to the code, and then create an action method named Startguess, which generates a random number and compares it to the number of guesses, and updates the prompt with the following code:
| 123456789 |
@IBAction func startGuess() { varrandomNumber = Int(arc4random_uniform(6)) if(guessNumber == randomNumber) { resultLabel.setText("Correct. You win!") } else{ resultLabel.setText("Wrong. The number is \(randomNumber)") }} |
The code first uses the Arc4random_uniform (6) function to generate a random number from 0 to 5, which is then compared to show the result.
Get. You can test the app.
Run Watch App
To run the app, select the "Watchkitdemo WatchKit app" scheme, then specify the device to run and click the Run button to test your app. The screen size can also be toggled by hardware > External displays > Apple watch-38mm in the simulator.
Apple Watch will be officially released in a few months.