-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
238 lines (153 loc) · 17.2 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>KakaMic</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description">
<meta property="og:type" content="website">
<meta property="og:title" content="KakaMic">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="KakaMic">
<meta property="og:description">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="KakaMic">
<meta name="twitter:description">
<link rel="alternate" href="/atom.xml" title="KakaMic" type="application/atom+xml">
<link rel="icon" href="/favicon.png">
<link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div id="container">
<div id="wrap">
<header id="header">
<div id="banner"></div>
<div id="header-outer" class="outer">
<div id="header-title" class="inner">
<h1 id="logo-wrap">
<a href="/" id="logo">KakaMic</a>
</h1>
<h2 id="subtitle-wrap">
<a href="/" id="subtitle">Everybody in this country should learn how to program a computer... because it teaches you how to think. - Steve Jobs</a>
</h2>
</div>
<div id="header-inner" class="inner">
<nav id="main-nav">
<a id="main-nav-toggle" class="nav-icon"></a>
<a class="main-nav-link" href="/">Home</a>
<a class="main-nav-link" href="/archives">Archives</a>
</nav>
<nav id="sub-nav">
<a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
<a id="nav-search-btn" class="nav-icon" title="Search"></a>
</nav>
<div id="search-form-wrap">
<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"></button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
</div>
</div>
</div>
</header>
<div class="outer">
<section id="main">
<article id="post-Designated-Initializers" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/04/26/Designated-Initializers/" class="article-date">
<time datetime="2016-04-26T10:18:50.000Z" itemprop="datePublished">2016-04-26</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/04/26/Designated-Initializers/">Designated Initializers</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>Objective-C<br>一个对象可以有多个构造方法</p>
<p>指定构造器(Designated Initializers)<br>必须调用父类的指定构造器</p>
<p>便利构造器(Convenience Initializers)<br>必须调用指定构造器</p>
<p>iOS8后在指定构造器后添加宏`NS_DESIGNATED_INITIALIZER`,跟编译器指明哪个是指定构造器</p>
<p>Profile.h</p>
<figure class="highlight plain"><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><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">#import <Foundation/Foundation.h></span><br><span class="line"></span><br><span class="line">@interface Profile : NSObject</span><br><span class="line"></span><br><span class="line">@property (nonatomic, copy) NSString *name;</span><br><span class="line"></span><br><span class="line">- (instancetype)init;</span><br><span class="line"></span><br><span class="line">- (instancetype)initWithName:(NSString *)name NS_DESIGNATED_INITIALIZER;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<p>Profile.m</p>
<figure class="highlight plain"><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><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">#import "Profile.h"</span><br><span class="line"></span><br><span class="line">@implementation Profile</span><br><span class="line"></span><br><span class="line">- (instancetype)init {</span><br><span class="line"> // 必须调用指定构造器</span><br><span class="line"> return [self initWithName:@"Unknown"];</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">- (instancetype)initWithName:(NSString *)name {</span><br><span class="line"> // 必须调用父类的指定构造器</span><br><span class="line"> self = [super init];</span><br><span class="line"> if (self) {</span><br><span class="line"> _name = [name copy];</span><br><span class="line"> }</span><br><span class="line"> return self;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<p>参考:<br><a href="http://useyourloaf.com/blog/xcode-6-objective-c-modernization/" target="_blank" rel="external">http://useyourloaf.com/blog/xcode-6-objective-c-modernization/</a><br><a href="http://stackoverflow.com/questions/26185239/ios-designated-initializers-using-ns-designated-initializer" target="_blank" rel="external">http://stackoverflow.com/questions/26185239/ios-designated-initializers-using-ns-designated-initializer</a><br><a href="http://honglu.me/2015/09/16/Objective-C%E5%BE%88%E6%9C%89%E7%94%A8%E7%9A%84%E7%89%B9%E6%80%A7/" target="_blank" rel="external">http://honglu.me/2015/09/16/Objective-C%E5%BE%88%E6%9C%89%E7%94%A8%E7%9A%84%E7%89%B9%E6%80%A7/</a><br><a href="http://www.starfelix.com/blog/2014/04/13/zheng-que-bian-xie-designated-initializerde-ji-ge-yuan-ze/" target="_blank" rel="external">http://www.starfelix.com/blog/2014/04/13/zheng-que-bian-xie-designated-initializerde-ji-ge-yuan-ze/</a></p>
</div>
<footer class="article-footer">
<a data-url="http://yoursite.com/2016/04/26/Designated-Initializers/" data-id="cinilek880002vgddqwfcrjb6" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-CollectionView动态居中显示解析" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/04/15/CollectionView动态居中显示解析/" class="article-date">
<time datetime="2016-04-15T08:25:50.000Z" itemprop="datePublished">2016-04-15</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/04/15/CollectionView动态居中显示解析/">CollectionView动态居中显示解析</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p><img src="https://raw.githubusercontent.com/KakaMic/BlogResource/master/collectionviewcell1.png" alt=""></p>
<p>控制最后滚动的位置<br>-(CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity</p>
<p>参数<br>proposedContentOffset:停止滚动时的偏移量(正数)<br>velocity:滚动速度</p>
<p>手停止滚动离开控件时系统调用targetContentOffsetForProposedContentOffset</p>
<h1 id="关键方法:"><a href="#关键方法:" class="headerlink" title="关键方法:"></a>关键方法:</h1><figure class="highlight plain"><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><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 初始化</span><br><span class="line"> */</span><br><span class="line">- (void)prepareLayout {</span><br><span class="line"> [super prepareLayout];</span><br><span class="line"> self.itemSize = CGSizeMake(itemWidth, CGRectGetHeight(self.collectionView.bounds));</span><br><span class="line"> self.scrollDirection = UICollectionViewScrollDirectionHorizontal;</span><br><span class="line"> self.minimumLineSpacing = kCellInterval;</span><br><span class="line"> CGFloat inset = 100;</span><br><span class="line"> self.sectionInset = UIEdgeInsetsMake(0, inset, 0, inset);// 设置inset左右留白</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 控件滚动 重新布局</span><br><span class="line"> */</span><br><span class="line">- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {</span><br><span class="line"> return YES;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 控制最后滚动的位置</span><br><span class="line"> *</span><br><span class="line"> * @param proposedContentOffset 停止滚动时的偏移量(正数)</span><br><span class="line"> * @param velocity 滚动速度</span><br><span class="line"> *</span><br><span class="line"> * @return 最终控件的偏移量</span><br><span class="line"> */</span><br><span class="line">- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {</span><br><span class="line"> </span><br><span class="line"> // 滚动停止后控件在屏幕上的可视范围</span><br><span class="line"> CGRect viewRect ;</span><br><span class="line"> viewRect.origin = proposedContentOffset;</span><br><span class="line"> viewRect.size = self.collectionView.frame.size;</span><br><span class="line"> </span><br><span class="line"> // 在viewRect屏幕可见范围内的Cell Attributes数组</span><br><span class="line"> NSArray *viewAttributes = [self layoutAttributesForElementsInRect:viewRect];</span><br><span class="line"> </span><br><span class="line"> // 控件在屏幕中间位置的点(偏移量 + 控件宽度/2)</span><br><span class="line"> CGFloat center_X = proposedContentOffset.x + CGRectGetWidth(self.collectionView.bounds) / 2 ;</span><br><span class="line"> </span><br><span class="line"> // 通过center_X和可见范围cell的中点(上图红点)比对,判断那个cell离中间最近</span><br><span class="line"> CGFloat cell2CenterOffset = MAXFLOAT;</span><br><span class="line"> for (UICollectionViewLayoutAttributes *attribute in viewAttributes) {</span><br><span class="line"> if(ABS(attribute.center.x - center_X) < ABS(cell2CenterOffset)){</span><br><span class="line"> // 得到cell距中点的最小距离差</span><br><span class="line"> cell2CenterOffset = attribute.center.x - center_X;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> // 返回最终控件的偏移量</span><br><span class="line"> return CGPointMake(proposedContentOffset.x + cell2CenterOffset, proposedContentOffset.y);</span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>例1:如上图cell_1距中点靠左,<br><figure class="highlight plain"><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></pre></td><td class="code"><pre><span class="line">CGRectGetWidth(self.collectionView.bounds) / 2 = 187.5</span><br><span class="line">cell.center.x=187.5,</span><br><span class="line">center_X=14+187.5,</span><br><span class="line">cell2CenterOffset=cell.center.x - center_X = -14,</span><br></pre></td></tr></table></figure></p>
<p>说明偏移量proposedContentOffset.x需要减去14,即控件自动向右移动14的距离</p>
<p>例2:若cell_1距中点靠右,<br><figure class="highlight plain"><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></pre></td><td class="code"><pre><span class="line">CGRectGetWidth(self.collectionView.bounds) / 2 = 187.5</span><br><span class="line">cell.center.x=387.5,</span><br><span class="line">center.X=172.5+187.5</span><br><span class="line">cell2CenterOffset=cell.center.x - center_X = 387.5 - 360 = 27.5</span><br></pre></td></tr></table></figure></p>
<p>说明偏移量proposedContentOffset.x需要增加27.5,即控件自动向左移动27.5的距离</p>
<h1 id="总结:"><a href="#总结:" class="headerlink" title="总结:"></a>总结:</h1><p>控件往左滚动,偏移量正数递增,通过得到屏幕中点相对于控件滚动的距离和每个cell在控件上的center.x比较,得到最小的距离,<br>cell在中点右边,cell.x-center_X > 0,则需要增加差值的偏移量,控件自动向左滑动差值距离<br>cell在中点的左边,cell.x - center_X < 0,则需要减少差值的偏移量,控件自动向右滑动差值距离</p>
<p><img src="https://raw.githubusercontent.com/KakaMic/BlogResource/master/collectionview2.jpeg" alt=""></p>
</div>
<footer class="article-footer">
<a data-url="http://yoursite.com/2016/04/15/CollectionView动态居中显示解析/" data-id="cinilek830001vgddovpi7yui" class="article-share-link">Share</a>
</footer>
</div>
</article>
</section>
<aside id="sidebar">
<div class="widget-wrap">
<h3 class="widget-title">Archives</h3>
<div class="widget">
<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/">April 2016</a></li></ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Recent Posts</h3>
<div class="widget">
<ul>
<li>
<a href="/2016/04/26/Designated-Initializers/">Designated Initializers</a>
</li>
<li>
<a href="/2016/04/15/CollectionView动态居中显示解析/">CollectionView动态居中显示解析</a>
</li>
</ul>
</div>
</div>
</aside>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info" class="inner">
© 2016 KakaMic<br>
Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
</div>
</div>
</footer>
</div>
<nav id="mobile-nav">
<a href="/" class="mobile-nav-link">Home</a>
<a href="/archives" class="mobile-nav-link">Archives</a>
</nav>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script src="/js/script.js"></script>
</div>
</body>
</html>