1
00:00:05,890 --> 00:00:09,090
What's a bigger one Mark Price you're at death's slopes dot com.

2
00:00:09,090 --> 00:00:13,390
And let's continue building our arcade app here.

3
00:00:13,530 --> 00:00:18,900
And we want to do is get these items rotating and it may not be that complex actually and then maybe

4
00:00:18,900 --> 00:00:20,010
we'll do some refactoring.

5
00:00:20,010 --> 00:00:22,550
So first things first is how are we going to do it.

6
00:00:22,650 --> 00:00:28,220
Well we're going to need to just like you would use and spray it or seen it we're going to use an action.

7
00:00:28,530 --> 00:00:35,640
And what we're going to do is create a rotary action sort of say let rotate equals LCN action.

8
00:00:35,640 --> 00:00:45,720
So a scene action that repeat forever and then we'll put an action set of here seen action that rotate

9
00:00:45,720 --> 00:00:46,250
by.

10
00:00:46,290 --> 00:00:47,670
We want to rotate by something.

11
00:00:47,670 --> 00:00:48,380
OK.

12
00:00:48,570 --> 00:00:56,190
And we are going to say zero on the X but on the Y we're going to say C.G. float because we need to

13
00:00:56,190 --> 00:01:01,630
cast a value here 0.01 times double dot pi.

14
00:01:03,780 --> 00:01:05,510
Doubled up pi is just pi.

15
00:01:05,560 --> 00:01:08,810
So it's the way you get pi you know 3.1 for blah blah blah.

16
00:01:09,210 --> 00:01:14,400
And then on the z axis we're going to rotate that either one is zero and the duration we're going to

17
00:01:14,400 --> 00:01:15,540
say 0.1.

18
00:01:15,540 --> 00:01:23,970
So every point one of a second we want to rotate it by point zero 1 times doubled up PI along the along

19
00:01:23,970 --> 00:01:28,410
the y axis and then it'll rotate it very smoothly.

20
00:01:28,470 --> 00:01:30,300
So long as we run the action forever.

21
00:01:30,390 --> 00:01:39,200
And so we've got the action here so what we can do now is here in our node we should just be able to

22
00:01:39,200 --> 00:01:48,630
add it to that so we're going to say no dot add action or run action is what I was looking for.

23
00:01:48,700 --> 00:01:59,330
Rotate and we're just going to copy this here and we're going to paste it here and we're going to paste

24
00:01:59,330 --> 00:02:01,920
it here.

25
00:02:02,450 --> 00:02:05,840
We're using the same action and rotating by the y axis.

26
00:02:05,850 --> 00:02:08,710
And let's see if that actually works for us here.

27
00:02:08,850 --> 00:02:13,710
And look at that we have rotating objects now.

28
00:02:14,720 --> 00:02:16,100
We're going to see this as is.

29
00:02:16,220 --> 00:02:22,400
But you could make this a little more exciting by having the rotate rotation star at a different time

30
00:02:22,400 --> 00:02:24,930
period so they're all rotating differently.

31
00:02:24,950 --> 00:02:26,530
They will be rotating at the same speed.

32
00:02:26,540 --> 00:02:28,370
That's important and rotating at different speeds.

33
00:02:28,370 --> 00:02:37,100
But you might want to do delay the rotation on these by a couple of millisecond So they they are rotating

34
00:02:37,190 --> 00:02:41,280
at different angles or you could well you could also do which might even be better.

35
00:02:41,370 --> 00:02:46,550
Is rotate the object after you place it just rotate it at a certain position and then have it start

36
00:02:46,730 --> 00:02:49,920
from that position and it would look cool or two but we're not going to answer that you can play with

37
00:02:49,940 --> 00:02:51,200
our with that you get the point.

38
00:02:51,200 --> 00:02:53,450
It's not a very hard thing to do and will be a challenge for you.

39
00:02:53,450 --> 00:02:57,350
So we've got two or three objects here which is really awesome.

40
00:02:57,350 --> 00:03:06,330
So maybe the next thing to do is well let's think about this here we have these three objects here and

41
00:03:06,350 --> 00:03:10,190
I don't like the fact that the code we're copying and pasting those three times we may not be able to

