1
00:00:07,030 --> 00:00:07,340
Hello.

2
00:00:07,360 --> 00:00:10,560
Hello ladies and gentlemen welcome back to the target topic.

3
00:00:10,570 --> 00:00:14,130
All about the Iowa accelerometer and the motion controls.

4
00:00:14,170 --> 00:00:19,870
We're going to go ahead and continue in this video to handle the accelerometer data that we are now

5
00:00:19,870 --> 00:00:20,830
accessing.

6
00:00:20,830 --> 00:00:22,460
Thanks to CM motion manager.

7
00:00:22,460 --> 00:00:27,520
So penélope in your project and we're just going to be diving right into our update labels function

8
00:00:27,520 --> 00:00:31,690
here a little more room for you guys so we can see everything that's going on here.

9
00:00:31,690 --> 00:00:39,270
All this good stuff in the last video we printed out the x y and z data for the accelerometer but it

10
00:00:39,400 --> 00:00:44,440
looked a little crazy on the screen it was updating you know as as the device is moving.

11
00:00:44,440 --> 00:00:49,840
And even if it's moving perfectly still the slightest of movements is enough to set it off.

12
00:00:49,840 --> 00:00:52,670
It's a very very very sensitive accelerometer.

13
00:00:52,930 --> 00:00:58,210
So what we're going to do is we're basically going to go ahead and just set the maximum number of fractional

14
00:00:58,210 --> 00:01:04,590
digits that we can display We're going to use a number formatter to convert the x y and z values and

15
00:01:04,590 --> 00:01:11,760
then we're also going to go ahead and update the text properties of our X Y and Z labels just like so.

16
00:01:11,950 --> 00:01:15,240
So let's do it take this print statement and delete it.

17
00:01:15,280 --> 00:01:16,250
We don't need it.

18
00:01:16,330 --> 00:01:17,220
Hey that rhymed.

19
00:01:17,530 --> 00:01:20,910
And what we're going to do is we're going to go ahead and create a number formatter.

20
00:01:20,980 --> 00:01:28,960
So go ahead and type let formatter equals number formatter and instantiate it like so we're going to

21
00:01:28,960 --> 00:01:36,040
go ahead and set up a minimum fraction digits property and a maximum fraction digits property so that

22
00:01:36,040 --> 00:01:42,400
it knows the range or the maximum number of fractional digits to put at the end of those decimal values.

23
00:01:42,400 --> 00:01:45,080
So go ahead and type formatter.

24
00:01:45,280 --> 00:01:50,890
Minimum fraction digits and I don't know if you saw that but it says the minimum number of digits after

25
00:01:50,890 --> 00:01:55,720
the decimal separator allowed as input and output by the receiver.

26
00:01:55,720 --> 00:01:58,550
So we're going to set a minimum of 1.

27
00:01:58,720 --> 00:02:02,980
So you know point 3 that's the the minimum is 1.

28
00:02:03,190 --> 00:02:08,530
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

29
00:02:08,530 --> 00:02:11,560
fraction digits to also be equal to 1.

30
00:02:11,560 --> 00:02:17,970
Now all we need to do is to pull out the individual values for x y and z.

31
00:02:18,040 --> 00:02:24,310
But when we printed out our accelerometer data you saw a printout as one long string containing all

32
00:02:24,310 --> 00:02:25,680
of that information.

33
00:02:25,750 --> 00:02:27,370
But there's an easier way to get into it.

34
00:02:27,370 --> 00:02:34,330
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

35
00:02:34,420 --> 00:02:36,510
values from the accelerometer data.

36
00:02:36,520 --> 00:02:37,110
Check it out.

37
00:02:37,120 --> 00:02:42,070
So type Let X and that's going to be equal to accelerometer data.

38
00:02:42,400 --> 00:02:47,010
But if we type X we don't get a property if we type just the dot.

39
00:02:47,020 --> 00:02:54,170
You can see there's a property within the data here within the S.M. accelerometer data called acceleration.

40
00:02:54,400 --> 00:02:58,980
And this is a property that holds the acceleration measured by the accelerometer.

41
00:02:58,990 --> 00:03:07,570
So if we select this and then try to pull out a value check that out we get the x axis acceleration

42
00:03:07,660 --> 00:03:10,150
injuries which is also a gravitational force.

43
00:03:10,150 --> 00:03:13,380
Kind of cool that we can measure Gs on our iPhone.

44
00:03:13,540 --> 00:03:19,540
But anyway so if we print out the x value here X is now equal to whatever value is coming out of the

45
00:03:19,540 --> 00:03:30,880
x axis we can do the same thing for y acceleration dot dot y z equals accelerometer data acceleration

46
00:03:30,880 --> 00:03:31,870
dot Z.

