-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1441 lines (1017 loc) · 60.1 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 lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
<link rel="apple-touch-icon" sizes="180x180" href="/favicon.ico">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon.ico">
<link rel="mask-icon" href="/favicon.ico" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"jefferyfan.github.io","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"hide","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"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"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":true,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
</script>
<meta property="og:type" content="website">
<meta property="og:title" content="Jeffery's Blog">
<meta property="og:url" content="http://jefferyfan.github.io/index.html">
<meta property="og:site_name" content="Jeffery's Blog">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="Jeffery Fan">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="http://jefferyfan.github.io/">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false,
lang : 'zh-CN'
};
</script>
<title>Jeffery's Blog</title>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?c6d56a4590a7e7d056f66c6777e57527";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<h1 class="site-title">Jeffery's Blog</h1>
<span class="logo-line-after"><i></i></span>
</a>
<p class="site-subtitle" itemprop="description">为而不争</p>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="main-menu menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="home fa-fw"></i>首页</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="th fa-fw"></i>分类</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>
</li>
</ul>
</nav>
</div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content index posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://jefferyfan.github.io/graphics/font-rendering/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/portrait.jpg">
<meta itemprop="name" content="Jeffery Fan">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jeffery's Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/graphics/font-rendering/" class="post-title-link" itemprop="url">字体渲染概览</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2022-08-16 20:44:13" itemprop="dateCreated datePublished" datetime="2022-08-16T20:44:13+08:00">2022-08-16</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Graphics/" itemprop="url" rel="index"><span itemprop="name">Graphics</span></a>
</span>
</span>
<span id="/graphics/font-rendering/" class="post-meta-item leancloud_visitors" data-flag-title="字体渲染概览" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="前⾔"><a href="#前⾔" class="headerlink" title="前⾔"></a>前⾔</h2><p>本⽂旨在介绍⽂字渲染需要了解的基本知识点,以及些许延伸扩展;不涉及到实际代码的编写,不涉及具体算法实现,但中间穿插引⽤的外部⽂档有部分代码实现可供参考。</p>
<blockquote>
<p>本文引用的图都是来源网络博文,部分图片并未记录来源。如涉及侵权请联系我。</p>
</blockquote>
<h2 id="字体发展历史"><a href="#字体发展历史" class="headerlink" title="字体发展历史"></a>字体发展历史</h2><ul>
<li>在 Macintosh 电脑中,使用点阵字体(格式后缀:bdf、pcf、fnt、hbf等)。点阵字体放大后会出现锯齿、模糊的情况。</li>
<li>在同一时期,Adobe 发明了基于 PostScript Type 1 的矢量字体格式,但 Type 1 字体是加密的,Adobe 通过售卖字体认证赚取利润,苹果也不得不向 Adobe 购买 Type 1 字体认证。</li>
<li>于是,苹果决定设计全新的字体格式,最后在 1991 年发布为 TrueType 格式,同时也包括 Times Roman、Helvetica、Courier 等大量字体。</li>
<li>但是 TrueType 字体反响并不好,因为大部分用户已经购买了 Adobe Type 1 字体,没必要再切换。因此,苹果联合微软对抗 Adobe,授权给微软使用 TrueType 字体。1991年,微软在 Windows 3.1 系统上支持了 TrueType,并和 MonoType 公司联合开发了大量著名字体,例如 Arial 等字体。</li>
<li>1994 年,微软独自开发了 TrueType Open 字体;1996 年 Adobe 加入开发,兼容了 Type1 字体格式,更名为 OpenType。2007 年,OpenType 被国际标准组织 (ISO)采用。OpenType 常见后缀 otf、ttf、ttc。</li>
<li>2023年,Adobe 将停止支持 Type 1 字体创作。</li>
</ul>
<br/>
<blockquote>
<p><strong>SFNT</strong>:SFNT 是苹果在开发 TrueType 字体格式时设计的通⽤字体数据存储结构,SFNT 是 spline font 或者 scalable font 的缩写,TrueType、OpenType、WOFF 等格式都采⽤ SFNT 作为容器。 </p>
<p><strong>WOFF</strong>:Web Open Font Format,采⽤压缩格式,字体⽂件更⼩,适合⽹⻚使⽤。可以简单理解为 OpenType + 压缩。⼀般⽐ ttf 字体⼩ 40%。WOFF2 是 WOFF 的下⼀代标准,在 WOFF 的基础上提⾼ 30% 的 压缩率。</p>
</blockquote>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/graphics/font-rendering/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://jefferyfan.github.io/programing/iOS/non-fragile-ivar/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/portrait.jpg">
<meta itemprop="name" content="Jeffery Fan">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jeffery's Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/programing/iOS/non-fragile-ivar/" class="post-title-link" itemprop="url">Objective-C 动态之 Non-fragile ivar</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2019-11-22 15:29:21" itemprop="dateCreated datePublished" datetime="2019-11-22T15:29:21+08:00">2019-11-22</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/" itemprop="url" rel="index"><span itemprop="name">Programing</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
</span>
</span>
<span id="/programing/iOS/non-fragile-ivar/" class="post-meta-item leancloud_visitors" data-flag-title="Objective-C 动态之 Non-fragile ivar" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Objective-C 是一门动态语言。所谓动态,在于消息发送和转发,在于 Method Swizzling,同时也在于 Non-fragile ivar。之前有一篇文章简单介绍过消息发送和转发(<a href="/2016/01/24/programing/iOS/tutorial/02MethodResolve/">iOS教程(二)消息发送</a>),这一篇主要介绍下 Non-fragile ivar 特性及其实现方式。</p>
<h2 id="什么是-ivar-?"><a href="#什么是-ivar-?" class="headerlink" title="什么是 ivar ?"></a>什么是 ivar ?</h2><p>ivar(instance variable)就是类成员变量。一般情况下,property 都会自动生成一个成员变量。</p>
<p>假设有一个 MyObject 类,有 array 和 color 两个成员变量, MyObject 实例的内存结构如下:<br><img src="/images/ivar-layout.jpeg" alt="ivar-layout"></p>
<p>如果要获取 color 的值,self 指针加上偏移量,再解引用就可以了,这也是 runtime 获取成员变量的方式。</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">UIColor</span> *color = *(<span class="built_in">UIColor</span> *)((<span class="type">char</span> *)<span class="keyword">self</span> + <span class="number">16</span>);</span><br></pre></td></tr></table></figure>
<p>当然,runtime 的逻辑比这复杂,例如 atomic 加锁等。</p>
<h2 id="什么是-Non-fragile-ivar-?"><a href="#什么是-Non-fragile-ivar-?" class="headerlink" title="什么是 Non-fragile ivar ?"></a>什么是 Non-fragile ivar ?</h2><p>fragile 的含义是脆弱的,Non-fragile ivar 解决的是 fragile ivar 的问题。</p>
<h4 id="fragile-ivar-问题"><a href="#fragile-ivar-问题" class="headerlink" title="fragile ivar 问题"></a>fragile ivar 问题</h4><p>在 32 位的 Mac 上,假定有一个 NSCustomView 继承自 NSView,NSCustomView 有两个成员变量。(至于为什么是 32 位的 Mac,接下来会介绍。)</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/programing/iOS/non-fragile-ivar/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://jefferyfan.github.io/2019/11/15/programing/iOS/weak/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/portrait.jpg">
<meta itemprop="name" content="Jeffery Fan">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jeffery's Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2019/11/15/programing/iOS/weak/" class="post-title-link" itemprop="url">iOS weak 的底层实现原理</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2019-11-15 20:47:26" itemprop="dateCreated datePublished" datetime="2019-11-15T20:47:26+08:00">2019-11-15</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/" itemprop="url" rel="index"><span itemprop="name">Programing</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
</span>
</span>
<span id="/2019/11/15/programing/iOS/weak/" class="post-meta-item leancloud_visitors" data-flag-title="iOS weak 的底层实现原理" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>在 iOS 开发中,weak property 或者是 <code>__weak</code> 修饰的变量,在对象释放后,变量会自动置为 nil。delegate 模式、block 中 weak strong dance 都会用到。</p>
<p>比如,声明一个 weak 的 delegate。</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">@property</span> (<span class="keyword">nonatomic</span>, <span class="keyword">weak</span>) <span class="type">id</span> delegate;</span><br></pre></td></tr></table></figure>
<p>weak 用的好可以避免内存泄漏和野指针崩溃。weak 有这么大的作用,底层具体是咋实现的呢?本文探讨下 weak 的底层实现原理。</p>
<h3 id="初探-weak-实现"><a href="#初探-weak-实现" class="headerlink" title="初探 weak 实现"></a>初探 weak 实现</h3><p>首先通过汇编来看下,底层怎么实现的。先写几行代码。</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> <span class="type">id</span> referent = [<span class="built_in">NSObject</span> new];</span><br><span class="line"> __<span class="keyword">weak</span> <span class="type">id</span> weakObj = referent;</span><br><span class="line"> <span class="built_in">NSLog</span>(@“%@“, weakObj);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>在 Xcode 中勾选 Debug -> Debug Workflow -> Always Show Disassembly,然后在 NSLog 行加个断点,运行之后可以看到这样一段汇编代码:</p>
<p><img src="/images/weak-disassembly.png" alt="disassembly code"></p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2019/11/15/programing/iOS/weak/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://jefferyfan.github.io/2019/11/06/programing/iOS/tutorial/03responder/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/portrait.jpg">
<meta itemprop="name" content="Jeffery Fan">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jeffery's Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2019/11/06/programing/iOS/tutorial/03responder/" class="post-title-link" itemprop="url">iOS 教程(三)事件响应链</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2019-11-06 22:44:19" itemprop="dateCreated datePublished" datetime="2019-11-06T22:44:19+08:00">2019-11-06</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/" itemprop="url" rel="index"><span itemprop="name">Programing</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
</span>
</span>
<span id="/2019/11/06/programing/iOS/tutorial/03responder/" class="post-meta-item leancloud_visitors" data-flag-title="iOS 教程(三)事件响应链" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>在我们点击屏幕的时候,系统捕获到触摸事件,系统把包含这些触摸事件的信息包装成 UITouch 和 UIEvent 实例,然后找到当前运行的应用,逐级寻找能够响应这个事件的对象,直到没有响应者响应。这一系列响应者组成了响应链。</p>
<p>首先,系统捕获到点击行为后,将点击事件封装成 UIEvent 对象。接下来,就需要确定具体触摸到哪个 view,也即是找到手指触摸到的处于屏幕最前端的 view,这一步叫 hit-testing。</p>
<h3 id="hit-testing"><a href="#hit-testing" class="headerlink" title="hit-testing"></a>hit-testing</h3><p>UIView 中有两个方法,hitTest 和 pointInside:</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="built_in">UIView</span> *)hitTest:(<span class="built_in">CGPoint</span>)point withEvent:(<span class="keyword">nullable</span> <span class="built_in">UIEvent</span> *)event;</span><br><span class="line">- (<span class="type">BOOL</span>)pointInside:(<span class="built_in">CGPoint</span>)point withEvent:(<span class="keyword">nullable</span> <span class="built_in">UIEvent</span> *)event; </span><br></pre></td></tr></table></figure>
<p>pointInside 方法是判断 point 是否在当前 view 内。<br>hitTest 方法是调用 pointInside 函数判断触点是否在当前 view 内,以及递归调用子 view 的 hitTest 方法,找到实际触摸的 view。</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2019/11/06/programing/iOS/tutorial/03responder/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://jefferyfan.github.io/2018/03/17/programing/iOS/block-universal-link/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/portrait.jpg">
<meta itemprop="name" content="Jeffery Fan">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jeffery's Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2018/03/17/programing/iOS/block-universal-link/" class="post-title-link" itemprop="url">如何屏蔽 Universal Link 调端?</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2018-03-17 15:56:20" itemprop="dateCreated datePublished" datetime="2018-03-17T15:56:20+08:00">2018-03-17</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/" itemprop="url" rel="index"><span itemprop="name">Programing</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a>
</span>
</span>
<span id="/2018/03/17/programing/iOS/block-universal-link/" class="post-meta-item leancloud_visitors" data-flag-title="如何屏蔽 Universal Link 调端?" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>苹果在 iOS 9 开始提供 Universal Link 唤起 App 的能力。Universal Link 时机上是一个 https 链接,在用户点击网页中的 https 链接时,先尝试唤起 App,唤起失败则加载对应的网页。</p>
<p>相比于使用 <code>scheme://</code> 这种 scheme 的方式唤起 App,Universal Link 唤起的优点在于苹果并未提供接口进行拦截,一般 App 不会进行拦截。并且在唤起失败时,能自动加载对应的页面。</p>
<p>因此,业内使用 Universal Link 唤起 App的逐渐增多。用户被恶意导流至其他 App 的问题日益严重。本文研究 iOS Universal Link 唤起 App 的逻辑,找到拦截 Universal Link 的方案。</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2018/03/17/programing/iOS/block-universal-link/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://jefferyfan.github.io/2017/05/07/programing/design-pattern/decorator/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/portrait.jpg">
<meta itemprop="name" content="Jeffery Fan">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jeffery's Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2017/05/07/programing/design-pattern/decorator/" class="post-title-link" itemprop="url">装饰者模式(Decorator)</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2017-05-07 12:49:01" itemprop="dateCreated datePublished" datetime="2017-05-07T12:49:01+08:00">2017-05-07</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/" itemprop="url" rel="index"><span itemprop="name">Programing</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/Design-Pattern/" itemprop="url" rel="index"><span itemprop="name">Design Pattern</span></a>
</span>
</span>
<span id="/2017/05/07/programing/design-pattern/decorator/" class="post-meta-item leancloud_visitors" data-flag-title="装饰者模式(Decorator)" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>这是设计模式系列第三篇博文,装饰者模式,也是《Head First 设计模式》的学习笔记。</p>
<h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><blockquote>
<p><strong>装饰者模式</strong> 动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。</p>
</blockquote>
<h3 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h3><p>老套路,为了后面讨论的需要,先简单的复述下《Head First 设计模式》中的例子(简化版)。</p>
<ol>
<li>星巴克中有一系列的咖啡(Coffee),其中一种为 DarkRoast。</li>
<li>咖啡可以配上不同的调料,假设有两种调料 Mocha 和 Whip。</li>
<li>顾客点咖啡之后,可以自主选择调料。</li>
</ol>
<p>在这三个条件之下,要怎么来设计星巴克的点单系统,以计算出最后的价格?</p>
<p>可以把咖啡和调料都抽象成顾客需要消费的产品(Beverage),咖啡对象和调料对象都实现 Beverage 协议。</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2017/05/07/programing/design-pattern/decorator/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://jefferyfan.github.io/2017/05/03/programing/python/python/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/portrait.jpg">
<meta itemprop="name" content="Jeffery Fan">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jeffery's Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2017/05/03/programing/python/python/" class="post-title-link" itemprop="url">Python 学习笔记</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2017-05-03 22:41:08" itemprop="dateCreated datePublished" datetime="2017-05-03T22:41:08+08:00">2017-05-03</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/" itemprop="url" rel="index"><span itemprop="name">Programing</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/Python/" itemprop="url" rel="index"><span itemprop="name">Python</span></a>
</span>
</span>
<span id="/2017/05/03/programing/python/python/" class="post-meta-item leancloud_visitors" data-flag-title="Python 学习笔记" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>也不是经常使用 Python,每次用时都忘的差不多,又需要重新查资料。所以,开篇博客记录下这些琐碎的知识点。<br>都是入门基础级别的知识点,大神请出门左拐👋👋。</p>
<h4 id="获取日期"><a href="#获取日期" class="headerlink" title="获取日期"></a>获取日期</h4><p><a target="_blank" rel="noopener" href="https://docs.python.org/2/library/datetime.html">Python lib doc: datetime</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line"></span><br><span class="line"><span class="comment"># 按格式打印当前时间</span></span><br><span class="line"><span class="built_in">print</span> datetime.datetime.now().strftime(<span class="string">'%m-%d'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 按格式打印昨天时间</span></span><br><span class="line">delta = datetime.timedelta(<span class="number">1</span>)</span><br><span class="line">yestoday = datetime.datetime.now() - delta</span><br><span class="line"><span class="built_in">print</span> yestoday.strftime(<span class="string">'%m-%d'</span>)</span><br></pre></td></tr></table></figure>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2017/05/03/programing/python/python/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://jefferyfan.github.io/2017/04/20/programing/design-pattern/observer/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/portrait.jpg">
<meta itemprop="name" content="Jeffery Fan">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jeffery's Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2017/04/20/programing/design-pattern/observer/" class="post-title-link" itemprop="url">观察者模式(Observer Pattern)</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2017-04-20 23:33:07" itemprop="dateCreated datePublished" datetime="2017-04-20T23:33:07+08:00">2017-04-20</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/" itemprop="url" rel="index"><span itemprop="name">Programing</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Programing/Design-Pattern/" itemprop="url" rel="index"><span itemprop="name">Design Pattern</span></a>
</span>
</span>
<span id="/2017/04/20/programing/design-pattern/observer/" class="post-meta-item leancloud_visitors" data-flag-title="观察者模式(Observer Pattern)" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>这是设计模式系列第二篇博文,观察者模式,也是《Head First 设计模式》的学习笔记。</p>
<h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><blockquote>
<p>主题(Subject):主题对象管理某些数据。<br>观察者(Observer):监听主题的数据变化,执行响应的操作。</p>
</blockquote>
<blockquote>
<p><strong>观察者模式</strong> 定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。</p>
</blockquote>
<h3 id="讨论"><a href="#讨论" class="headerlink" title="讨论"></a>讨论</h3><p>在日常开发中,经常用到观察者模式,相信大家都已经对观察者模式烂熟于心。但是是不是真的用的好呢?我提几个问题,看看大家是否有思考过,也欢迎留言讨论。</p>
<h4 id="通知数据更新,是选择以推的方式来实现还是以拉的方式来实现?各有哪些优缺点?"><a href="#通知数据更新,是选择以推的方式来实现还是以拉的方式来实现?各有哪些优缺点?" class="headerlink" title="通知数据更新,是选择以推的方式来实现还是以拉的方式来实现?各有哪些优缺点?"></a>通知数据更新,是选择以推的方式来实现还是以拉的方式来实现?各有哪些优缺点?</h4><p>我们先定义下推和拉。<br><strong>推的方式</strong>:通知 Observer 数据有更新时,通知把更新后的数据通过参数一起传递过去;<br><strong>拉的方式</strong>:数据更新时,只通知 Observer 数据有更新,具体需要哪些数据由 Observer 主动调 Subject 的接口查询。</p>
<p>拉的方式,Observer 并不知道具体更新的数据是哪些。收到数据更新时,只能一股脑的把自己需要的数据都重新查询一遍,有时候是很低效的。其次,有时候 Observer 需要知道更新前的数据以及更新后的数据,使用拉的方式也无法解决这种场景。</p>
<p>如果选择推的方式,以上两个缺点都可以解决。但是同时也带来了一个新的问题。在推的方式中,更新的数据是以参数来传递的。当需要新增参数时,就需要修改所有 Observer 的方法,略蛋疼。</p>
<p>但其实也不是没有解决方法,可以使用一个 model 来传递。当通知更新的参数大于三个时,建议以传递 model 的方式来通知更新。</p>
<!--noindex-->
<div class="post-button">
<a class="btn" href="/2017/04/20/programing/design-pattern/observer/#more" rel="contents">
阅读全文 »
</a>
</div>
<!--/noindex-->
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://jefferyfan.github.io/2017/04/18/guide/hexo-github-custom-host/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/portrait.jpg">
<meta itemprop="name" content="Jeffery Fan">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Jeffery's Blog">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2017/04/18/guide/hexo-github-custom-host/" class="post-title-link" itemprop="url">Hexo 自定义域名(托管在 Github)</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2017-04-18 22:40:24" itemprop="dateCreated datePublished" datetime="2017-04-18T22:40:24+08:00">2017-04-18</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Guide/" itemprop="url" rel="index"><span itemprop="name">Guide</span></a>
</span>
,
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Guide/Hexo/" itemprop="url" rel="index"><span itemprop="name">Hexo</span></a>
</span>
</span>
<span id="/2017/04/18/guide/hexo-github-custom-host/" class="post-meta-item leancloud_visitors" data-flag-title="Hexo 自定义域名(托管在 Github)" title="阅读次数">
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>最近 Vultr 的 VPS 越来越不给力,挂在 Vultr VPS 上的博客加载速度越来越慢。遂决定将博客迁回 github pages。</p>
<p>其实很简单,两步搞定:</p>
<ul>
<li>首先,在 github 博客仓库根目录下新建一个文件,文件名<code>CNAME</code>,文件内容为自定义的域名,例如:blog.duxevr.com</li>
<li>在 DNS 服务商那增加一个<code>CNAME</code>记录,指向 github 博客的地址。<br>github 博客的 url 为 YOUR-GITHUB-NAME.github.io,例如我的 github 博客url为:jefferyfan.github.io。</li>
</ul>
<p>Hexo 配置相关文章:</p>
<ul>
<li><a href="/2016/07/24/guide/hexo-custom-host-vps/">Hexo博客自定义域名和部署到VPS</a></li>
<li><a href="/2015/07/22/guide/hexo-guide/">Hexo博客使用教程</a></li>
</ul>