42
00:03:10,190 --> 00:03:12,310
get around it but at least I think that could be a better place for that.

43
00:03:12,320 --> 00:03:17,480
We might want to refactor but I think maybe instead what we can do is the tap detection whenever items

44
00:03:17,480 --> 00:03:22,670
tapped we just detect it and then we grab its name and pass it on to the previous view controller.

45
00:03:22,670 --> 00:03:25,420
So let's work on that feature now because we know that we're going to need it.

46
00:03:25,490 --> 00:03:27,320
So here we go.

47
00:03:28,560 --> 00:03:31,430
Stop it it's our computer runs nice and fast.

48
00:03:31,530 --> 00:03:45,090
And here in our ramp picker VC let's just create the let's create a a gesture recognizer handler for

49
00:03:45,090 --> 00:03:47,730
when the screen is actually tapped.

50
00:03:48,750 --> 00:03:53,960
And we'll add a just a tap gesture to the whole scene and then we'll handle it accordingly.

51
00:03:53,970 --> 00:03:55,780
So let's create a function here.

52
00:03:55,890 --> 00:04:01,530
When you say at 0 BJC because we're going use a gesture recognizer which is going to use the selector

53
00:04:01,530 --> 00:04:06,730
syntax we have to make this an object of C runtime function meaning it's going to use Objective-C features.

54
00:04:06,900 --> 00:04:15,690
When you say func handle tap and windows can say gesture recognizer you just use recognizer and we're

55
00:04:15,690 --> 00:04:17,530
just putting the underscore here.

56
00:04:17,580 --> 00:04:23,370
So we don't have to give it a name and let's leave it empty for now and then what we're going to do

57
00:04:23,370 --> 00:04:26,600
is actually add a gesture recognizer to the scene itself.

58
00:04:26,610 --> 00:04:29,690
So we'll do it right here.

59
00:04:29,880 --> 00:04:39,060
We'll say let tat equals you tap gesture recognizer and the target is going to be self and the action

60
00:04:39,570 --> 00:04:42,680
is going to be selector handle tap

61
00:04:46,380 --> 00:04:53,010
and then all we have to do is scene view not add gesture recognizer and let's add the taps.

62
00:04:53,040 --> 00:04:59,340
Now whenever somebody taps this screen or the scene this scene gets seen.

63
00:04:59,400 --> 00:05:00,190
All we have to do.

64
00:05:00,200 --> 00:05:04,660
We're the scene view excuse me all we have to do is implement the code over here and handle tap.

65
00:05:04,740 --> 00:05:05,600
And what do we want to do.

66
00:05:05,610 --> 00:05:07,990
We want to detect which item was tapped.

67
00:05:08,280 --> 00:05:12,010
Or rather we want to even see if an item was tapped and if an item was tapped.

68
00:05:12,120 --> 00:05:17,390
What we want to do is get the name of that item and pass it back to the ramp.

69
00:05:17,400 --> 00:05:21,880
Placers So when you go to tap that screen and knows which ramp to to to place.

70
00:05:21,900 --> 00:05:22,580
OK.

71
00:05:23,100 --> 00:05:25,770
So let's get started.

72
00:05:25,860 --> 00:05:28,480
Let it kind of sounded like Lego Batman.

73
00:05:28,480 --> 00:05:31,310
I should be like Lego Batman too.

74
00:05:31,440 --> 00:05:32,890
Let's write some code.

75
00:05:33,360 --> 00:05:34,420
I'm an orphan.

76
00:05:34,800 --> 00:05:38,220
Let p equals an orphaned coater gesture.

77
00:05:38,380 --> 00:05:45,020
Recognize in fact I should record my whole video in the voice gesture recognizer that location.

78
00:05:45,330 --> 00:05:46,750
No not feeling it.

79
00:05:47,150 --> 00:05:47,540
All right.

80
00:05:47,700 --> 00:05:51,650
Well when you're talking to yourself for hours on end at a screen pretending that people are there he

81
00:05:51,670 --> 00:05:58,930
will talk in a Batman voice to let people gesture recognize their location in eye view.

82
00:05:59,190 --> 00:06:01,090
And so which view do you want the scene view.

