-
Notifications
You must be signed in to change notification settings - Fork 149
/
8. Animation.srt
8254 lines (6589 loc) · 171 KB
/
8. Animation.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,536
[MUSIC]
2
00:00:04,605 --> 00:00:09,208
Standford University.
>> Okay, well,
3
00:00:09,276 --> 00:00:14,846
welcome to lecture number 8,
CS193P, Fall of 2017. Today
4
00:00:14,915 --> 00:00:18,183
it's all about animation, just
going to continue a little bit
5
00:00:18,251 --> 00:00:20,819
of what we talked about Last
time. There's three kinds of
6
00:00:20,888 --> 00:00:22,087
information I'm gonna
talk about today.
7
00:00:22,156 --> 00:00:25,924
The first is
UIView Property Animation. And
8
00:00:25,992 --> 00:00:27,492
it's exactly what
it sounds like.
9
00:00:27,561 --> 00:00:31,462
You can animate these yellow
properties on your view.
10
00:00:31,531 --> 00:00:34,066
This really the only things
you can animate using this
11
00:00:34,135 --> 00:00:37,569
UIView Property Animation
mechanism. But
12
00:00:37,637 --> 00:00:39,370
it's pretty powerfu.
You've got the frame and
13
00:00:39,439 --> 00:00:42,441
center that animating the
position of the view. You've
14
00:00:42,509 --> 00:00:45,343
got the bounds which will
animate the size of the view,
15
00:00:45,412 --> 00:00:48,112
although only in a transient
way because it's the frame
16
00:00:48,181 --> 00:00:50,816
that determines where you are.
And so If you're going to make
17
00:00:50,884 --> 00:00:53,285
it larger,
it occupies more space.
18
00:00:53,353 --> 00:00:56,321
In super view, you have to
set the frame. The transform,
19
00:00:56,389 --> 00:00:58,490
which is super powerful,
we saw that with the card,
20
00:00:58,558 --> 00:01:00,659
where we rotated
the corners upside down. So
21
00:01:00,727 --> 00:01:03,428
you can do rotation, you could
also do scaling with that,
22
00:01:03,496 --> 00:01:06,831
in fact, that's probably a
better way to do scaling than
23
00:01:06,900 --> 00:01:09,901
trying to mock with your
bounds. And then an opacity,
24
00:01:09,969 --> 00:01:12,837
another good one, fading views
in and out, appearing and
25
00:01:12,906 --> 00:01:15,573
disappearing, and background
color. We almost never do
26
00:01:15,642 --> 00:01:18,476
that, but you could do that.
So the way this works is with
27
00:01:18,545 --> 00:01:22,013
a class called UIView Property
Animator, not surprisingly.
28
00:01:22,082 --> 00:01:26,217
And it works with closures.
And basically the way it works
29
00:01:26,286 --> 00:01:29,821
is you set a bunch of things
on your property animator,
30
00:01:29,890 --> 00:01:32,791
UIView property animator, to
say what kind of animation you
31
00:01:32,860 --> 00:01:36,227
want. How long it's gonna
take, what curve of going
32
00:01:36,296 --> 00:01:39,531
through the process quickly or
slowly it's gonna do. If you
33
00:01:39,599 --> 00:01:42,667
wanna delay before you start.
All these kinds of things.
34
00:01:42,736 --> 00:01:45,269
You set that up and then you
give it a closure. And inside
35
00:01:45,338 --> 00:01:50,041
the closure is just code that
modifies these properties. So
36
00:01:50,110 --> 00:01:52,744
that's it, then it will
animate those changes
37
00:01:52,813 --> 00:01:56,214
that you asked for in the
fashion that you described.
38
00:01:56,283 --> 00:01:59,651
Okay so, this is a super
simple way to animate these
39
00:01:59,720 --> 00:02:02,754
properties in a view. So let's
take a look at what it looks
40
00:02:02,823 --> 00:02:06,124
like to call it now. I'm not
even gonna scratch the surface
41
00:02:06,192 --> 00:02:09,794
of UIViewPropertyAnimator's
power, it's amazing. You can
42
00:02:09,863 --> 00:02:12,197
create the se animations. You
can scrub them backwards and
43
00:02:12,266 --> 00:02:14,833
forwards. You can have
them with auto-repeat and
44
00:02:14,902 --> 00:02:16,034
reverse, do all these things.
45
00:02:16,103 --> 00:02:19,004
I'm just gonna talk about
the simplest way to use it
46
00:02:19,073 --> 00:02:22,573
which is this class method
running property animator.
47
00:02:22,642 --> 00:02:25,844
Okay, this thing creates
a property animator that will
48
00:02:25,913 --> 00:02:29,348
immediately start running. Now
the arguments to it are very
49
00:02:29,416 --> 00:02:32,351
simple. The first one with
duration is just how long this
50
00:02:32,419 --> 00:02:34,919
animation is going to take to
happen. So you can make it
51
00:02:34,988 --> 00:02:37,956
a one second animation, or a
ten second, solely up to you.
52
00:02:38,025 --> 00:02:40,825
Delay is how long to wait
before you start this
53
00:02:40,894 --> 00:02:43,895
animation. Why would you wanna
delay the starting animation?
54
00:02:43,964 --> 00:02:46,097
Well, maybe you got some other
animation going on you wanna
55
00:02:46,166 --> 00:02:50,001
wait, some time for definition
although there's ways to
56
00:02:50,070 --> 00:02:52,771
chain animations too, so
that when one finishes,
57
00:02:52,839 --> 00:02:54,973
you start another one,
but it might delay for
58
00:02:55,041 --> 00:02:57,542
that reason. Then options
we'll talk about later,
59
00:02:57,611 --> 00:03:00,411
just various options, how you
want animations to run. And
60
00:03:00,480 --> 00:03:03,748
then the all important closure
right there, animations,
61
00:03:03,817 --> 00:03:07,219
it takes no arguments, returns
no arguments this closure and
62
00:03:07,287 --> 00:03:09,120
you just put the code
that's gonna
63
00:03:09,189 --> 00:03:11,522
modify those
properties in ther.
64
00:03:11,591 --> 00:03:14,893
And then there's a completion
closure as well. This will get
65
00:03:14,962 --> 00:03:18,096
called when the animation
actually finishes running.
66
00:03:18,165 --> 00:03:20,331
And that completion one
has an argument there,
67
00:03:20,400 --> 00:03:23,067
which the position's
either the start,
68
00:03:23,136 --> 00:03:25,102
because you might be running
this animation backwards,
69
00:03:25,171 --> 00:03:28,173
believe it or not. Or it's the
end, so the animation got all
70
00:03:28,242 --> 00:03:31,275
the way to the end. Or if the
animation got interrupted in
71
00:03:31,344 --> 00:03:34,245
the middle, then the position
will be called current,
72
00:03:34,314 --> 00:03:37,182
dot current. This is an enum
right here, dot current.
73
00:03:37,251 --> 00:03:39,384
Why would an animation get
interrupted in the middle?
74
00:03:39,453 --> 00:03:42,320
You start another animation
that animates one of the same
75
00:03:42,389 --> 00:03:45,590
properties.
Then that animation wins and
76
00:03:45,659 --> 00:03:47,392
it starts to taking over.
77
00:03:47,461 --> 00:03:49,694
Now you can have multiple of
these property animators going
78
00:03:49,763 --> 00:03:53,098
side by side. All modifying
different properties, but
79
00:03:53,166 --> 00:03:56,734
once one starts to pick on
the properties of another one,
80
00:03:56,803 --> 00:03:59,037
the later one starts
to win and take over.
81
00:03:59,106 --> 00:04:01,406
Okay, and then this
one will stop, go to,
82
00:04:01,474 --> 00:04:05,310
say it's complete with the
current position. Now, there's
83
00:04:05,379 --> 00:04:08,212
something very important to
understand conceptually about
84
00:04:08,281 --> 00:04:12,016
how animations work. And
I'm gonna show you an example
85
00:04:12,085 --> 00:04:15,120
of calling this running
property on an, animator. That
86
00:04:15,188 --> 00:04:19,758
closure that you passed to it
gets executed immediately.
87
00:04:20,994 --> 00:04:23,762
It does not take five
seconds,or ten seconds, or
88
00:04:23,831 --> 00:04:25,797
however long for
that closure to execute.
89
00:04:25,866 --> 00:04:26,931
It executes immediately and
90
00:04:27,000 --> 00:04:30,702
takes effect immediately.
So, the animation is only
91
00:04:30,771 --> 00:04:34,572
what the user is seeing.
The user is seeing your
92
00:04:34,641 --> 00:04:37,409
animation happen over five or
ten seconds, but actually it
93
00:04:37,478 --> 00:04:40,344
happened the instant you
started the animation. Okay,
94
00:04:40,413 --> 00:04:43,715
so there's a difference going
on here. There's the reality,
95
00:04:43,783 --> 00:04:46,618
which is in your code. That
all happens instantly, then
96
00:04:46,686 --> 00:04:49,921
there's the presentation of
it that happens to the user.
97
00:04:49,989 --> 00:04:52,023
That's what happens over time.
So,
98
00:04:52,092 --> 00:04:55,093
this can be confusing because
you might have an animation
99
00:04:55,162 --> 00:04:57,361
that you set to take
go off in two seconds,
100
00:04:57,430 --> 00:05:00,532
and it starts running for two
seconds. And, you know you're
101
00:05:00,601 --> 00:05:03,601
thinking, yeah, my animation,
when it's done, my frame or
102
00:05:03,670 --> 00:05:06,404
my center or my alpha will be
changed. But no, the instant
103
00:05:06,472 --> 00:05:10,642
you started that animator,
it got that stuff changed. So
104
00:05:10,711 --> 00:05:12,176
you have to think about
these two timeframes.
105
00:05:12,245 --> 00:05:14,379
This is what makes animation
somewhat difficult,
106
00:05:14,448 --> 00:05:16,814
is thinking about them,
what we call the model, but
107
00:05:16,883 --> 00:05:19,050
has nothing to do with
model view controller. But
108
00:05:19,119 --> 00:05:20,985
you know the actual
reality in your code, and
109
00:05:21,054 --> 00:05:23,188
then the presentation which is
a different thing if we're to
110
00:05:23,257 --> 00:05:24,689
user seeing.
111
00:05:24,758 --> 00:05:25,923
Alright, so
112
00:05:25,992 --> 00:05:29,427
here I'm going to take a view
that's fully opaque and
113
00:05:29,495 --> 00:05:32,830
I'm gonna fade it out, and
then when it's gone I'm going
114
00:05:32,899 --> 00:05:35,901
to remove it from superview.
So this is basically make
115
00:05:35,969 --> 00:05:39,237
a view disappear from the
screen animation. £So first,
116
00:05:39,306 --> 00:05:41,940
I'm just checking to make
sure that I'm fully opaque,
117
00:05:42,009 --> 00:05:45,576
if my alpha is 1, then I'm
calling the animator here.
118
00:05:45,645 --> 00:05:49,047
This animation is gonna take
3 seconds to fade out, and
119
00:05:49,115 --> 00:05:51,382
it's not going to start
Until 2 seconds from now.
120
00:05:51,451 --> 00:05:53,552
That's what the first
two arguments mean.
121
00:05:53,620 --> 00:05:55,120
Then I picked one
option just for
122
00:05:55,188 --> 00:05:57,021
fun, which is
allowUserInteraction
123
00:05:57,090 --> 00:05:59,423
which means that as it's
fading out, gestures and
124
00:05:59,492 --> 00:06:02,694
stuff will still work on it.
Otherwise,
125
00:06:02,763 --> 00:06:06,097
if you don't specify this then
as animations happening this
126
00:06:06,165 --> 00:06:09,100
kind of animation, you
won't be able to, you know,
127
00:06:09,169 --> 00:06:12,971
tap on things or whatever.
Then here's my animations.
128
00:06:13,040 --> 00:06:16,007
My animation is just setting
my transparency to zero
129
00:06:16,076 --> 00:06:19,511
fully transparent, right?
So, that happens
130
00:06:19,579 --> 00:06:22,280
immediately when I call this
method. This method running
131
00:06:22,349 --> 00:06:25,383
property animator return
immediately having executed
132
00:06:25,451 --> 00:06:29,253
that closure immediately.
And then I have a completion.
133
00:06:29,322 --> 00:06:32,190
If the thing gets to the end
without being interrupted,
134
00:06:32,259 --> 00:06:34,493
in other words I do fade
all the way out to 0,
135
00:06:34,561 --> 00:06:37,262
then I'm going to remove
myself from my super view.
136
00:06:37,330 --> 00:06:39,898
That's what this little
completion closure is all
137
00:06:39,967 --> 00:06:43,701
about. But notice I put
print alpha equals whatever?
138
00:06:43,770 --> 00:06:47,638
That's gonna say alpha
equals 0. Do you see why?
139
00:06:47,707 --> 00:06:50,174
Cuz even though it's gonna
take five seconds for
140
00:06:50,243 --> 00:06:53,111
that alpha to go to zero it
goes to zero immediately,
141
00:06:53,180 --> 00:06:57,081
because I executed
this animation. So
142
00:06:57,150 --> 00:06:59,417
I've said that I'm sure your
like yeah makes sense, but
143
00:06:59,486 --> 00:07:02,920
until you start coding it up
you're gonna be like woah,
144
00:07:02,989 --> 00:07:04,555
that's right,
I already changed that,
145
00:07:04,624 --> 00:07:06,358
it just hasn't
appeared on screen, so
146
00:07:06,426 --> 00:07:09,594
you have to get a little bit
used to that. What are some
147
00:07:09,663 --> 00:07:11,596
of the options that you can
do when you're doing these
148
00:07:11,665 --> 00:07:14,466
animations? The first one,
begin from current state,
149
00:07:14,534 --> 00:07:16,835
is if you're animating some
property and then you start
150
00:07:16,903 --> 00:07:19,437
another animation that
animates the same property.
151
00:07:19,505 --> 00:07:22,473
Does it start from the real
value of the property, which
152
00:07:22,542 --> 00:07:25,543
is what it got set to, like
alpha equals zero? So does it
153
00:07:25,612 --> 00:07:28,079
jump to transparent and
start animating from there? Or
154
00:07:28,147 --> 00:07:31,683
does it pick up from wherever
that other animation was?
155
00:07:31,752 --> 00:07:35,353
This is really, do you use the
state of alpha that is being
156
00:07:35,422 --> 00:07:37,756
animated or do you use the
real state of alpha which is
157
00:07:37,824 --> 00:07:40,458
what's in the code which
using our previous slide,
158
00:07:40,526 --> 00:07:45,163
would be zero? Okay, so this
is kinda like picking up or
159
00:07:45,231 --> 00:07:48,232
just using the real version.
We do this quite a bit if we
160
00:07:48,301 --> 00:07:50,134
have overlapping
animations that are doing
161
00:07:50,203 --> 00:07:54,272
the same property by two
different animations.
162
00:07:54,340 --> 00:07:56,675
What else we got in there?
We got repeat and autoreverse.
163
00:07:56,743 --> 00:07:58,442
So you can have animations
that can go forwards and
164
00:07:58,511 --> 00:08:00,679
backwards and
forwards and backwards.
165
00:08:00,747 --> 00:08:02,780
It's kind of in a loop.
That's kind of fun.
166
00:08:02,849 --> 00:08:05,383
Down at the bottom you see
these curves. Curve ease in,
167
00:08:05,451 --> 00:08:07,652
ease out. For example,
third from the bottom.
168
00:08:07,721 --> 00:08:09,554
That's just saying that
when you move the thing,
169
00:08:09,623 --> 00:08:11,989
does it move linearly?
That's curve linear,
170
00:08:12,058 --> 00:08:14,825
which would be like it would
move like that, or does it
171
00:08:14,894 --> 00:08:17,695
kind of ease into moving
slowly then pick up speed and
172
00:08:17,764 --> 00:08:20,432
then slow down at the end? Now
why would you want it to do
173
00:08:20,500 --> 00:08:25,637
that? Well things that move
like very linearly feel kind
174
00:08:25,705 --> 00:08:29,006
of robotic and mechanical.
Things that ease in and
175
00:08:29,075 --> 00:08:31,909
then ease out, feel a little
more like someone picked them
176
00:08:31,978 --> 00:08:34,812
up and moved them over and
put them down. You see. So
177
00:08:34,881 --> 00:08:37,315
it is just kind of more
natural movement. So like for
178
00:08:37,383 --> 00:08:40,585
moving things you almost
always want curve ease in,
179
00:08:40,653 --> 00:08:44,188
ease out. But other ones like
maybe fading out to alpha.
180
00:08:44,257 --> 00:08:49,560
Maybe you don't need that you
can just linearly fade out. So
181
00:08:49,629 --> 00:08:51,730
that's it for view animation,
super, super easy.
182
00:08:51,798 --> 00:08:55,400
You can only animate those
properties. It's super easy to
183
00:08:55,468 --> 00:08:58,403
do. And again,
I only scratched the surface.
184
00:08:58,471 --> 00:09:01,071
I showed you one method in
UIViewPropertyAnimator.
185
00:09:01,140 --> 00:09:03,007
It has dozens of methods,
186
00:09:03,076 --> 00:09:05,643
lots of capability in there.
All right, so
187
00:09:05,711 --> 00:09:08,079
let's talk about a totally
different kinda animation now.
188
00:09:08,148 --> 00:09:11,516
This is animating
an entire view change.
189
00:09:11,585 --> 00:09:14,452
So you've got a view and
it's gonna completely change
190
00:09:14,520 --> 00:09:17,422
the way it looks and you wanna
animate that in some ways.
191
00:09:17,491 --> 00:09:18,956
And there's limited
ways to do that.
192
00:09:19,025 --> 00:09:22,593
The classic example of this is
a playing card. When a playing
193
00:09:22,662 --> 00:09:26,063
card is face down, it looks
like the Stanford logo or
194
00:09:26,132 --> 00:09:29,567
it's the back of a card. When
it's face up it's got pips and
195
00:09:29,636 --> 00:09:31,268
corners and all of that stuff.
196
00:09:31,337 --> 00:09:35,840
Okay, well when I wanna flip
my card over I could just
197
00:09:35,908 --> 00:09:37,875
change it immediately and
it would just change back and
198
00:09:37,944 --> 00:09:42,080
forth starkly, but this allows
me to actually flip it over.
199
00:09:42,149 --> 00:09:44,915
Like a 3D looking flip. And
200
00:09:44,984 --> 00:09:47,918
you can also cross dissolve
from one thing to another.
201
00:09:47,987 --> 00:09:50,454
Those are the two major things
you can do here. There's curl
202
00:09:50,523 --> 00:09:52,924
up from the bottom too, which
makes it like it's curling up,