47
00:03:31,890 --> 00:03:34,090
K. Super-Duper cool stuff.

48
00:03:34,090 --> 00:03:40,920
And now all we need to do is we need to go ahead and essentially pull out the.

49
00:03:41,110 --> 00:03:44,710
Now all we need to do is essentially convert these into strings.

50
00:03:44,710 --> 00:03:47,410
And if you remember well hey you know what let's do it.

51
00:03:47,410 --> 00:03:51,790
Let's actually print these out as they are and let's see what we get X Y and Z.

52
00:03:51,820 --> 00:03:56,650
So let's build and run this wups command are to build and run and that we're going to see what happens

53
00:03:56,650 --> 00:04:00,190
when we actually print those values out exactly as they are.

54
00:04:00,190 --> 00:04:03,160
Here we go pulling it open here and look at that.

55
00:04:03,280 --> 00:04:08,380
We are still getting very similar values to what we were last time if I took my phone up the values

56
00:04:08,380 --> 00:04:09,390
change here.

57
00:04:09,430 --> 00:04:10,330
Tilt it down.

58
00:04:10,330 --> 00:04:12,760
They change to the side to the left to the right.

59
00:04:12,760 --> 00:04:14,520
They're all changing.

60
00:04:14,770 --> 00:04:19,150
But guys this is a lot of information for every single update.

61
00:04:19,150 --> 00:04:21,250
We don't want that that much information.

62
00:04:21,250 --> 00:04:24,030
And that's actually why we created this number matter.

63
00:04:24,130 --> 00:04:26,590
So go ahead and stop it from running.

64
00:04:26,590 --> 00:04:27,960
Delete this print statement here.

65
00:04:27,970 --> 00:04:33,160
And what we're going to do is we're going to use our formatter and we're going to go ahead and type

66
00:04:33,190 --> 00:04:35,670
formatter dot string for.

67
00:04:35,860 --> 00:04:41,020
And we can pass in a number of value in it will convert it into a string matching the formatting that

68
00:04:41,020 --> 00:04:41,560
we desire.

69
00:04:41,560 --> 00:04:48,670
So go ahead and type formatter dot string for any candidate that allows you to pass in any number value

70
00:04:49,180 --> 00:04:50,130
like so.

71
00:04:50,500 --> 00:04:50,920
OK.

72
00:04:50,920 --> 00:04:56,680
We're going to go ahead and cut this and replace that any with our accelerometer data and we're going

73
00:04:56,680 --> 00:05:02,530
to do it again here formatted string for we're going to take our accelerometer data here and we're going

74
00:05:02,530 --> 00:05:09,040
to go ahead and replace any with the wide data and now we're going to go ahead and call formatter string

75
00:05:09,040 --> 00:05:10,160
for.

76
00:05:10,500 --> 00:05:17,570
And we'll do the same thing for the Z-axis of course getting rid of that extra are now.

77
00:05:18,250 --> 00:05:19,620
We're getting musical anyway.

78
00:05:19,770 --> 00:05:25,100
So now we have X Y and Z available to us as strings.

79
00:05:25,170 --> 00:05:28,760
And now all we need to do is set the text property of our label.

80
00:05:28,890 --> 00:05:34,890
So go ahead and type X label dot text set it to be equal to x.

81
00:05:35,250 --> 00:05:37,830
Ok that's just going to print out the numerical value though.

82
00:05:38,250 --> 00:05:44,140
And in our storyboard here we wanted it to say X colon and then print the number.

83
00:05:44,160 --> 00:05:51,930
So what we can actually do is we can go ahead and create a string here and we can say X colon space

84
00:05:52,800 --> 00:05:57,840
and then actually you know let's just do a space and then we're going to go ahead and encapsulate this

85
00:05:57,840 --> 00:06:05,000
string and print out the value just like so k so X space and then print out the value for x.

86
00:06:05,100 --> 00:06:07,110
We can do the same thing with the y label.

87
00:06:07,120 --> 00:06:09,880
Whoops looks like we're taking part of that on accident.

88
00:06:10,170 --> 00:06:10,690
Whoops.

89
00:06:10,700 --> 00:06:14,670
Why label that text equals string.

90
00:06:14,730 --> 00:06:20,430
Why Kolins space and go ahead and encapsulate the y value in a string.

91
00:06:20,430 --> 00:06:21,930
Then what are we going to do.

92
00:06:22,110 --> 00:06:30,930
We're going to do Z label and we will print out Z label like so by typing Z colon and then encapsulate

93
00:06:30,930 --> 00:06:33,490
the Z property just like so.

94
00:06:33,600 --> 00:06:38,130
Now if I build and run this you'll notice string interpellation produces a bug description for an optional