83
00:06:01,260 --> 00:06:05,210
So we want to detect the location of the tap in the scene view.

84
00:06:05,220 --> 00:06:06,770
So it's going to find the location.

85
00:06:07,020 --> 00:06:12,240
And it's going to store it here so we can use it and get results we're going to see if it actually hit

86
00:06:12,240 --> 00:06:12,890
something.

87
00:06:13,020 --> 00:06:21,780
OK that's a scene view dot hit test and be very lucky that all of this code is written for us by Apple

88
00:06:21,780 --> 00:06:25,350
so we don't actually have to write great cast coding detection stuff.

89
00:06:25,350 --> 00:06:26,580
This makes it really easy.

90
00:06:26,670 --> 00:06:31,020
So we're going to pass and the point to see if something was hit and for the options we're going to

91
00:06:31,020 --> 00:06:38,490
go ahead and just put an empty dictionary here like so.

92
00:06:39,250 --> 00:06:39,900
OK.

93
00:06:39,910 --> 00:06:42,270
We don't need any options hit results.

94
00:06:42,310 --> 00:06:48,540
So we're going to see if anything was hit and if hit results count is greater than zero.

95
00:06:48,550 --> 00:06:50,160
So if we hit an object.

96
00:06:50,310 --> 00:06:53,520
OK this would have a result greater than zero.

97
00:06:53,530 --> 00:06:58,330
We'd have one or one or more object in our case we're looking for one.

98
00:06:58,390 --> 00:07:02,270
But what I'm saying is let's say we had five objects in the scene and we missed it.

99
00:07:02,290 --> 00:07:04,330
We're even seeing if the three we missed it.

100
00:07:04,360 --> 00:07:06,580
This is going to be zero and we hit nothing.

101
00:07:06,640 --> 00:07:09,630
So we're going to see if anything was hit.

102
00:07:09,880 --> 00:07:10,480
OK.

103
00:07:10,570 --> 00:07:14,860
And if something was hit we're just going to grab the node that was hit we can grab it right here and

104
00:07:14,860 --> 00:07:18,220
this is how you would do it with a game or Air care or anything when you're tapping the screen this

105
00:07:18,220 --> 00:07:19,670
is how you get a 3D object.

106
00:07:19,690 --> 00:07:21,230
And so you can reference it.

107
00:07:21,580 --> 00:07:26,870
And what we're going to do is we're going to say let no equals hit results.

108
00:07:27,180 --> 00:07:29,470
Zero note.

109
00:07:29,820 --> 00:07:30,680
OK.

110
00:07:30,780 --> 00:07:32,390
Where it is going to grab that node.

111
00:07:32,910 --> 00:07:34,200
No big deal.

112
00:07:34,830 --> 00:07:36,900
And then we're going to say switch.

113
00:07:37,350 --> 00:07:40,050
We're going to check now the name of the node.

114
00:07:40,290 --> 00:07:42,290
So switch no name.

115
00:07:42,570 --> 00:07:45,000
And we're going to forcefully unwrap this.

116
00:07:45,000 --> 00:07:49,620
And the reason why we're going to forcefully unwrap it is because we're developing we're still at compile

117
00:07:49,620 --> 00:07:52,390
time and the name of the node.

118
00:07:52,560 --> 00:07:53,380
OK.

119
00:07:53,640 --> 00:07:55,540
In our case will always be there.

120
00:07:55,770 --> 00:07:56,410
OK.

121
00:07:56,700 --> 00:08:01,340
And so at compile time we have to we have to force unwrap this here.

122
00:08:01,370 --> 00:08:05,630
We have to unwrap it here so we can know that it's there if it's not there or app won't even work.

123
00:08:05,730 --> 00:08:08,450
Now when would you not want to force and wrap this.

124
00:08:08,500 --> 00:08:08,950
OK.

125
00:08:09,090 --> 00:08:14,880
Well if you were let's say had a game and let's say the game had objects that were being deleted left

126
00:08:14,880 --> 00:08:17,960
and right like the lasers shooting aliens or something.

127
00:08:18,300 --> 00:08:21,540
And then you went to go check the no doubt name with a note that might not even be there could have

128
00:08:21,540 --> 00:08:22,230
been destroyed.

