-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
2058 lines (960 loc) · 93.7 KB
/
index.html
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
<!doctype html>
<html class="theme-next muse use-motion" lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />
<meta name="description" content="brandlee's blog">
<meta property="og:type" content="website">
<meta property="og:title" content="Brandlee's Playground">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Brandlee's Playground">
<meta property="og:description" content="brandlee's blog">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Brandlee's Playground">
<meta name="twitter:description" content="brandlee's blog">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Muse',
sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
fancybox: true,
motion: true,
duoshuo: {
userId: '0',
author: 'Author'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/"/>
<title> Brandlee's Playground </title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="en">
<div class="container sidebar-position-left
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Brandlee's Playground</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">Dream Epic</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
Home
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
Archives
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
Tags
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/03/23/Android事件传递机制/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Brand Lee">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Brandlee's Playground">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/03/23/Android事件传递机制/" itemprop="url">
Android事件传递机制
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2017-03-23T18:48:16+08:00">
2017-03-23
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/03/23/Android事件传递机制/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2017/03/23/Android事件传递机制/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Android事件传递机制一直都是一个痛点,希望这篇文章能够给你点不一样的</p>
<p>基础知识—>源码分析—>进阶—>应用场景</p>
<!--noindex-->
<div class="post-button text-center">
<a class="btn" href="/2017/03/23/Android事件传递机制/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/03/20/hello-world/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Brand Lee">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Brandlee's Playground">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/03/20/hello-world/" itemprop="url">
Hello World
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2017-03-20T10:56:55+08:00">
2017-03-20
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/03/20/hello-world/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2017/03/20/hello-world/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Welcome to <a href="http://hexo.io/" target="_blank" rel="external">Hexo</a>! This is your very first post. Check <a href="http://hexo.io/docs/" target="_blank" rel="external">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="http://hexo.io/docs/troubleshooting.html" target="_blank" rel="external">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="external">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo new <span class="string">"My New Post"</span></div></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/writing.html" target="_blank" rel="external">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo server</div></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/server.html" target="_blank" rel="external">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo generate</div></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/generating.html" target="_blank" rel="external">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ hexo deploy</div></pre></td></tr></table></figure>
<p>More info: <a href="http://hexo.io/docs/deployment.html" target="_blank" rel="external">Deployment</a></p>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/06/24/ViewDragHelper你需要知道的一切/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Brand Lee">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Brandlee's Playground">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/06/24/ViewDragHelper你需要知道的一切/" itemprop="url">
ViewDragHelper你需要知道的一切
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-06-24T10:17:06+08:00">
2016-06-24
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2016/06/24/ViewDragHelper你需要知道的一切/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/06/24/ViewDragHelper你需要知道的一切/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<blockquote>
<p>ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number of useful operations and state tracking for allowing a user to drag and reposition views within their parent ViewGroup.</p>
</blockquote>
<!--noindex-->
<div class="post-button text-center">
<a class="btn" href="/2016/06/24/ViewDragHelper你需要知道的一切/#more" rel="contents">
Read more »
</a>
</div>
<!--/noindex-->
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/05/31/Notes-of-Hacking-with-Swift/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Brand Lee">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Brandlee's Playground">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/05/31/Notes-of-Hacking-with-Swift/" itemprop="url">
Notes of Hacking with Swift
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-05-31T13:24:03+08:00">
2016-05-31
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2016/05/31/Notes-of-Hacking-with-Swift/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/05/31/Notes-of-Hacking-with-Swift/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Variables-and-constants"><a href="#Variables-and-constants" class="headerlink" title="Variables and constants"></a>Variables and constants</h2><ul>
<li>A <strong>variable</strong> is a data store that can have its value changed whenever you want.</li>
<li>A <strong>constant</strong> is a data store that you set once and can never change.</li>
<li><p>In Swift, you make a variable using the <code>var</code> keyword, and a constants using the <code>let</code> keyword:</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> name = <span class="string">"Brand Lee"</span></div><div class="line"><span class="comment">// you shouldn't use the var keyword each time – that's only used when you're declaring new variables</span></div><div class="line">name = <span class="string">"Romeo"</span></div><div class="line"></div><div class="line"><span class="keyword">let</span> name = <span class="string">"Brand Lee"</span></div><div class="line"><span class="comment">// Cannot assign to 'let' value 'name'</span></div><div class="line"><span class="comment">// name = "Romeo"</span></div></pre></td></tr></table></figure>
</li>
<li><p><strong>Important note</strong>: variable and constant names must be unique in your code.</p>
</li>
</ul>
<h2 id="Types-of-Data"><a href="#Types-of-Data" class="headerlink" title="Types of Data"></a>Types of Data</h2><figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> name: <span class="type">String</span></div><div class="line">name = <span class="string">"Tim McGraw"</span></div><div class="line"></div><div class="line"><span class="keyword">var</span> age: <span class="type">Int</span></div><div class="line">age = <span class="number">25</span></div><div class="line"></div><div class="line"><span class="keyword">var</span> latitude: <span class="type">Double</span></div><div class="line">latitude = <span class="number">36.166667</span></div><div class="line"></div><div class="line"><span class="keyword">var</span> longitude: <span class="type">Float</span></div><div class="line">longitude = -<span class="number">86.783333</span></div><div class="line"></div><div class="line"><span class="keyword">var</span> stayOutTooLate: <span class="type">Bool</span></div><div class="line">stayOutTooLate = <span class="literal">true</span></div></pre></td></tr></table></figure>
<ul>
<li>Two options to create your variable:<ol>
<li>create your variable and give it an initial value on one line of code;</li>
<li><strong>type annotations</strong>,like below:</li>
</ol>
</li>
<li><strong>Note</strong>: some people like to put a space before and after the colon, making <code>var name : String</code>, but they are wrong and you should try to avoid mentioning their wrongness in polite company.</li>
<li>The lesson here is that Swift always wants to know what type of data every variable or constant will hold,called <strong>type safety</strong>.</li>
<li>Note how both <code>String</code> and <code>Int</code> have capital letters at the start, whereas name and age do not – this is the standard coding convention in Swift.</li>
<li><code>Float</code> and <code>Double</code>:the official Apple recommendation is always to use <code>Double</code> because it has the highest accuracy(<code>Double</code> has twice the accuracy of <code>Float</code>).</li>
<li>it’s possible to specify a data type and provide a value at the same time:</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> name: <span class="type">String</span> = <span class="string">"Tim McGraw"</span></div></pre></td></tr></table></figure>
<h2 id="Operators"><a href="#Operators" class="headerlink" title="Operators"></a>Operators</h2><ul>
<li>In Swift strings are <strong>case-sensitive</strong>.</li>
</ul>
<h2 id="String-interpolation"><a href="#String-interpolation" class="headerlink" title="String interpolation"></a>String interpolation</h2><ul>
<li>fancy name but a simple thing:combining variables and constants inside a string.</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> name = <span class="string">"Tim McGraw"</span></div><div class="line"><span class="keyword">var</span> age = <span class="number">25</span></div><div class="line"><span class="keyword">var</span> latitude = <span class="number">36.166667</span></div><div class="line"><span class="string">"Your name is \(name)"</span></div><div class="line"><span class="comment">// output: Your name is Tim McGraw</span></div><div class="line"></div><div class="line"><span class="comment">// Doing that using + is much more difficult, because Swift doesn't let you add integers and doubles to a string</span></div><div class="line"><span class="string">"Your name is \(name), your age is \(age), and your latitude is \(latitude)"</span></div><div class="line"></div><div class="line"><span class="string">"Your name is "</span> + name</div></pre></td></tr></table></figure>
<ul>
<li>One of the powerful features of string interpolation is that everything between ( and ) can actually be a full Swift expression.</li>
</ul>
<h2 id="Arrays"><a href="#Arrays" class="headerlink" title="Arrays"></a>Arrays</h2><ul>
<li><p>Swift uses <code>type inference</code> to figure out what type of data your array holds</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> oddNumbers = [<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>]</div><div class="line"><span class="keyword">var</span> songs = [<span class="string">"Shake it Off"</span>, <span class="string">"You Belong with Me"</span>, <span class="string">"Back to December"</span>]</div><div class="line"><span class="comment">// Swift starts counting at 0</span></div><div class="line">songs[<span class="number">0</span>]</div><div class="line">songs[<span class="number">1</span>]</div><div class="line">songs[<span class="number">2</span>]</div><div class="line"><span class="comment">// print out the data type of variable</span></div><div class="line">songs.<span class="keyword">dynamicType</span> <span class="comment">// output: Array<String>.Type</span></div><div class="line"></div><div class="line"><span class="comment">// Swift is behind the scenes converting the array to hold the data type NSObject</span></div><div class="line"><span class="keyword">var</span> songs = [<span class="string">"Shake it Off"</span>, <span class="string">"You Belong with Me"</span>, <span class="string">"Back to December"</span>, <span class="number">3</span>]</div><div class="line">songs.<span class="keyword">dynamicType</span> <span class="comment">// output: Array<NSObject>.Type</span></div><div class="line"></div><div class="line"><span class="comment">// use type annotations to specify exactly what type of data you want an array to store</span></div><div class="line"><span class="keyword">var</span> songs: [<span class="type">String</span>] = [<span class="string">"Shake it Off"</span>, <span class="string">"You Belong with Me"</span>, <span class="string">"Back to December"</span>]</div></pre></td></tr></table></figure>
</li>
<li><p>Creating arrays</p>
</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// uses a type annotation to make it clear we want an array of strings, and it assigns an empty array to it</span></div><div class="line"><span class="keyword">var</span> songs: [<span class="type">String</span>] = []</div><div class="line"></div><div class="line"><span class="comment">// the () tells Swift we want to create the array in question, which is then assigned to songs using type inference</span></div><div class="line"><span class="keyword">var</span> songs = [<span class="type">String</span>]()</div><div class="line"></div><div class="line"><span class="keyword">var</span> songs = [<span class="string">"Shake it Off"</span>, <span class="string">"You Belong with Me"</span>, <span class="string">"Love Story"</span>]</div><div class="line"><span class="keyword">var</span> songs2 = [<span class="string">"Today was a Fairytale"</span>, <span class="string">"White Horse"</span>, <span class="string">"Fifteen"</span>]</div><div class="line"><span class="keyword">var</span> both = songs + songs2</div><div class="line"></div><div class="line">both += [<span class="string">"Everything has Changed"</span>]</div></pre></td></tr></table></figure>
<h2 id="Dictionaries"><a href="#Dictionaries" class="headerlink" title="Dictionaries"></a>Dictionaries</h2><ul>
<li>Dictionaries are another common type of collection, but they differ from arrays because they let you access values based on a key you specify.<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> person = [</div><div class="line"> <span class="string">"first"</span>: <span class="string">"Taylor"</span>,</div><div class="line"> <span class="string">"middle"</span>: <span class="string">"Alison"</span>,</div><div class="line"> <span class="string">"last"</span>: <span class="string">"Swift"</span>,</div><div class="line"> <span class="string">"month"</span>: <span class="string">"December"</span>,</div><div class="line"> <span class="string">"website"</span>: <span class="string">"taylorswift.com"</span></div><div class="line"> ]</div><div class="line"></div><div class="line">person[<span class="string">"middle"</span>]</div><div class="line">person[<span class="string">"month"</span>]</div></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="Conditional-statements"><a href="#Conditional-statements" class="headerlink" title="Conditional statements"></a>Conditional statements</h2><figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> action: <span class="type">String</span></div><div class="line"><span class="keyword">var</span> person = <span class="string">"hater"</span></div><div class="line"></div><div class="line"><span class="keyword">if</span> person == <span class="string">"hater"</span> {</div><div class="line"> action = <span class="string">"hate"</span></div><div class="line">} <span class="keyword">else</span> <span class="keyword">if</span> person == <span class="string">"player"</span> {</div><div class="line"> action = <span class="string">"play"</span></div><div class="line">} <span class="keyword">else</span> {</div><div class="line"> action = <span class="string">"cruise"</span></div><div class="line">}</div></pre></td></tr></table></figure>
<ul>
<li>Evaluating multiple conditions<br>Swift uses something called <code>short-circuit</code> evaluation to boost performance: if it is evaluating multiple things that all need to be true, and the first one is false, it doesn’t even both evaluating the rest.</li>
</ul>
<h2 id="Loops"><a href="#Loops" class="headerlink" title="Loops"></a>Loops</h2><figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="number">1</span>...<span class="number">10</span> {</div><div class="line"> <span class="built_in">print</span>(<span class="string">"\(i) x 10 is \(i * 10)"</span>)</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// If you don't need to know what number you're on, you can use an underscore instead.</span></div><div class="line"></div><div class="line"><span class="keyword">var</span> str = <span class="string">"Fakers gonna"</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> <span class="number">_</span> <span class="keyword">in</span> <span class="number">1</span> ... <span class="number">5</span> {</div><div class="line"> str += <span class="string">" fake"</span></div><div class="line">}</div><div class="line"></div><div class="line"><span class="built_in">print</span>(str)</div></pre></td></tr></table></figure>
<ul>
<li>the half open range operator: <code>..<</code><br>counts from one number up to and excluding another</li>
<li>Looping over arrays</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> songs = [<span class="string">"Shake it Off"</span>, <span class="string">"You Belong with Me"</span>, <span class="string">"Back to December"</span>]</div><div class="line"></div><div class="line"><span class="keyword">for</span> song <span class="keyword">in</span> songs {</div><div class="line"> <span class="built_in">print</span>(<span class="string">"My favorite song is \(song)"</span>)</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">var</span> people = [<span class="string">"players"</span>, <span class="string">"haters"</span>, <span class="string">"heart-breakers"</span>, <span class="string">"fakers"</span>]</div><div class="line"><span class="keyword">var</span> actions = [<span class="string">"play"</span>, <span class="string">"hate"</span>, <span class="string">"break"</span>, <span class="string">"fake"</span>]</div><div class="line"></div><div class="line"><span class="comment">// half open range operator</span></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="number">0</span> ..< people.<span class="built_in">count</span> {</div><div class="line"> <span class="built_in">print</span>(<span class="string">"\(people[i]) gonna \(actions[i])"</span>)</div><div class="line">}</div></pre></td></tr></table></figure>
<ul>
<li>One important note: although programmers conventionally use i, j and even k for loop constants, you can name them whatever you please: for personNumber in 0 ..< people.count is perfectly valid</li>
<li>While loops</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> counter = <span class="number">0</span></div><div class="line"></div><div class="line"><span class="keyword">while</span> <span class="literal">true</span> {</div><div class="line"> <span class="built_in">print</span>(<span class="string">"Counter is now \(counter)"</span>)</div><div class="line"> counter += <span class="number">1</span></div><div class="line"></div><div class="line"> <span class="keyword">if</span> counter == <span class="number">556</span> {</div><div class="line"> <span class="keyword">break</span></div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="Switch-case"><a href="#Switch-case" class="headerlink" title="Switch case"></a>Switch case</h2><ul>
<li>Swift will ensure your cases are exhaustive.That is, if there’s the possibility of your variable having a value you don’t check for, Xcode will refuse to build your app.</li>
<li>Swift can apply some evaluation to your case statements in order to match against variables.</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> studioAlbums = <span class="number">5</span></div><div class="line"></div><div class="line"><span class="keyword">switch</span> studioAlbums {</div><div class="line"><span class="keyword">case</span> <span class="number">0</span>...<span class="number">1</span>:</div><div class="line"> <span class="built_in">print</span>(<span class="string">"You're just starting out"</span>)</div><div class="line"></div><div class="line"><span class="keyword">case</span> <span class="number">2</span>...<span class="number">3</span>:</div><div class="line"> <span class="built_in">print</span>(<span class="string">"You're a rising star"</span>)</div><div class="line"></div><div class="line"><span class="keyword">case</span> <span class="number">4</span>...<span class="number">5</span>:</div><div class="line"> <span class="built_in">print</span>(<span class="string">"You're world famous!"</span>)</div><div class="line"></div><div class="line"><span class="keyword">default</span>:</div><div class="line"> <span class="built_in">print</span>(<span class="string">"Have you done something new?"</span>)</div><div class="line">}</div></pre></td></tr></table></figure>
<ul>
<li>use the <code>fallthrough</code> keyword to make one case fall into the next</li>
</ul>
<h2 id="Functions"><a href="#Functions" class="headerlink" title="Functions"></a>Functions</h2><ul>
<li>Functions let you define re-usable pieces of code that perform specific pieces of functionality.</li>
<li><p>an old Apple naming convention: function names should be designed to be read, including their first parameter. </p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">func</span> <span class="title">printAlbumRelease</span><span class="params">(name: String, year: Int)</span></span> {</div><div class="line"> <span class="built_in">print</span>(<span class="string">"\(name) was released in \(year)"</span>)</div><div class="line">}</div><div class="line"></div><div class="line">printAlbumRelease(<span class="string">"Fearless"</span>, year: <span class="number">2008</span>)</div><div class="line">printAlbumRelease(<span class="string">"Speak Now"</span>, year: <span class="number">2010</span>)</div><div class="line">printAlbumRelease(<span class="string">"Red"</span>, year: <span class="number">2012</span>)</div></pre></td></tr></table></figure>
</li>
<li><p>Swift functions can return a value by writing <code>-></code> then a data type. </p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">func</span> <span class="title">albumsIsTaylor</span><span class="params">(name: String)</span></span> -> <span class="type">Bool</span> {</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"Taylor Swift"</span> { <span class="keyword">return</span> <span class="literal">true</span> }</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"Fearless"</span> { <span class="keyword">return</span> <span class="literal">true</span> }</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"Speak Now"</span> { <span class="keyword">return</span> <span class="literal">true</span> }</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"Red"</span> { <span class="keyword">return</span> <span class="literal">true</span> }</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"1989"</span> { <span class="keyword">return</span> <span class="literal">true</span> }</div><div class="line"></div><div class="line"> <span class="keyword">return</span> <span class="literal">false</span></div><div class="line">}</div></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="Optionals"><a href="#Optionals" class="headerlink" title="Optionals"></a>Optionals</h2><ul>
<li>An optional value is one that might have a value or might not.</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// question mark: that means "optional string."</span></div><div class="line"><span class="function"><span class="keyword">func</span> <span class="title">getHaterStatus</span><span class="params">(weather: String)</span></span> -> <span class="type">String</span>? {</div><div class="line"> <span class="keyword">if</span> weather == <span class="string">"sunny"</span> {</div><div class="line"> <span class="keyword">return</span> <span class="literal">nil</span></div><div class="line"> } <span class="keyword">else</span> {</div><div class="line"> <span class="keyword">return</span> <span class="string">"Hate"</span></div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// when you declare a value as being optional, Swift will make sure you handle it safely.</span></div><div class="line"><span class="comment">// make status a optional String</span></div><div class="line"><span class="keyword">var</span> status: <span class="type">String</span>?</div><div class="line">status = getHaterStatus(<span class="string">"rainy"</span>)</div><div class="line"><span class="comment">// or</span></div><div class="line"><span class="keyword">var</span> status = getHaterStatus(<span class="string">"rainy"</span>)</div></pre></td></tr></table></figure>
<ul>
<li><code>optional unwrapping</code>:checks whether an optional has a value, and if so unwraps it into a non-optional type then runs a code block.<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span> <span class="keyword">let</span> unwrappedStatus = status {</div><div class="line"> <span class="comment">// unwrappedStatus contains a non-optional value!</span></div><div class="line">} <span class="keyword">else</span> {</div><div class="line"> <span class="comment">// in case you you want an else block, here you go…</span></div><div class="line">}</div></pre></td></tr></table></figure>
</li>
</ul>
<p>When you work with these “might be there, might not be” values, Swift forces you to unwrap them before using them, thus acknowledging that there might not be a value. That’s what <code>if let</code> syntax does: <strong>if the optional has a value then unwrap it and use it, otherwise don’t use it at all</strong>.</p>
<ul>
<li><code>Force unwrapping optionals</code>:Swift lets you override its safety by using the exclamation mark character: !</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">func</span> <span class="title">yearAlbumReleased</span><span class="params">(name: String)</span></span> -> <span class="type">Int</span>? {</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"Taylor Swift"</span> { <span class="keyword">return</span> <span class="number">2006</span> }</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"Fearless"</span> { <span class="keyword">return</span> <span class="number">2008</span> }</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"Speak Now"</span> { <span class="keyword">return</span> <span class="number">2010</span> }</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"Red"</span> { <span class="keyword">return</span> <span class="number">2012</span> }</div><div class="line"> <span class="keyword">if</span> name == <span class="string">"1989"</span> { <span class="keyword">return</span> <span class="number">2014</span> }</div><div class="line"></div><div class="line"> <span class="keyword">return</span> <span class="literal">nil</span></div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">var</span> year = yearAlbumReleased(<span class="string">"Red"</span>)</div><div class="line"></div><div class="line"><span class="keyword">if</span> year == <span class="literal">nil</span> {</div><div class="line"> <span class="built_in">print</span>(<span class="string">"There was an error"</span>)</div><div class="line">} <span class="keyword">else</span> {</div><div class="line"><span class="comment">// Note the exclamation mark: it means "I'm certain this contains a value, so force unwrap it now."</span></div><div class="line"> <span class="built_in">print</span>(<span class="string">"It was released in \(year!)"</span>)</div><div class="line">}</div></pre></td></tr></table></figure>
<ul>
<li><code>Implicitly unwrapped optionals</code><ol>
<li>A regular variable must contain a value. Example: String must contain a string, even if that is string empty, i.e. “”.</li>
<li>An optional variable might contain a value, or might not. It must be unwrapped before it is used. Example: String? might contain a string, or it might contain nil. The only way to find out is to unwrap it.</li>
<li>An implicitly unwrapped optional might contain a value, or might not. But it does not need to be unwrapped before it is used. Swift won’t check for you, so you need to be extra careful. Example: String! might contain a string, or it might contain nil – and it’s down to you to use it appropriately.</li>
</ol>
</li>
</ul>
<h2 id="Optional-chaining"><a href="#Optional-chaining" class="headerlink" title="Optional chaining"></a>Optional chaining</h2><ul>
<li><code>optional chaining</code>, which lets you run code only if your optional has a value.</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// Note that there's a question mark in there, which is the optional chaining: everything after the question mark will only be run if everything before the question mark has a value.</span></div><div class="line"><span class="keyword">let</span> album = albumReleasedYear(<span class="number">2006</span>)?.someOptionalValue?.someOtherOptionalValue?.whatever</div></pre></td></tr></table></figure>
<ul>
<li><code>The nil coalescing operator</code><br>it effectively stops them from being optional because you provide a non-optional value B</li>
</ul>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">let</span> album = albumReleasedYear(<span class="number">2006</span>) ?? <span class="string">"unknown"</span></div><div class="line"><span class="built_in">print</span>(<span class="string">"The album is \(album)"</span>)</div></pre></td></tr></table></figure>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/03/26/View的事件分发机制实践/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Brand Lee">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Brandlee's Playground">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/03/26/View的事件分发机制实践/" itemprop="url">
View的事件分发机制实践
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2016-03-26T14:12:43+08:00">
2016-03-26
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2016/03/26/View的事件分发机制实践/#comments" itemprop="discussionUrl">
<span class="post-comments-count ds-thread-count" data-thread-key="2016/03/26/View的事件分发机制实践/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>先上代码:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomViewOne</span> <span class="keyword">extends</span> <span class="title">LinearLayout</span> </span>{</div><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">"CustomView"</span>;</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CustomViewOne</span><span class="params">(Context context)</span> </span>{</div><div class="line"> <span class="keyword">super</span>(context);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CustomViewOne</span><span class="params">(Context context, AttributeSet attrs)</span> </span>{</div><div class="line"> <span class="keyword">super</span>(context, attrs);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CustomViewOne</span><span class="params">(Context context, AttributeSet attrs, <span class="keyword">int</span> defStyleAttr)</span> </span>{</div><div class="line"> <span class="keyword">super</span>(context, attrs, defStyleAttr);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="meta">@Override</span></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onInterceptTouchEvent</span><span class="params">(MotionEvent ev)</span> </span>{</div><div class="line"> <span class="keyword">boolean</span> intercepted = <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">switch</span> (ev.getAction()) {</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_DOWN:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewOne onInterceptTouchEvent: ACTION_DOWN"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_MOVE:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewOne onInterceptTouchEvent: ACTION_MOVE"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_UP:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewOne onInterceptTouchEvent: ACTION_UP"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> intercepted;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="meta">@Override</span></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onTouchEvent</span><span class="params">(MotionEvent event)</span> </span>{</div><div class="line"> <span class="keyword">boolean</span> intercepted = <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">switch</span> (event.getAction()) {</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_DOWN:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewOne onTouchEvent: ACTION_DOWN"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_MOVE:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewOne onTouchEvent: ACTION_MOVE"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_UP:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewOne onTouchEvent: ACTION_UP"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> intercepted;</div><div class="line"> }</div><div class="line"></div><div class="line">}</div></pre></td></tr></table></figure></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomViewTwo</span> <span class="keyword">extends</span> <span class="title">LinearLayout</span> </span>{</div><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">"CustomView"</span>;</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CustomViewTwo</span><span class="params">(Context context)</span> </span>{</div><div class="line"> <span class="keyword">super</span>(context);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CustomViewTwo</span><span class="params">(Context context, AttributeSet attrs)</span> </span>{</div><div class="line"> <span class="keyword">super</span>(context, attrs);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CustomViewTwo</span><span class="params">(Context context, AttributeSet attrs, <span class="keyword">int</span> defStyleAttr)</span> </span>{</div><div class="line"> <span class="keyword">super</span>(context, attrs, defStyleAttr);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="meta">@Override</span></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onInterceptTouchEvent</span><span class="params">(MotionEvent ev)</span> </span>{</div><div class="line"> <span class="keyword">boolean</span> intercepted = <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">switch</span> (ev.getAction()) {</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_DOWN:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewTwo onInterceptTouchEvent: ACTION_DOWN"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_MOVE:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewTwo onInterceptTouchEvent: ACTION_MOVE"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_UP:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewTwo onInterceptTouchEvent: ACTION_UP"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> intercepted;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="meta">@Override</span></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onTouchEvent</span><span class="params">(MotionEvent event)</span> </span>{</div><div class="line"> <span class="keyword">boolean</span> intercepted = <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">switch</span> (event.getAction()) {</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_DOWN:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewTwo onTouchEvent: ACTION_DOWN"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_MOVE:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewTwo onTouchEvent: ACTION_MOVE"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_UP:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewTwo onTouchEvent: ACTION_UP"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> intercepted;</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomViewThree</span> <span class="keyword">extends</span> <span class="title">View</span> </span>{</div><div class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">"CustomView"</span>;</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CustomViewThree</span><span class="params">(Context context)</span> </span>{</div><div class="line"> <span class="keyword">super</span>(context);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CustomViewThree</span><span class="params">(Context context, AttributeSet attrs)</span> </span>{</div><div class="line"> <span class="keyword">super</span>(context, attrs);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="title">CustomViewThree</span><span class="params">(Context context, AttributeSet attrs, <span class="keyword">int</span> defStyleAttr)</span> </span>{</div><div class="line"> <span class="keyword">super</span>(context, attrs, defStyleAttr);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="meta">@Override</span></div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">onTouchEvent</span><span class="params">(MotionEvent event)</span> </span>{</div><div class="line"> <span class="keyword">boolean</span> intercepted = <span class="keyword">false</span>;</div><div class="line"> <span class="keyword">switch</span> (event.getAction()) {</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_DOWN:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewThree onTouchEvent: ACTION_DOWN"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_MOVE:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewThree onTouchEvent: ACTION_MOVE"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> <span class="keyword">case</span> MotionEvent.ACTION_UP:</div><div class="line"> Log.d(TAG, <span class="string">"CustomViewThree onTouchEvent: ACTION_UP"</span>);</div><div class="line"> <span class="keyword">break</span>;</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> intercepted;</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<ul>
<li>情形一</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">View</th>
<th style="text-align:center">onInterceptTouchEvent</th>
<th style="text-align:center">onTouchEvent</th>
</tr>
</thead>