-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy path12. Drag and Drop, Table View, Collection View, and Text Field.srt
6866 lines (5483 loc) · 142 KB
/
12. Drag and Drop, Table View, Collection View, and Text Field.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,937
[MUSIC]
2
00:00:05,005 --> 00:00:07,439
Stanford University.
>> Okay, well,
3
00:00:07,508 --> 00:00:12,177
welcome to Lecture number
12 of Stanford CS193P,
4
00:00:12,246 --> 00:00:16,481
Fall of 2017. And today,
we're gonna continue the demo
5
00:00:16,550 --> 00:00:19,251
we started with Emoji Art and
we're gonna,
6
00:00:19,320 --> 00:00:22,787
we learned a little bit about
table view in the last one.
7
00:00:22,856 --> 00:00:25,791
This time, we're gonna learn
more about collection view and
8
00:00:25,860 --> 00:00:28,293
also learn a little bit
how drag and drop, and
9
00:00:28,362 --> 00:00:31,229
collection view and table view
all go together. And then I'm
10
00:00:31,298 --> 00:00:33,498
gonna go back to the slides
and talk about TextField.
11
00:00:33,567 --> 00:00:36,468
And I originally had planned
to do a demo in Emoji Art,
12
00:00:36,537 --> 00:00:38,437
of adding a TextField
in there, but
13
00:00:38,505 --> 00:00:41,673
we also have to finish early
today, because there's some
14
00:00:41,742 --> 00:00:44,609
huge midterm, in this
room right after us, and
15
00:00:44,678 --> 00:00:46,911
we have to have time
to clear out of here.
16
00:00:46,980 --> 00:00:51,082
So, I will probably do that
demo first thing, next Monday,
17
00:00:51,151 --> 00:00:54,653
or I might just skip that
demo, we'll see how it goes.
18
00:00:55,856 --> 00:00:57,489
All right, so
back to Emoji Art here.
19
00:00:57,558 --> 00:01:01,359
What we're gonna do is make
first I'm gonna make Emoji Art
20
00:01:01,428 --> 00:01:04,796
scrollable, because we didn't,
it just currently,
21
00:01:04,865 --> 00:01:08,333
it's fixed. Then we're gonna
add a collectionView along
22
00:01:08,401 --> 00:01:10,134
the top there,
you remember it from the demo,
23
00:01:10,203 --> 00:01:13,205
where we're gonna be able to
choose our emoji from. And
24
00:01:13,273 --> 00:01:16,908
then we're gonna add some
drag and drop from that
25
00:01:16,977 --> 00:01:20,912
collectionView, both into our
view and into other apps, and
26
00:01:20,981 --> 00:01:26,218
vice versa, etc.
So let's, actually,
27
00:01:26,287 --> 00:01:31,589
let's take a quick look at
what our app looks like so
28
00:01:31,658 --> 00:01:36,628
far, okay, and it's
basically working nicely and
29
00:01:36,697 --> 00:01:41,833
that we can pick up images.
So I pick up an image here and
30
00:01:41,902 --> 00:01:44,869
drag it in and
drop it to change it.
31
00:01:44,938 --> 00:01:47,739
And of course, we have our
table view on the left for
32
00:01:47,808 --> 00:01:50,242
choosing documents and
adding documents.
33
00:01:50,310 --> 00:01:52,677
Of course, we haven't hooked
our table view up, and
34
00:01:52,746 --> 00:01:55,847
that's okay, but because we're
gonna do that next week. But
35
00:01:55,916 --> 00:01:58,750
one thing I wanna have you
note about this table view
36
00:01:58,819 --> 00:02:02,654
that's kind of annoying is
that I can't get rid of it.
37
00:02:02,722 --> 00:02:05,157
See this table view right
here? You can't swipe it away,
38
00:02:05,226 --> 00:02:07,993
and that's because
essentially, this app thinks
39
00:02:08,062 --> 00:02:10,929
it's in landscape mode on
an iPad, and so of course,
40
00:02:10,998 --> 00:02:14,098
this master and the detail to
splitView are side by side.
41
00:02:14,167 --> 00:02:17,436
But, since I'm in
multi-tasking mode right here,
42
00:02:17,504 --> 00:02:19,738
so much of my
screen is taken up,
43
00:02:19,806 --> 00:02:21,973
with Safari over
here on the right,
44
00:02:22,041 --> 00:02:26,211
that it's annoying because I
really wanna focusing on my
45
00:02:26,279 --> 00:02:29,581
Emoji Art document. So I'm
gonna talk to you a little bit
46
00:02:29,649 --> 00:02:32,550
how to make it so that that
splitView on the left can be
47
00:02:32,619 --> 00:02:35,887
slid out of the way, even in
splitView and landscape mode.
48
00:02:35,955 --> 00:02:39,024
So that's the first little
thing we're gonna do. So let's
49
00:02:39,092 --> 00:02:42,661
hop on over to our code over
here and see how we do that.
50
00:02:42,730 --> 00:02:46,131
It's quite easy, actually,
splitView has a var,
51
00:02:46,200 --> 00:02:49,634
a property in it called
preferred display mode, and
52
00:02:49,703 --> 00:02:54,139
you can control what happens
with the master through that.
53
00:02:54,208 --> 00:02:56,341
And, it's just that the
default here is not what we
54
00:02:56,409 --> 00:02:59,444
want. Now the kinda
kooky thing about that,
55
00:02:59,512 --> 00:03:04,016
I'm gonna set this var in my
master code. So I'm gonna go
56
00:03:04,084 --> 00:03:07,486
to my emojiArtDocument
TableViewController, right?
57
00:03:07,554 --> 00:03:10,088
That's the TableViewController
with the one, two, three,
58
00:03:10,157 --> 00:03:11,990
the thing that we want
to slide out. And
59
00:03:12,058 --> 00:03:14,626
what I'm gonna set it is
kind of an unusual place.
60
00:03:14,695 --> 00:03:18,463
I'm gonna set it in
viewWillLayoutSubviews. And
61
00:03:18,532 --> 00:03:20,531
you might be like, whoa,
viewWillLayoutSubviews,
62
00:03:20,600 --> 00:03:22,801
that seems like a weird
place to set that. Well,
63
00:03:22,869 --> 00:03:25,770
the reason I'm gonna set it
there is cuz when the layout
64
00:03:25,839 --> 00:03:28,907
changes, of a splitView,
it often resets that preferred
65
00:03:28,976 --> 00:03:32,577
mode. So if I wanna keep
enforcing this thing of it,
66
00:03:32,646 --> 00:03:35,814
the master being in slide
on top mode, I have to keep
67
00:03:35,883 --> 00:03:38,950
telling it to do that, but
I also have to be careful
68
00:03:39,018 --> 00:03:42,587
because setting that preferred
mode can cause it to relay
69
00:03:42,656 --> 00:03:44,989
out. So I don't wanna end up
in an infinite loop here,
70
00:03:45,058 --> 00:03:46,458
right, where in
LayoutSubViews,
71
00:03:46,526 --> 00:03:48,760
I'm setting preferred mode,
it's causing a layout,
72
00:03:48,828 --> 00:03:52,697
I'm coming back in. So I'm
gonna do this by saying, if my
73
00:03:52,766 --> 00:03:56,034
splitViewController?.prefer-
red,
74
00:03:56,103 --> 00:03:59,671
to call preferredDisplayMode
!= the one I
75
00:03:59,740 --> 00:04:03,008
want. And you can see
there's quite a few here,
76
00:04:03,077 --> 00:04:06,311
there's primaryOverlay,
there's primaryHidden,
77
00:04:06,380 --> 00:04:09,681
there's Automatic,
there's alwaysShowBoth, etc.
78
00:04:09,750 --> 00:04:13,318
But the one I want is
primaryOverlay, so if it's not
79
00:04:13,387 --> 00:04:18,156
primaryOverlay, then I'm gonna
set my splitViewControllers,
80
00:04:18,225 --> 00:04:22,394
preferredDisplayMode =
.primaryOverlay. And so
81
00:04:22,463 --> 00:04:25,897
that'll keep enforcing
it every time, even if,
82
00:04:25,965 --> 00:04:28,300
every time we rotate or
whatever,
83
00:04:28,368 --> 00:04:32,303
I'm gonna keep forcing this
cuz I always want that, this
84
00:04:32,372 --> 00:04:36,408
table view to be appearing
on top of my emoji art view.
85
00:04:36,477 --> 00:04:40,211
But I also wanna be able
to swipe it out of the way,
86
00:04:40,280 --> 00:04:42,614
I just swiped it
out of the way.
87
00:04:42,682 --> 00:04:46,785
Wait, you can't see, hold on,
I'll show you. Let me hide
88
00:04:46,853 --> 00:04:50,522
here, Hide Others, okay, so
here, I'm swiping it in, and
89
00:04:50,591 --> 00:04:53,624
then I'm swiping it away.
Swiping it in, swiping it
90
00:04:53,693 --> 00:04:56,828
away, and I always want it to
be that way because it's very
91
00:04:56,897 --> 00:04:59,830
unimportant to access this
table. I just go in here,
92
00:04:59,899 --> 00:05:03,234
pick the document I want, now
that the document's in there,
93
00:05:03,303 --> 00:05:05,570
I wanna be having as
much room to you know,
94
00:05:05,639 --> 00:05:08,473
to drag my stuff in and
work here as possible.
95
00:05:08,541 --> 00:05:12,110
Do you understand that
primary overlay thing?
96
00:05:12,179 --> 00:05:14,312
And I'm gonna ask you to do
that in your homework, just so
97
00:05:14,381 --> 00:05:18,082
you kinda see, what it's
all about there. All right,
98
00:05:18,151 --> 00:05:21,686
the next thing I wanna
do is to make it so
99
00:05:21,755 --> 00:05:24,956
that my EmojiArtView
is scrollable and
100
00:05:25,024 --> 00:05:29,026
zoomable, right, cuz right
now, it's just a fixed image.
101
00:05:29,095 --> 00:05:30,262
And we already know
how to do that, so
102
00:05:30,330 --> 00:05:32,830
I'm not gonna waste too much
time showing you how to make
103
00:05:32,899 --> 00:05:35,800
it scrollable, I'm gonna do it
quickly. I'm gonna have some
104
00:05:35,869 --> 00:05:39,170
precanned code to do it, but
it's nothing new, it's nothing
105
00:05:39,239 --> 00:05:42,140
that you haven't seen before,
in our previous demos.
106
00:05:42,208 --> 00:05:44,576
So I'm gonna go back to my
storyboard, here it is,
107
00:05:44,645 --> 00:05:48,346
right here. I'm gonna go down
to this viewController right
108
00:05:48,415 --> 00:05:51,750
here, and instead of,
right now, what we have,
109
00:05:51,819 --> 00:05:54,953
just to remind you,
is we have this drop zone.
110
00:05:55,021 --> 00:05:57,222
And inside the drop zone,
we just put our EmojiArtView.
111
00:05:57,290 --> 00:05:59,925
That's why it's not scrollable
right there. So I'm gonna
112
00:05:59,993 --> 00:06:03,427
delete it, and instead I'm
gonna put a scrollView there,
113
00:06:03,496 --> 00:06:06,631
so I'm gonna go down here and
search for a scrollView,
114
00:06:06,700 --> 00:06:09,667
here it is. I'm gonna drag the
scrollView in here, I'm gonna
115
00:06:09,736 --> 00:06:12,070
pin the the scrollView to
the edges of the drop zone,
116
00:06:12,139 --> 00:06:13,605
of course. So I'll go here and
117
00:06:13,674 --> 00:06:16,241
just pin it right up to the
edges. We know how to do that
118
00:06:16,309 --> 00:06:18,809
with this little pin thing,
bloop, it spins out, that's
119
00:06:18,878 --> 00:06:23,048
great. Now I'm going to create
an outlet to this scrollView,
120
00:06:23,116 --> 00:06:26,884
put it up, where are we gonna
put it? Let's put it right
121
00:06:26,953 --> 00:06:29,754
under the drop zone, in here.
So I'll Ctrl+drag,
122
00:06:29,823 --> 00:06:34,993
create that scrollView.
Now we know that when we
123
00:06:35,062 --> 00:06:39,064
have a scrollView outlet like
this, by the way, down here,
124
00:06:39,132 --> 00:06:42,767
this emojiArtView is no longer
gonna be an outlet. Okay,
125
00:06:42,836 --> 00:06:45,337
it's just gonna be
a regular var and
126
00:06:45,405 --> 00:06:49,006
I'll just set it = creating
one. This is exactly what we
127
00:06:49,075 --> 00:06:52,276
did with our image view in our
other demo. So we haven't done
128
00:06:52,345 --> 00:06:55,780
anything new here. And when we
have a scrollView like this,
129
00:06:55,849 --> 00:06:57,815
we remember we've got to set
its minimum scroll view,
130
00:06:57,884 --> 00:06:59,817
maximum.
We've gotta set the delegate,
131
00:06:59,886 --> 00:07:01,419
we've gotta implement
that view for zoom.
132
00:07:01,487 --> 00:07:04,188
Remember all that? So I'm
gonna do all that really quick
133
00:07:04,257 --> 00:07:08,259
here. Trying to remember what
I called, yeah, emojiscroll.
134
00:07:08,328 --> 00:07:11,029
So here it is, here's all that
code. I just pop it in there.
135
00:07:11,098 --> 00:07:14,932
In fact, let me go over
here and show it in big, so
136
00:07:15,001 --> 00:07:16,701
you can see it. So
here's the ScrollView Outlet.
137
00:07:16,770 --> 00:07:20,037
So I just added a didSet
to it, to set those things,
138
00:07:20,106 --> 00:07:22,173
right, the delegate. And
then I added it as a subview.
139
00:07:22,241 --> 00:07:23,875
Here's viewForZooming.
140
00:07:23,943 --> 00:07:26,378
We're obviously zooming
in our emojiArtView.
141
00:07:26,446 --> 00:07:29,180
Then I added a var here,
emojiArt background image,
142
00:07:29,249 --> 00:07:31,816
which does the stuff we need
to do like setting our content
143
00:07:31,885 --> 00:07:33,918
size equal to the frame.
144
00:07:33,987 --> 00:07:36,454
Right, remember we want that.
We always want that frame of
145
00:07:36,523 --> 00:07:39,023
our image view to the same
as our content size.
146
00:07:39,092 --> 00:07:41,025
I put a little bit of zooming
code in here you can go
147
00:07:41,094 --> 00:07:42,360
look at later,
which is kind of fun,
148
00:07:42,429 --> 00:07:46,364
to give it an initial zoom
that's good. And then,
149
00:07:46,433 --> 00:07:48,700
of course, we need to go
down here and use this.
150
00:07:48,769 --> 00:07:51,503
So instead of just setting the
emoji art, we use background
151
00:07:51,571 --> 00:07:54,839
image directly, I'm gonna use
this emoji art background
152
00:07:54,908 --> 00:07:59,811
image var that I just created.
That's this var right here.
153
00:08:01,114 --> 00:08:03,448
Hey everybody. See everything
I did right there?
154
00:08:03,517 --> 00:08:06,784
And of course it's complaining
that I'm not a UI split view
155
00:08:06,853 --> 00:08:09,954
or UI scroll view delegate
by the way, so I'll do that.
156
00:08:10,023 --> 00:08:14,592
UI scroll view delegate,
now I implement that. So
157
00:08:14,661 --> 00:08:19,397
let's make sure this all
works. So here I am in my app,
158
00:08:19,466 --> 00:08:21,599
I'm just gonna swipe that
thing out of the way.
159
00:08:21,668 --> 00:08:23,968
I'm gonna grab an image
here and drag and
160
00:08:24,036 --> 00:08:28,072
drop it in. Here it is and
hopefully I can scroll. Whew!
161
00:08:28,141 --> 00:08:31,809
I can I can scroll way
in scroll pan around
162
00:08:31,878 --> 00:08:35,413
scroll in but you know what?
That looks incredibly ugly
163
00:08:35,482 --> 00:08:37,782
to have that thing in
the upper left. Like that,
164
00:08:37,850 --> 00:08:40,185
if I really were working
on this thing, and I have
165
00:08:40,253 --> 00:08:41,385
to all my work in
the upper left,
166
00:08:41,454 --> 00:08:43,654
that would be absolutely
ridiculous. So
167
00:08:43,723 --> 00:08:46,824
what I really want is, I want
this thing to stay centered.
168
00:08:46,893 --> 00:08:49,693
As I'm zooming in and out,
when I'm zoomed out here.
169
00:08:49,762 --> 00:08:52,697
And when I zoom in, I want to
say in the center. Not jump up
170
00:08:52,766 --> 00:08:55,767
there to the left like that.
So how am I going to do that?
171
00:08:55,836 --> 00:08:58,769
Well, there's a lot of ways to
do that, actually. I'm gonna
172
00:08:58,838 --> 00:09:01,272
do it in a simple way that
lets me teach your about how
173
00:09:01,340 --> 00:09:05,377
to control the constraints in
your storyboard from code.
174
00:09:07,047 --> 00:09:09,247
That's what this little thing
is all about that I'm gonna
175
00:09:09,315 --> 00:09:12,117
show you here. So what I'm
gonna do is I'm gonna go back
176
00:09:12,185 --> 00:09:16,954
to my storyboard.
And I'm going to
177
00:09:17,023 --> 00:09:22,093
have this scroll view right
here always be centered.
178
00:09:22,162 --> 00:09:24,028
So I'm going to keep this
scroll view in the center,
179
00:09:24,097 --> 00:09:26,498
now, normally that's already
happening, because I'm pinned
180
00:09:26,566 --> 00:09:29,500
to the edges. So of course
it's in the center, but
181
00:09:29,569 --> 00:09:33,370
what I'm gonna do then is have
the scroll view's width and
182
00:09:33,439 --> 00:09:37,008
height stick to its content
size. So if you zoom in so
183
00:09:37,077 --> 00:09:38,843
that the content area
gets really small,
184
00:09:38,912 --> 00:09:40,945
the scroll view is gonna get
small and it's gonna stay
185
00:09:41,014 --> 00:09:42,947
centered. So that's how
we're gonna do this, that's
186
00:09:43,016 --> 00:09:46,251
the trick. So how we gonna do
that? Well, of course we need
187
00:09:46,319 --> 00:09:48,553
to make it to our scroll view
right here stays centered.
188
00:09:48,622 --> 00:09:51,789
So I'm gonna click down here
and just pick horizontally and
189
00:09:51,858 --> 00:09:54,392
vertically in the containers,
so now it's centered.
190
00:09:54,461 --> 00:09:56,494
Again, that's not doing
anything because those other
191
00:09:56,563 --> 00:09:59,864
constraints. But I still want
these other constraints over
192
00:09:59,933 --> 00:10:02,600
here. I just don't
want them to be fixed.
193
00:10:02,669 --> 00:10:04,935
I want them to be greater than
or equal. This is something
194
00:10:05,004 --> 00:10:07,638
we did in the previous one, as
well. So I'm gonna edit all of
195
00:10:07,707 --> 00:10:10,875
these ones about the edges to
just be greater than equals,
196
00:10:10,944 --> 00:10:13,978
because I really just want
my scroll view to never go
197
00:10:14,047 --> 00:10:15,546
outside the bounds.
198
00:10:15,615 --> 00:10:18,682
But I'm fine if my scroll view
gets smaller than the bounds,
199
00:10:18,751 --> 00:10:20,318
I just don't want it to get
bigger than the bounds, so
200
00:10:20,386 --> 00:10:22,921
that's why I'm putting this
greater than or equals into
201
00:10:22,989 --> 00:10:27,358
here. Now you can see that
things have turned red. That's