95
00:06:38,130 --> 00:06:40,490
value did you mean to make this explicit.

96
00:06:40,500 --> 00:06:45,450
That's basically saying hey we don't know explicitly that this is a string and check it out.

97
00:06:45,450 --> 00:06:49,400
That's because string for object returns an optional string.

98
00:06:49,500 --> 00:06:57,150
You know if we passed in the wrong data type it could cause a crash but since number values can be encapsulated

99
00:06:57,150 --> 00:07:03,160
properly as a string we know we're safe to force on this and if we do that then that string interpellation

100
00:07:03,180 --> 00:07:09,210
error goes away because this is now optionally or non optionally forced as a string.

101
00:07:09,210 --> 00:07:11,070
So are you ready to try it out.

102
00:07:11,420 --> 00:07:12,240
Let's build and run.

103
00:07:12,240 --> 00:07:14,450
Let's see what we get.

104
00:07:14,780 --> 00:07:17,070
Oh no you know what.

105
00:07:17,070 --> 00:07:21,570
Cancel that going to go ahead and print out X Y and Z again before we see what it looks like in the

106
00:07:21,570 --> 00:07:23,080
label.

107
00:07:23,640 --> 00:07:25,160
X Y and Z.

108
00:07:25,380 --> 00:07:32,430
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

109
00:07:32,460 --> 00:07:33,680
but the cons..

110
00:07:33,780 --> 00:07:34,410
And look at that.

111
00:07:34,410 --> 00:07:37,540
That is much much easier data to deal with.

112
00:07:37,710 --> 00:07:44,540
As you can see since we removed so much of the information we removed a lot of the decimal points.

113
00:07:44,580 --> 00:07:50,310
Obviously we get less accuracy and if you wanted you would use more accuracy in an actual application.

114
00:07:50,310 --> 00:07:55,950
But for just being an app that displays the general data what we can do is we can use this.

115
00:07:56,190 --> 00:08:00,820
And as I move my phone you can see that it actually changes the values.

116
00:08:00,840 --> 00:08:02,630
That's so cool.

117
00:08:02,700 --> 00:08:07,860
So let's go ahead and let's actually uncomment where we passed it into the labels and then we'll be

118
00:08:07,860 --> 00:08:09,300
able to actually see what's going on.

119
00:08:09,300 --> 00:08:13,540
So I'll pull up in the simulator as soon as it is done here.

120
00:08:13,550 --> 00:08:15,960
Here we go up up.

121
00:08:16,740 --> 00:08:17,520
All right.

122
00:08:17,740 --> 00:08:19,130
So look at that.

123
00:08:19,230 --> 00:08:22,230
That's actually updating and if I move my phone.

124
00:08:22,230 --> 00:08:27,650
Those labels are going to be changing based on how I move it so I can rotate it to the right.

125
00:08:27,900 --> 00:08:33,210
Rotate it back up rotate it down and you can see the proper axes move.

126
00:08:33,210 --> 00:08:37,190
The way you would expect really really cool stuff guys.

127
00:08:37,210 --> 00:08:38,100
Wow.

128
00:08:38,470 --> 00:08:41,370
So yeah that's how you use the accelerometer in Iowa.

129
00:08:41,380 --> 00:08:46,270
It's really no nonsense it's really easy like there's not even a delegate that goes with this you just

130
00:08:46,270 --> 00:08:51,340
call the function pass it into a handler and then perform the actions based on how the accelerometer

131
00:08:51,340 --> 00:08:53,310
data how you want to handle it.

132
00:08:53,310 --> 00:08:55,940
So super super easy stuff guys.

133
00:08:55,960 --> 00:08:58,470
This is the first section of this target topic.

134
00:08:58,480 --> 00:09:04,300
You're already done fantastic work in the next section we're going to build an app that utilizes the

135
00:09:04,300 --> 00:09:10,600
accelerometer and the gyroscope to create what's called a parallax effect and it's a beautiful way of

136
00:09:10,600 --> 00:09:17,320
using the motion sensors to move content dynamically as you move your device just like some of the wallpapers

137
00:09:17,500 --> 00:09:19,260
on the iPhone if you set your wallpaper.

138
00:09:19,480 --> 00:09:24,310
You can set it in parallax mode so that when you move your device you can see the wallpaper sort of

139
00:09:24,310 --> 00:09:30,610
shift as you move your device it helps it to look beautiful and dynamic and it can just add that little

140
00:09:30,610 --> 00:09:33,610
extra magic into your applications.

141
00:09:33,610 --> 00:09:39,100
So let's head over to the next section let's be introduced to that app see how it works and then let's

142
00:09:39,100 --> 00:09:39,730
build it.

143
00:09:39,820 --> 00:09:41,210
I'll see you there.