129
00:08:22,260 --> 00:08:24,910
Who knows that might not had a name at that point in time.

130
00:08:25,290 --> 00:08:30,480
And you could have a crash in that case you want to gracefully handle it with some some if let's regard

131
00:08:30,480 --> 00:08:31,180
statements.

132
00:08:31,260 --> 00:08:35,400
But in our case our app cannot function without this feature being there.

133
00:08:35,460 --> 00:08:39,810
So at compile time it will crash which is what we want we want it to crash.

134
00:08:39,810 --> 00:08:41,550
If it's if it's not there.

135
00:08:41,670 --> 00:08:43,420
So that's why I'm Force unwrapping it.

136
00:08:43,490 --> 00:08:48,270
So you'll hear me say don't ever force unwrap you know optionals or Apple crash.

137
00:08:48,270 --> 00:08:49,300
Well there are cases.

138
00:08:49,290 --> 00:08:53,010
Now you're becoming an advanced developer there are cases when it does not make sense to write a bunch

139
00:08:53,010 --> 00:08:59,960
of code for something that has to work no matter what and you understand more as time goes by as you

140
00:08:59,960 --> 00:09:05,100
program and swift So we're just checking the name of the notes so what we've done is we've tap the screen.

141
00:09:05,290 --> 00:09:08,180
OK we've gotten the location story here in P.

142
00:09:08,210 --> 00:09:12,590
This is the point or the location that was tapped in the screen and then what we do is we do a hit test

143
00:09:12,950 --> 00:09:17,060
when you pass a point into a hit test the sync it does all the math under the hood to see if you hit

144
00:09:17,060 --> 00:09:20,670
something and if it does OK the count will be greater than zero.

145
00:09:20,720 --> 00:09:21,870
The results that count.

146
00:09:22,130 --> 00:09:26,130
And then we grab the first element in there and we grab the note attached to it.

147
00:09:26,140 --> 00:09:30,830
There's other information attached with a hit but we're grabbing the node which so if we tapped the

148
00:09:30,830 --> 00:09:34,620
pyramid it would actually be grabbing the pyramid at this point in this node would be the pyramid.

149
00:09:34,970 --> 00:09:36,200
And then we're just checking the name.

150
00:09:36,200 --> 00:09:40,220
This is the identity that we set in the scene view when we're looking at her objects.

151
00:09:40,220 --> 00:09:41,880
The node name.

152
00:09:42,000 --> 00:09:43,830
And so I'm just doing a switch statement.

153
00:09:43,940 --> 00:09:49,720
We're going to say case if the case is a pipe What do we want to do.

154
00:09:49,950 --> 00:09:55,970
Well we want to basically tell the other view controller that we selected the pipe.

155
00:09:56,000 --> 00:09:58,440
We don't have code for that yet so we're just going to break.

156
00:09:58,820 --> 00:10:06,910
And if the case is quarter let's say pyramid pyramid we're going to break.

157
00:10:07,360 --> 00:10:17,330
And if the case is quarter we're going to break and then the default will just we'll just leave that

158
00:10:17,330 --> 00:10:18,550
empty for now.

159
00:10:23,920 --> 00:10:25,520
So this is looking good.

160
00:10:25,730 --> 00:10:28,740
It's yelling at us because we aren't doing anything here which is fine.

161
00:10:28,930 --> 00:10:39,730
But I want to we need to do now is have some way to let the other view controller know that we we selected

162
00:10:39,730 --> 00:10:46,870
something OK and that we we tap on something and then we want the other view controller to know that

163
00:10:47,440 --> 00:10:48,580
that's what we're going to do.

164
00:10:48,640 --> 00:10:51,290
We're going to grab that and place it on the screen.

165
00:10:51,340 --> 00:10:53,360
So there's two ways of doing this.

166
00:10:53,650 --> 00:10:58,320
We could do it with a protocol and become Adelia of the protocol.

167
00:10:58,330 --> 00:11:02,860
We could also hold a reference to the previous view controller as well.

168
00:11:02,980 --> 00:11:05,900
Either one would work just fine.

169
00:11:05,950 --> 00:11:08,460
And so let's just let's just keep it simple.

170
00:11:08,470 --> 00:11:09,600
First we can move to a protocol.

