Hello.
Hello ladies and gentlemen welcome back to the target topic.
All about the Iowa accelerometer and the motion controls.
We're going to go ahead and continue in this video to handle the accelerometer data that we are now
accessing.
Thanks to CM motion manager.
So penélope in your project and we're just going to be diving right into our update labels function
here a little more room for you guys so we can see everything that's going on here.
All this good stuff in the last video we printed out the x y and z data for the accelerometer but it
looked a little crazy on the screen it was updating you know as as the device is moving.
And even if it's moving perfectly still the slightest of movements is enough to set it off.
It's a very very very sensitive accelerometer.
So what we're going to do is we're basically going to go ahead and just set the maximum number of fractional
digits that we can display We're going to use a number formatter to convert the x y and z values and
then we're also going to go ahead and update the text properties of our X Y and Z labels just like so.
So let's do it take this print statement and delete it.
We don't need it.
Hey that rhymed.
And what we're going to do is we're going to go ahead and create a number formatter.
So go ahead and type let formatter equals number formatter and instantiate it like so we're going to
go ahead and set up a minimum fraction digits property and a maximum fraction digits property so that
it knows the range or the maximum number of fractional digits to put at the end of those decimal values.
So go ahead and type formatter.
Minimum fraction digits and I don't know if you saw that but it says the minimum number of digits after
the decimal separator allowed as input and output by the receiver.
So we're going to set a minimum of 1.
So you know point 3 that's the the minimum is 1.
And you know what I don't know if I want it to be any more than 1 so I'm going to also set the maximum
fraction digits to also be equal to 1.
Now all we need to do is to pull out the individual values for x y and z.
But when we printed out our accelerometer data you saw a printout as one long string containing all
of that information.
But there's an easier way to get into it.
So let's create one property for each axis X Y and Z and then we're going to go ahead and pull out the
values from the accelerometer data.
Check it out.
So type Let X and that's going to be equal to accelerometer data.
But if we type X we don't get a property if we type just the dot.
You can see there's a property within the data here within the S.M. accelerometer data called acceleration.
And this is a property that holds the acceleration measured by the accelerometer.
So if we select this and then try to pull out a value check that out we get the x axis acceleration
injuries which is also a gravitational force.
Kind of cool that we can measure Gs on our iPhone.
But anyway so if we print out the x value here X is now equal to whatever value is coming out of the
x axis we can do the same thing for y acceleration dot dot y z equals accelerometer data acceleration
dot Z.
K. Super-Duper cool stuff.
And now all we need to do is we need to go ahead and essentially pull out the.
Now all we need to do is essentially convert these into strings.
And if you remember well hey you know what let's do it.
Let's actually print these out as they are and let's see what we get X Y and Z.
So let's build and run this wups command are to build and run and that we're going to see what happens
when we actually print those values out exactly as they are.
Here we go pulling it open here and look at that.
We are still getting very similar values to what we were last time if I took my phone up the values
change here.
Tilt it down.
They change to the side to the left to the right.
They're all changing.
But guys this is a lot of information for every single update.
We don't want that that much information.
And that's actually why we created this number matter.
So go ahead and stop it from running.
Delete this print statement here.
And what we're going to do is we're going to use our formatter and we're going to go ahead and type
formatter dot string for.
And we can pass in a number of value in it will convert it into a string matching the formatting that
we desire.
So go ahead and type formatter dot string for any candidate that allows you to pass in any number value
like so.
OK.
We're going to go ahead and cut this and replace that any with our accelerometer data and we're going
to do it again here formatted string for we're going to take our accelerometer data here and we're going
to go ahead and replace any with the wide data and now we're going to go ahead and call formatter string
for.
And we'll do the same thing for the Z-axis of course getting rid of that extra are now.
We're getting musical anyway.
So now we have X Y and Z available to us as strings.
And now all we need to do is set the text property of our label.
So go ahead and type X label dot text set it to be equal to x.
Ok that's just going to print out the numerical value though.
And in our storyboard here we wanted it to say X colon and then print the number.
So what we can actually do is we can go ahead and create a string here and we can say X colon space
and then actually you know let's just do a space and then we're going to go ahead and encapsulate this
string and print out the value just like so k so X space and then print out the value for x.
We can do the same thing with the y label.
Whoops looks like we're taking part of that on accident.
Whoops.
Why label that text equals string.
Why Kolins space and go ahead and encapsulate the y value in a string.
Then what are we going to do.
We're going to do Z label and we will print out Z label like so by typing Z colon and then encapsulate
the Z property just like so.
Now if I build and run this you'll notice string interpellation produces a bug description for an optional
value did you mean to make this explicit.
That's basically saying hey we don't know explicitly that this is a string and check it out.
That's because string for object returns an optional string.
You know if we passed in the wrong data type it could cause a crash but since number values can be encapsulated
properly as a string we know we're safe to force on this and if we do that then that string interpellation
error goes away because this is now optionally or non optionally forced as a string.
So are you ready to try it out.
Let's build and run.
Let's see what we get.
Oh no you know what.
Cancel that going to go ahead and print out X Y and Z again before we see what it looks like in the
label.
X Y and Z.
Now I'm going to build and run it k and we're going to see what we get in the simulator or not the simulator
but the cons..
And look at that.
That is much much easier data to deal with.
As you can see since we removed so much of the information we removed a lot of the decimal points.
Obviously we get less accuracy and if you wanted you would use more accuracy in an actual application.
But for just being an app that displays the general data what we can do is we can use this.
And as I move my phone you can see that it actually changes the values.
That's so cool.
So let's go ahead and let's actually uncomment where we passed it into the labels and then we'll be
able to actually see what's going on.
So I'll pull up in the simulator as soon as it is done here.
Here we go up up.
All right.
So look at that.
That's actually updating and if I move my phone.
Those labels are going to be changing based on how I move it so I can rotate it to the right.
Rotate it back up rotate it down and you can see the proper axes move.
The way you would expect really really cool stuff guys.
Wow.
So yeah that's how you use the accelerometer in Iowa.
It's really no nonsense it's really easy like there's not even a delegate that goes with this you just
call the function pass it into a handler and then perform the actions based on how the accelerometer
data how you want to handle it.
So super super easy stuff guys.
This is the first section of this target topic.
You're already done fantastic work in the next section we're going to build an app that utilizes the
accelerometer and the gyroscope to create what's called a parallax effect and it's a beautiful way of
using the motion sensors to move content dynamically as you move your device just like some of the wallpapers
on the iPhone if you set your wallpaper.
You can set it in parallax mode so that when you move your device you can see the wallpaper sort of
shift as you move your device it helps it to look beautiful and dynamic and it can just add that little
extra magic into your applications.
So let's head over to the next section let's be introduced to that app see how it works and then let's
build it.
I'll see you there.