-
Notifications
You must be signed in to change notification settings - Fork 149
/
6. Multitouch.srt
7812 lines (6238 loc) · 161 KB
/
6. Multitouch.srt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,401 --> 00:00:04,970
[MUSIC]
2
00:00:05,038 --> 00:00:09,174
Stanford University.
>> Okay,
3
00:00:09,243 --> 00:00:13,845
well welcome to Lecture 6,
Stanford CS193P,
4
00:00:13,913 --> 00:00:18,183
Fall of 2017. So today I'm
gonna continue that demo that
5
00:00:18,251 --> 00:00:21,620
I started last time. It's
gonna be gigantic demo today,
6
00:00:21,689 --> 00:00:24,790
covering mostly stuff having
to do with custom views.
7
00:00:24,859 --> 00:00:26,524
Then I come back
to the slides,
8
00:00:26,593 --> 00:00:28,826
just a few brief slides
on multi touch and
9
00:00:28,895 --> 00:00:32,664
how we do that. Then we'll go
back to the demo and add some
10
00:00:32,733 --> 00:00:36,568
multi touch gestures to our
little playing card thing.
11
00:00:36,636 --> 00:00:39,705
Here's the old slide of what's
you're gonna learn today,
12
00:00:39,773 --> 00:00:42,541
which you go back and look at
this slide after the demo.
13
00:00:42,609 --> 00:00:46,812
Then try to decide, did I
learn that, well, we will find
14
00:00:46,880 --> 00:00:50,048
out. By the way between last
lecture and this lecture, I
15
00:00:50,117 --> 00:00:53,318
went ahead and finished off a
custom string convertible for
16
00:00:53,386 --> 00:00:56,187
all three of these things.
I just made suits custom
17
00:00:56,256 --> 00:00:59,591
string,convertible return its
raw value, remember its raw
18
00:00:59,660 --> 00:01:02,928
values are these little equals
things here. And then rank,
19
00:01:02,996 --> 00:01:05,597
I had to actually implement
a little description,
20
00:01:05,666 --> 00:01:08,600
right there, where I returned
A for the one. And then
21
00:01:08,669 --> 00:01:12,871
a string version of a number,
or the kind J, Q, or K. But
22
00:01:12,940 --> 00:01:15,941
once I implemented custom
strings convertible on
23
00:01:16,009 --> 00:01:19,277
all three of these things.
And then this code we had back
24
00:01:19,346 --> 00:01:22,114
here where we just printed
out ten random cards,
25
00:01:22,182 --> 00:01:24,583
that prints out a lot
nicer on the console.
26
00:01:24,652 --> 00:01:26,885
So let's take a look and
see what it does now.
27
00:01:26,953 --> 00:01:29,587
See, it just prints it out
here as kind of an abbreviated
28
00:01:29,656 --> 00:01:32,757
version, which is, if you're
debugging, it's a lot nicer to
29
00:01:32,826 --> 00:01:34,926
be printing your cards out and
seeing that.
30
00:01:34,995 --> 00:01:38,530
And you might want to do the
same thing in your assignment
31
00:01:38,598 --> 00:01:42,734
number three as well.
So that's it for that.
32
00:01:42,803 --> 00:01:45,037
We've completely
finished our model for
33
00:01:45,105 --> 00:01:48,139
this MVC, that we're
building here, this app,
34
00:01:48,208 --> 00:01:51,176
this PlayingCard, so we have
a deck of playing cards.
35
00:01:51,245 --> 00:01:55,546
So now it's time to dive in to
drawing these, these cards.
36
00:01:55,615 --> 00:01:58,616
And we're gonna do that with a
custom UIView subclass, which
37
00:01:58,685 --> 00:02:01,853
is I'm gonna call PlayingCard
view. Now you create a custom
38
00:02:01,922 --> 00:02:04,589
view in the same way you
create other classes.
39
00:02:04,658 --> 00:02:07,859
So you're gonna do File > New
> File. But here instead of
40
00:02:07,927 --> 00:02:11,529
picking Swift File, which is
like a UI independent thing,
41
00:02:11,598 --> 00:02:16,801
you're gonna pick Cocoa Touch
Class. That's because our UI
42
00:02:16,870 --> 00:02:21,306
view is a subclass of a cocoa
touch or UI kit class.
43
00:02:21,374 --> 00:02:25,010
So I'm gonna call it,
playing, playing card view,
44
00:02:25,078 --> 00:02:28,980
it's gonna be subclass of
UIView. A lot of other UI kit
45
00:02:29,049 --> 00:02:33,718
things can be subclassed here,
but the one I want is UIView.
46
00:02:33,787 --> 00:02:35,821
And it says, where you
wanna put it? By the way,
47
00:02:35,889 --> 00:02:38,890
I just wanna remind you all,
some of you are putting your
48
00:02:38,959 --> 00:02:41,960
files at the top level,
the project level, so they're
49
00:02:42,028 --> 00:02:44,962
ending up like next to your
X code project right there.
50
00:02:45,031 --> 00:02:47,799
You really wanna be putting
them down a level in here.
51
00:02:47,868 --> 00:02:50,802
This is where we collect
all of our classes. So just
52
00:02:50,871 --> 00:02:53,505
a little reminder there, we're
seeing that on the homework.
53
00:02:53,574 --> 00:02:56,408
And so here's my UIView
subclass, look at this, see?
54
00:02:56,476 --> 00:02:58,576
Subclass of UIView,
that's great.
55
00:02:58,645 --> 00:03:01,913
And it even gave me a stub of
a very important method here,
56
00:03:01,982 --> 00:03:05,450
which of course, is our draw
rect. Now, you notice this is
57
00:03:05,519 --> 00:03:08,853
commented out, in this stub,
that's because this iOS
58
00:03:08,922 --> 00:03:11,656
actually looks to see if
you have a draw rect.
59
00:03:11,725 --> 00:03:14,025
And if you do, it makes
an off screen buffer for
60
00:03:14,094 --> 00:03:16,694
you, and all kinds of
preparations for you to draw,
61
00:03:16,763 --> 00:03:19,163
okay, and that's not cheap,
it's not free.
62
00:03:19,232 --> 00:03:21,766
So if you don't actually
draw in your draw rect,
63
00:03:21,835 --> 00:03:24,236
then you would want to
leave it commented out.
64
00:03:24,304 --> 00:03:27,839
Now why would you ever have
a UIView, or UIView subclass
65
00:03:27,908 --> 00:03:31,443
that doesn't have a draw rect?
Well, that's actually quite
66
00:03:31,512 --> 00:03:33,878
common, you do all your
drawing with subviews,
67
00:03:33,947 --> 00:03:37,182
consider UI stack view, right?
It's a UIView, it does all its
68
00:03:37,250 --> 00:03:39,851
drawing with views that
are stacked inside of it.
69
00:03:39,920 --> 00:03:43,054
It doesn't do any actual
drawing itself, it has no draw
70
00:03:43,123 --> 00:03:46,124
rect, right? But we are gonna
have a draw rect, of course,
71
00:03:46,192 --> 00:03:48,559
because we are going to be
drawing a playing card.
72
00:03:48,628 --> 00:03:51,830
Now I'm actually just,
for example purposes here,
73
00:03:51,898 --> 00:03:54,765
I'm gonna draw some of my
card with sub views and
74
00:03:54,834 --> 00:03:58,202
some of my card with this draw
rect, okay. So that way you'll
75
00:03:58,271 --> 00:04:00,705
get to see one view that
actually does both. And
76
00:04:00,774 --> 00:04:03,341
in your homework assignment,
you're probably gonna have at
77
00:04:03,410 --> 00:04:04,976
least one view that
does subviews, and
78
00:04:05,045 --> 00:04:07,078
at least one view that
has a draw rect. So
79
00:04:07,146 --> 00:04:11,016
you'll be able to see all that
at action, in action here.
80
00:04:11,084 --> 00:04:14,085
All right, so we got this
PlayingCardView. Let's go back
81
00:04:14,154 --> 00:04:17,889
over to our storyboard right
here, and put a UI view,
82
00:04:17,958 --> 00:04:21,626
a PlayingCardView basically,
into our UI, okay.
83
00:04:21,695 --> 00:04:25,096
So how do we do that? Well,
how do we put views in our UI?
84
00:04:25,165 --> 00:04:28,032
We go over here to utilities
and down at the bottom,
85
00:04:28,101 --> 00:04:30,735
maybe we drag out a button,
or we drag a label.
86
00:04:30,804 --> 00:04:34,105
And of course where is playing
card view? Well, it's not in
87
00:04:34,174 --> 00:04:36,775
here, of course, cuz these are
all just the things that come
88
00:04:36,843 --> 00:04:40,579
with X-code. But I can drag
out towards the bottom here,
89
00:04:40,647 --> 00:04:43,915
this guy, View,
which is a generic UI view.
90
00:04:43,984 --> 00:04:47,051
So I drag him out here and
his class or
91
00:04:47,120 --> 00:04:51,055
his type is just UI view. I'm
going to make my background
92
00:04:51,124 --> 00:04:54,259
a different color so we can
see him a little better there.
93
00:04:54,327 --> 00:04:56,862
So I'm just gonna select my
background and change it to,
94
00:04:56,930 --> 00:04:59,097
oop, orange, I love orange,
there's orange.
95
00:04:59,166 --> 00:05:01,532
San Francisco Giants
colors right there.
96
00:05:01,601 --> 00:05:04,002
So here's my kind
of generic UI view.
97
00:05:04,071 --> 00:05:06,471
And I don't want this to
be a generic UI view,
98
00:05:06,540 --> 00:05:09,640
I want it to be a playing card
view, okay, cuz that's what
99
00:05:09,709 --> 00:05:11,976
I've been working on.
And the way we do that is
100
00:05:12,045 --> 00:05:13,945
with the different
inspector on the right.
101
00:05:14,013 --> 00:05:16,414
You see we've been using
this inspector right here,
102
00:05:16,482 --> 00:05:19,384
the attributes inspector.
Right next door to it is this
103
00:05:19,453 --> 00:05:21,720
guy. This is
the identity inspector,
104
00:05:21,789 --> 00:05:24,623
it inspects the identity
of the selected thing.
105
00:05:24,691 --> 00:05:28,393
So here I have a view selected
and it's of type UI view,
106
00:05:28,461 --> 00:05:32,230
you see the class? But I can
go here and change it to be
107
00:05:32,298 --> 00:05:36,067
a playing card view. So now
this is a playing card view,
108
00:05:36,136 --> 00:05:39,904
and any time the system needs
to draw it, it's gonna use our
109
00:05:39,973 --> 00:05:44,676
draw(rect) right here. It's
a code that we've written.
110
00:05:44,744 --> 00:05:47,845
So that's awesome. Now I'm
gonna do a little bit of auto
111
00:05:47,914 --> 00:05:50,915
layout here that you've seen
before. So this is nothing
112
00:05:50,984 --> 00:05:53,985
new, but I'm just gonna put
this up in the edge here,
113
00:05:54,053 --> 00:05:56,955
put this one down here and
I'm gonna pin it to the edges.
114
00:05:57,024 --> 00:06:00,057
So my PlayingCard is
gonna be kind of tall and
115
00:06:00,126 --> 00:06:04,229
thin in portrait mode and kind
of short and wide in landscape
116
00:06:04,298 --> 00:06:07,865
mode, but that's okay,
we'll fix that later. So
117
00:06:07,934 --> 00:06:10,868
I'm just gonna drag up to the
corner and set my leading and
118
00:06:10,937 --> 00:06:14,272
top spaces to be pinned. And
I'm gonna drag Ctrl+drag down
119
00:06:14,341 --> 00:06:16,975
to this corner and
set my trailing and bottom.
120
00:06:17,044 --> 00:06:19,544
So they'd start there,
so now if I go and
121
00:06:19,613 --> 00:06:22,914
go into landscape mode right
here, you can see that it
122
00:06:22,983 --> 00:06:26,251
pins to the edges, so
I have this funny shape. Now,
123
00:06:26,319 --> 00:06:29,654
I'm doing this mostly at the
start here because I want to
124
00:06:29,723 --> 00:06:33,424
show what happens inside your
view when your bounds change.
125
00:06:33,493 --> 00:06:36,527
Because here when we rotate,
our bounds are gonna be
126
00:06:36,596 --> 00:06:40,065
changing very dramatically,
from tall and thin to wide and
127
00:06:40,133 --> 00:06:42,800
short. So before we dive
into doing a playing card,
128
00:06:42,869 --> 00:06:44,969
I'm just gonna do
a little bit of drawing,
129
00:06:45,038 --> 00:06:47,472
show you how drawing works
with core graphics and
130
00:06:47,540 --> 00:06:50,041
UI bezier path like we
talked about in lecture. So
131
00:06:50,110 --> 00:06:53,344
lets first draw a circle, just
a circle in the middle of our
132
00:06:53,413 --> 00:06:57,281
view using core graphics, and
see what that code looks like.
133
00:06:57,350 --> 00:07:00,318
So in core graphics, we
always get the context first.
134
00:07:00,387 --> 00:07:03,855
So we can't draw in core
graphics without a context and
135
00:07:03,924 --> 00:07:06,391
we get that in our
drawrect by doing this
136
00:07:06,459 --> 00:07:10,562
UIGraphicsGetCurrentContext.
Now, this could return nil,
137
00:07:10,631 --> 00:07:14,031
that's why we do if-let, but
it will never return nil
138
00:07:14,100 --> 00:07:16,567
inside your drawrect.
Okay, it might turn,
139
00:07:16,636 --> 00:07:19,170
return nil in other contexts,
but in this environment,
140
00:07:19,239 --> 00:07:20,471
it's always gonna return,
141
00:07:20,540 --> 00:07:23,040
but we're still gonna do
if-let right there. We could
142
00:07:23,109 --> 00:07:25,576
do exclamation point where
we're just gonna do if-let.
143
00:07:25,645 --> 00:07:29,447
So now that I have a context,
now I can tell the context to
144
00:07:29,516 --> 00:07:33,184
do certain things, move to,
okay, I can do move to.
145
00:07:33,252 --> 00:07:37,422
I can do add line to,
things like that. Add curve,
146
00:07:37,491 --> 00:07:40,625
I can add these things that
basically are drawing a path,
147
00:07:40,694 --> 00:07:43,461
right, like a line moving
around. So I'm gonna
148
00:07:43,529 --> 00:07:47,565
make a circle. So I'm gonna
use one called addArc.
149
00:07:47,634 --> 00:07:51,670
An addArc is kinda cool,
it just like takes a point and
150
00:07:51,738 --> 00:07:55,240
then circumscribes a big
arc around a circle.
151
00:07:55,308 --> 00:07:57,876
And I'm just gonna use that
to go all the way around and
152
00:07:57,944 --> 00:08:01,079
create a circle. So when
addArc is creating a path,
153
00:08:01,148 --> 00:08:03,581
it wants to know what's
the center of this circular
154
00:08:03,650 --> 00:08:04,982
path that you're going on.
155
00:08:05,051 --> 00:08:08,286
And I'm gonna make it be the
center of my drawing area. And
156
00:08:08,355 --> 00:08:12,924
what rectangle specifies my
drawing area? Bounds, okay, my
157
00:08:12,992 --> 00:08:16,460
var bounds does that. So I'm
gonna create a CGPoint here,
158
00:08:16,529 --> 00:08:19,531
which, whose x coordinate
is my bounds midpoint.
159
00:08:19,599 --> 00:08:22,900
And I'm gonna create the y
coordinate is my bounds
160
00:08:22,969 --> 00:08:26,204
midpoint in y. So I'm
specifying right in the center
161
00:08:26,273 --> 00:08:29,240
of my drawing area, which
is my bounds. The radius,
162
00:08:29,309 --> 00:08:33,111
I'm just gonna do 100 points,
nice big circle.
163
00:08:33,179 --> 00:08:36,414
The start angle and
end angle here are in radians,
164
00:08:36,482 --> 00:08:40,452
not degrees, not 0 to 360,
it's radians, 0 to 2 pi.
165
00:08:40,520 --> 00:08:43,421
Does everyone know radian?
If you know what radians are.
166
00:08:43,489 --> 00:08:46,958
Okay, everybody, great, so
0 to 2 pi. And 0, by the way,
167
00:08:47,027 --> 00:08:50,228
is off to the right.
0 is not straight up,
168
00:08:50,297 --> 00:08:52,163
as you might imagine,
it is off to the right.
169
00:08:52,232 --> 00:08:53,664
So I'm gonna start
off to the right and
170
00:08:53,733 --> 00:08:55,233
I'm gonna go all the way
around my circle.
171
00:08:55,302 --> 00:08:56,867
I can go either clockwise or
counterclockwise,
172
00:08:56,936 --> 00:08:58,869
it doesn't matter cuz I'm
going all the way around. So
173
00:08:58,938 --> 00:09:01,406
how do I go around?
Well, that's 2 times pi. And
174
00:09:01,474 --> 00:09:06,477
there's a really nice,
little constant here,
175
00:09:06,546 --> 00:09:10,448
CGFloat.pi. Okay, and that's
how I can get pi in a CG,
176
00:09:10,517 --> 00:09:12,217
as a CGFloat.
And I can go clockwise or
177
00:09:12,285 --> 00:09:16,354
counterclockwise, it doesn't
matter. All right, so
178
00:09:16,423 --> 00:09:18,790
now I've created some path,
some drawing here.
179
00:09:18,858 --> 00:09:21,526
So I can do other things
in my context like,
180
00:09:21,595 --> 00:09:25,230
I can set the LineWidth, for
example, not the LineCap, but
181
00:09:25,298 --> 00:09:28,433
the LineWidth, 5 points wide.
That's a reasonably thick,
182
00:09:28,502 --> 00:09:32,136
not super thick, but
reasonably. I, of course,
183
00:09:32,205 --> 00:09:35,440
can set the colors I wanna
draw with using these static
184
00:09:35,508 --> 00:09:39,811
vars in UIColor. Like let's
say, green for our setFill.
185
00:09:39,879 --> 00:09:42,680
That's our favorite fill
color. And UIColor.red for
186
00:09:42,748 --> 00:09:47,018
our stroke color. Okay, so
I can set whatever colors. And
187
00:09:47,087 --> 00:09:52,524
then I can ask the context for
example, to stroke the path.
188
00:09:52,592 --> 00:09:56,928
So let's do strokePath here.
And you'd think I could then
189
00:09:56,996 --> 00:10:01,166
say context.fillPath. Let's
see if this will stroke and
190
00:10:01,234 --> 00:10:04,401
fill, and it won't. And the
reason for that is that when
191
00:10:04,470 --> 00:10:07,972
we draw in a context, it's
actually slightly different
192
00:10:08,040 --> 00:10:10,875
than using that UIBezierPath I
showed you in the slides.
193
00:10:10,943 --> 00:10:14,879
In the context, when we do
a strokePath like this,
194
00:10:14,947 --> 00:10:19,483
it consumes the path. Okay,
it uses up the path. And so
195
00:10:19,552 --> 00:10:22,153
when we do the fillPath on the
next line, there's no path.
196
00:10:22,222 --> 00:10:24,355
We'd have to start again. So
197
00:10:24,424 --> 00:10:26,790
that's one of the big
advantages of UIBezierPath. So
198
00:10:26,859 --> 00:10:30,862
let's do this exact same thing
here, but using UIBezierPath,
199
00:10:30,931 --> 00:10:35,032
all right? So I'm gonna say
let path = UIBezierPath.
200
00:10:35,101 --> 00:10:36,400
We'll start with an empty one.
201
00:10:36,469 --> 00:10:38,903
It had, I'll show you later
how to create a BezierPath to