171
00:11:09,610 --> 00:11:14,720
We need to but let's just keep it simple and hold and hold a reference to the to the controller.

172
00:11:14,830 --> 00:11:17,910
So what I'm gonna do is appear at the top OK.

173
00:11:18,090 --> 00:11:21,840
We go back and fix this if we want a protocol's probably the best way going forward.

174
00:11:21,850 --> 00:11:25,910
But I think our app is simple enough where it doesn't need a protocol just yet.

175
00:11:26,180 --> 00:11:36,040
And I'm going to keep a weak bar ramp place or VC ramp place for ABC and the reason I'm going to make

176
00:11:36,040 --> 00:11:42,160
it week is because we we don't want to retain cycle is probably not going to happen but we don't want

177
00:11:42,160 --> 00:11:47,900
one we don't want ramp placer VC or excuse your rent picker received a reference from place to place

178
00:11:47,990 --> 00:11:53,040
reference this one with strong references because they will have a return cycle and have a memory leak.

179
00:11:53,170 --> 00:11:54,650
So we're making this week here.

180
00:11:54,730 --> 00:12:00,100
So rule of thumb if you don't understand this is if there are too few controllers referencing each other

181
00:12:00,160 --> 00:12:05,910
or to anything too if there are two objects ever referencing each other one of them needs to be weak

182
00:12:06,370 --> 00:12:11,740
or you will have a retain cycle possibly a memory leak which is which is no bueno.

183
00:12:11,740 --> 00:12:17,140
And if you need more information on it just do a research do some research on iOS retain cycles and

184
00:12:17,140 --> 00:12:19,010
you can learn more about it.

185
00:12:19,030 --> 00:12:20,380
So ramp service.

186
00:12:20,720 --> 00:12:28,730
OK what we want to do is when we're loading when we're loading the this view controller here we'll go

187
00:12:28,730 --> 00:12:29,610
ahead and set it.

188
00:12:29,630 --> 00:12:41,540
So what we'll do is here in our ramp place or see we will go find our pop over which is right here and

189
00:12:41,540 --> 00:12:49,450
then here's our ramp picker right and so what we can do is we can say ramp picker Visi ramp placer Visi

190
00:12:49,490 --> 00:12:50,520
equals self.

191
00:12:50,660 --> 00:13:00,050
OK we're just sending in this view controller over here to the ramp ramp placer Durham picker and we're

192
00:13:00,380 --> 00:13:01,860
setting the ramp place or other ramp picker.

193
00:13:01,880 --> 00:13:07,970
If you're not confuse yet and so then what we do need to do is we need to have a function here that

194
00:13:07,970 --> 00:13:10,400
we call from the ramp picker.

195
00:13:10,400 --> 00:13:21,360
When an item is actually selected and I'm just going to call this function on ramp selected and let's

196
00:13:21,360 --> 00:13:27,240
go ahead and just give it a rap name of type string.

197
00:13:27,720 --> 00:13:30,370
So on ramp selected.

198
00:13:30,510 --> 00:13:31,250
OK.

199
00:13:31,500 --> 00:13:33,180
So that's good.

200
00:13:33,190 --> 00:13:37,750
Let's just give it a let's give it an empty name there onramp selected.

201
00:13:37,770 --> 00:13:38,650
What do you want to do.

202
00:13:38,700 --> 00:13:40,390
Well that doesn't matter just yet.

203
00:13:40,410 --> 00:13:46,740
We just want to implement the function here so cool again if you're going to be doing a lot of back

204
00:13:46,740 --> 00:13:50,280
and forth between if you control you're going one has a protocol we're just doing one function here

205
00:13:50,280 --> 00:13:52,510
so I'm not overly concerned about it.

206
00:13:52,740 --> 00:13:54,450
And you can always refactor.

207
00:13:54,450 --> 00:13:56,360
So wrap picker visi.

208
00:13:56,370 --> 00:14:02,550
So what we're doing is in every single one of these here we're just going to pass in the name of the

209
00:14:02,550 --> 00:14:05,830
item that was selected so we can work with it.

210
00:14:05,850 --> 00:14:15,400
So it should be as simple as doing something like this ramp placer Visi dot on ramp selected in order

