然后通过control-drag设置New Run button和New Run View Controller的segue为push。
New Run View Controller有两个状态:pre-run和during-run,通过事务逻辑管理它们是否显示。现在为起添加三个UIlabel分别设置为
Distance,Time,Pace。
再添加一个UILabel作为消息提示。最后加两个UIbutton:Start\Stop。最后你的视图如下所示:
This import MapKit,so that you can make use of MKMapKit. It also add outles for the various parts of the UI.
Then it sets up the basic for the rest of the code. A method that will configure the view for the current run,
whitch is called when the view is loaded and when a run is seted.
There’s last step before you’re ready to return to your Storyboards and connect all your outlets.
Open HomeViewController.m and add the following import at the of the file.
Then add the following method int the implementation:
Finally, head back to your Stortboard and set the following.
Set the class of the Home View Controller of HomeViewController.
Set the class of the New Run View COntroller of the NewRunViewController.
Connect all the outlets of the NewRunViewControllerand DetailViewController.
Connect both the receiced actions(startPressd and stopPressed) in NewRunViewController.
Connect the MKMapView to DetailViewController as it delegate.
After you done all right, you can build and run.
Math and Unit
There are several of the views you created and attached in the storyboard involve displaying statistic and times.
Core Location measures everything int the units of science, it also called metric system. However people in
United States use miles, so you need to make sure both systems are live in your apps.
Click File/New"File. Select iOS/CocoaTouch\Objective-C Class. Call the file MathController, inheriting from
NSObject and create it. Then Open MathController.h and make the file look like this:
Then open MathController.m and add the following to the top of the file.
Next, add the following methods to the implementation:
These methods can helpers that covert from distances,time and speeds into pretty strings.
Starting Run
Now, you'll start recording the location of the user for the durtion of the run.
First, you have to make a important project-level change. Click the project at the top of project navigate.
Then select Capabilities tab and open up Background Modes. Trun on the switch for this section on the right
and then tick Location update. This will allow the app to update the location even if the user presses the home
button or to take a call.
Now back to the code. Open NewRunViewController.m. Then add following imports at the top of the file:
Then add the CLLocationManagerDelegate protocol conformance declaration and several new propertiesto the
class extension category:
seconds tracks the durtion of run, in seconds.
distance cumulative distance of the run, in meters.
locationManager tell to start or stop reading the user’s location.
locations is an array to hold all the Location objects that will roll in.
timer will fire each second and update the UI accordingly.
Now add the following method to the implementation:
With this method, the timer will stoped when user navigates away from the view.
Add the following method:
This method will be called every scend.
Another method, add the following:
Finally, you tell the manager to start getting location updates.
To actually begin the run, add these lines to the end of startPressed::
Recordig the Run
You’ve created the CLLocationManager, but now you need to get updates from it.
Open NewRunController.m and add the following method:
This will be called each time there are new location updates to provide the app.
Saving the Run
You already arranged for the UI to accept this input, and now it’s time to process that data.
add this method to NewRunViewController.m:
In this method, you create a new Run object and give it the cumulative distance
and duration values as well as assign it a timestamp.
Each of the CLLocation objects recorded during the run is trimmed down to a new Location object and saved.
Finally, edit actionSheet:clickedButtonAtIndex: so that you stop reading locations
and that you save the run before performing the segue.
Run with the Simulator
I know that you want the apps you build encourage more enthusiasm for fitness.
You don’t need to lace up and head out the door either, for there’s a way to get the simulator to pretend it’s running!
Build & runin the simulator, and select Debug\Location\City Run to have the simulator start generating mock data:
Revealing the Map
Now, it's time to show the run stats.
Open the DetailViewController.m and add this at the top of the file:
Then, make configureView look like this:
This set up the details of the run into the varius labels.
Drawing the map will require just a little more detail. First, the region needs to be set so that can be shown on the run.
Then the line drawn over on the top to indicate where the run went to needs to be created and finally styled.
add the following method:
An MKCoordinateRegion represents the diaplay region for the map, and you define it by supplying a
center point and a span that defines horizontal and vertical ranges.
Next, add the following method:
This method says that whenever the map comes across a request to add an overlay, it should check if
it's a MKPolyline. If so, it should use a render that will draw a black line. An overlay is something
that drawn on the top of the map view. A polyline is such an overlay and represents line drawn from a
series of location points.
Lastly, you need to the define the coordinates for the polyline. Add the following method:
Here, you shoved the data from the Location objects into an array of CLLocationCoordinate2D.
Now, let's put these three together!
Here, you make sure that these are points to drawn, set the map region as define earlier, and add
the polyline overlay.
Last, you have to add the code at the end of configureView.
And now, you can build & Run, you should see a map after your simulator is done its workout.
Color Polyline
Now, the app is pretty cool, but I want to help users train more smarter is to show them how fast or slow
they ran.
Click File/New/File. Select iOS\Cocoa Touch\Objective-C class. Call the file MulticolorPolylineSegment that
inherits MKPolyline and create it. Then open MulticolorPolylineSegment.h and make it look like this:
The color is going to denote the speed and so the color of each segment is stored here on the polyline.
Other than that, it's the same as MKpolyline.
Then add the following implementation of the mothod:
This method returns the array of speed value for each sequential pair of locations.
First, you'll notice that a loop through all locations from the input. You have to covert each
Location to a CCLocation so you can use distanceFromLocation.
Then add the following code, just before the return in the method you just added:
Here you define three color you'll use for slow, medium and fast polyline segments.
At last, remove the exiting retun and add the followin code to the end of the method:
In this loop, you determine the value of each pre-calculated speed, relative to the full range of speeds.
Then determine the UIColor to apply to the segment.
Applying The Color Segments
Open the DetailViewController.m and add following import to the top of the file:
Then, find the LoadMap: replace the following line:
with:
This creates the array of segments using MulticolorPolylineSegment and adds all overays on the map.
Lastly, you shoud prepare your polyline renderer to pay attation to the specific color of each segment.
So replace your current implementation of mapView:rendererForOverlay: with the following code:
Now you can build & run, you'll see the multicolored map.
Show the map during the run
Now, the post-run map is stunning, but how about during the run?
Open Main.storyboard and find the New Run View Controller. Drag in a new NKMapView:
Then open NewRunViewController.m add this import at the top:
And add the MKMapViewDelegate protocol conformation declaration to this line:
Then add IBOutlet for the map to the class extension category:
And add this line to end of the viewWillAppear::
This makes sure the map is hidden at first. Now add this to the end of the startPressed::
This make sure the map appear when the run starts.
We also want to see the ployline, so let's use our old friend, mapView:rendererForOverlay:.
Add the following method:
In this method the polyline will always in blue.
Next, you need to write some codes to update the map region and draw the polyline ever time valid location
is found. Find your implementation of locationManager:didUpdateLocations: and update it to this:
Now, the map will always centers on the most recent location. and constantly adds little blue polylines.
Lastly, open Main.storyboard and find the New Run View Controller. Connect the outlet for mapView
on the map view, and set the mapView's delegate to the view controller.
Build & Run, and start a new run. You'll see the map updating in real-time.
This is the second and final part of the Run-Tracking tutorial. In first part, you created an app that: Use Core Location to track your route.Continually maps your path and reports your average pace for your run.Shows the map for yout route when...…