211
00:14:15,440 --> 00:14:20,910
to pass in the pipe and just literally do the same thing for each one just with a different object in

212
00:14:20,910 --> 00:14:25,680
there so cramped place or VCDs up on ramp selected.

213
00:14:26,370 --> 00:14:33,840
And we're going to pass and the name we're going to pass in.

214
00:14:33,840 --> 00:14:34,830
Pipe pyramid

215
00:14:37,590 --> 00:14:39,430
and then the quarter pipe.

216
00:14:39,870 --> 00:14:45,990
You know I'm wondering if we even need to do this at all on ramps like that we just pass and if we just

217
00:14:45,990 --> 00:14:51,540
pass in name without even doing a switch statement that would be weightless code so we could technically

218
00:14:51,540 --> 00:15:00,480
just say ramp place or AVC on ramp selected and let's just passing the node name and get rid of all

219
00:15:00,480 --> 00:15:04,680
this code that we just wrote make it more better.

220
00:15:05,910 --> 00:15:06,740
Yeah that'll work.

221
00:15:06,750 --> 00:15:10,440
And since we don't have anything else in the scene we only have three objects.

222
00:15:10,440 --> 00:15:12,290
We know it's only going be one of three names.

223
00:15:12,300 --> 00:15:15,440
And so I feel good I feel okay doing this putting this in here.

224
00:15:15,570 --> 00:15:19,590
And so yeah if there's is yeah we got a note here.

225
00:15:19,590 --> 00:15:20,540
This is great.

226
00:15:20,580 --> 00:15:21,940
I think this will work just fine.

227
00:15:21,960 --> 00:15:25,740
So we'll see if something's been tapped and if it's been tapped we just pass in the name of the object

228
00:15:26,460 --> 00:15:30,330
which will go over here to our ramp placer and then we can do what we want with it which is technically

229
00:15:30,330 --> 00:15:35,460
grabbing building that object again and then putting on the scene and maybe you're thinking it Mark

230
00:15:35,460 --> 00:15:43,180
why don't we just pass the whole note over to the ramp placer and use the same used the same object.

231
00:15:43,180 --> 00:15:49,090
Well we could but you know we've got an action running on a rotate action and we also the scale and

232
00:15:49,090 --> 00:15:53,140
the position are going to be off because it's different on the screen we need it full size now and I

233
00:15:53,140 --> 00:15:56,930
think it's cleaner to just recreate a new one.

234
00:15:56,950 --> 00:15:58,790
So I think this is great.

235
00:15:58,810 --> 00:16:05,710
We've got here is we've got a function that when we tap it it works well when it works actually let's

236
00:16:05,710 --> 00:16:06,720
test it here.

237
00:16:06,730 --> 00:16:16,320
Let's let's go ahead and just print and we're going to say no name and see if it actually works.

238
00:16:16,330 --> 00:16:17,050
How about that.

239
00:16:17,110 --> 00:16:20,160
Then we'll know if it's actually working OK.

240
00:16:20,460 --> 00:16:22,510
The secret button.

241
00:16:22,580 --> 00:16:23,690
So they're definitely spinning.

242
00:16:23,690 --> 00:16:33,410
So when I click these we should see names print out OK there does pipe pyramid quarter quarter quarter

243
00:16:33,950 --> 00:16:35,530
pyramid pipe.

244
00:16:35,570 --> 00:16:36,500
So it's definitely working.

245
00:16:36,500 --> 00:16:37,570
Our code is working.

246
00:16:37,640 --> 00:16:41,380
Look how easy it is to do hit detection with the scene.

247
00:16:41,480 --> 00:16:44,300
Did all the hard work for us under the hood and we just had to implement the code here.

248
00:16:44,300 --> 00:16:47,490
So really good stuff let's call this video done.

249
00:16:47,510 --> 00:16:52,490
And in the next exciting video what we'll do is we'll make it so when you select this it dismisses the

250
00:16:52,490 --> 00:16:58,250
view controller goes back to the other scene and then lets us place an object on the other scene.

251
00:16:58,280 --> 00:16:59,930
So that's it for now.

252
00:16:59,930 --> 00:17:02,470
Mark Price at death slopes Delcam moving on forward.

