forked from ymedialabs/ymedialabs.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
349 lines (272 loc) · 22.9 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[YMediaLabs Engineering Blog]]></title>
<link href="http://ymedialabs.github.io/atom.xml" rel="self"/>
<link href="http://ymedialabs.github.io/"/>
<updated>2015-05-25T18:00:25+05:30</updated>
<id>http://ymedialabs.github.io/</id>
<author>
<name><![CDATA[YMediaLabs]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Possible Abuse of Dispatch_after]]></title>
<link href="http://ymedialabs.github.io/blog/2015/05/25/possible-abuse-of-dispatch-after/"/>
<updated>2015-05-25T17:36:08+05:30</updated>
<id>http://ymedialabs.github.io/blog/2015/05/25/possible-abuse-of-dispatch-after</id>
<content type="html"><![CDATA[<p>I often come across code which uses dispatch_after GCD API to fire some process when an animation completes. I often feel this an abuse on using <strong>dispatch_after</strong> API. I would more like to use callbacks which notifies the completion of animation.</p>
<p>iOS provides these call backs via <strong>CATransaction</strong>. CATransaction groups several mono animation transaction into a atomic trasaction. All the changes to the rendering tree in the new run loop must be applied here.</p>
<p>Say there are two mono animations A (say translates a view in time t1) and B (say say scales a view in a time t2: t2>t1) and some process P needs to be fired after all the animation is complete i.e after time t2. Usually dispatch_after API is used to fire P after t2.</p>
<p>The usage of dispatch_after can be eliminate as follows:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="p">[</span><span class="bp">CATransaction</span> <span class="n">begin</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="bp">CATransaction</span> <span class="nl">setCompletionBlock</span><span class="p">:</span><span class="o">^</span><span class="p">{</span>
</span><span class='line'> <span class="c1">// call process P</span>
</span><span class='line'><span class="p">}];</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Animation A;</span>
</span><span class='line'><span class="c1">// Animation B;</span>
</span><span class='line'><span class="p">[</span><span class="bp">CATransaction</span> <span class="n">commit</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>
<p>when animation A and B are performerd in between [CATransaction begin] and [CATransaction commit] calls, the animations are considered as an atomic change and are applied to the render tree the same time and the transaction completion block is called when the atomic animation completes.</p>
<p>This way we can eliminate the usage of dispatch_after API reducing the overhead of changing the adjusting the time if the animation duration changes.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Shaping Up With CAShapeLayer]]></title>
<link href="http://ymedialabs.github.io/blog/2015/05/14/shaping-up-with-cashapelayer/"/>
<updated>2015-05-14T12:19:13+05:30</updated>
<id>http://ymedialabs.github.io/blog/2015/05/14/shaping-up-with-cashapelayer</id>
<content type="html"><![CDATA[<p>What out stands iOS mobile platform from others is the power of animations. For animations iOS provides several classes with animatable properties that makes animations look easier. However some animations like the <em>Apple’s application circular loader</em>, there are no special classes that readily available for animation.</p>
<p>This can be made possible with a special class <strong>CAShapeLayer</strong>. CAShapeLayer shapes itself to the path provided to it through the <em>path</em> property. The <em>path</em> property is a <em>CGPath</em> instance. We can use UIBezierPath API’s to get the required shape and retrive the CGPaths.</p>
<p>Lets get started to design out custom <strong>Apple application circular loader</strong> using key frame animations</p>
<p><strong>Create a CAShapeLayer</strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>self.animatinglayer <span class="o">=</span> <span class="o">[</span>CAShapeLayer layer<span class="o">]</span><span class="p">;</span>
</span><span class='line'>self.animatingLayer.strokeColor <span class="o">=</span> <<span class="c">#stroke color#>;</span>
</span><span class='line'>self.animatingLayer.fillColor <span class="o">=</span> <<span class="c">#fill color#>;</span>
</span><span class='line'>self.animatingLayer.lineWidth <span class="o">=</span> <<span class="c">#line width#>;</span>
</span><span class='line'>self.animatingLayer.frame <span class="o">=</span> self.bounds<span class="p">;</span>
</span><span class='line'><span class="o">[</span>self.layer addSublayer:self.animatingLayer<span class="o">]</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Create a Initial BezierPath and assign it to the above layer above</strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>UIBezierPath *initialPath <span class="o">=</span> <span class="o">[</span>UIBezierPath bezierPath<span class="o">]</span><span class="p">;</span> //empty path
</span><span class='line'>
</span><span class='line'>// get the radius of the inner path
</span><span class='line'>CGFloat <span class="nv">radius</span> <span class="o">=</span> MIN<span class="o">(</span>CGRectGetWidth<span class="o">(</span>self.bounds<span class="o">)</span>, CGRectGetHeight<span class="o">(</span>self.bounds<span class="o">))</span>/2<span class="p">;</span>
</span><span class='line'>
</span><span class='line'>// add the arc
</span><span class='line'><span class="o">[</span>initialPath addArcWithCenter:center radius:radius startAngle:degreeToRadian<span class="o">(</span>-90<span class="o">)</span> endAngle:degreeToRadian<span class="o">(</span>-90<span class="o">)</span> clockwise:YES<span class="o">]</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>// <span class="nb">set </span>the path
</span><span class='line'>self.animatinglayer.path <span class="o">=</span> initialPath.CGPath<span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>
<p>once we have the shape and the initial path all we need are the array of paths for key frame animation</p>
<p><strong>Create a Initial BezierPath and assign it to the above layer above</strong></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'>- <span class="o">(</span>NSArray *<span class="o">)</span>keyframePathsWithDuration:<span class="o">(</span>CGFloat<span class="o">)</span>duration lastUpdatedAngle:<span class="o">(</span>CGFloat<span class="o">)</span>lastUpdatedAngle newAngle:<span class="o">(</span>CGFloat<span class="o">)</span>newAngle radius:<span class="o">(</span>CGFloat<span class="o">)</span>radius <span class="nb">type</span>:<span class="o">(</span>RMIndicatorType<span class="o">)</span><span class="nb">type</span>
</span><span class='line'><span class="o">{</span>
</span><span class='line'> NSUInteger <span class="nv">frameCount</span> <span class="o">=</span> ceil<span class="o">(</span>duration * 60<span class="o">)</span><span class="p">;</span>
</span><span class='line'> NSMutableArray *array <span class="o">=</span> <span class="o">[</span>NSMutableArray arrayWithCapacity:frameCount + 1<span class="o">]</span><span class="p">;</span>
</span><span class='line'> <span class="k">for</span> <span class="o">(</span>int <span class="nv">frame</span> <span class="o">=</span> 0<span class="p">;</span> frame <<span class="o">=</span> frameCount<span class="p">;</span> frame++<span class="o">)</span>
</span><span class='line'> <span class="o">{</span>
</span><span class='line'> CGFloat <span class="nv">startAngle</span> <span class="o">=</span> degreeToRadian<span class="o">(</span>-90<span class="o">)</span><span class="p">;</span>
</span><span class='line'> CGFloat <span class="nv">endAngle</span> <span class="o">=</span> lastUpdatedAngle + <span class="o">(((</span>newAngle - lastUpdatedAngle<span class="o">)</span> * frame<span class="o">)</span> / frameCount<span class="o">)</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="o">[</span>array addObject:<span class="o">(</span>id<span class="o">)([</span>self pathWithStartAngle:startAngle endAngle:endAngle radius:radius <span class="nb">type</span>:type<span class="o">]</span>.CGPath<span class="o">)]</span><span class="p">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="o">[</span>NSArray arrayWithArray:array<span class="o">]</span><span class="p">;</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'>// Method to <span class="k">return</span> the bezier path with required start end angle and radius
</span><span class='line'>- <span class="o">(</span>UIBezierPath *<span class="o">)</span>pathWithStartAngle:<span class="o">(</span>CGFloat<span class="o">)</span>startAngle endAngle:<span class="o">(</span>CGFloat<span class="o">)</span>endAngle radius:<span class="o">(</span>CGFloat<span class="o">)</span>radius <span class="nb">type</span>:<span class="o">(</span>RMIndicatorType<span class="o">)</span><span class="nb">type</span>
</span><span class='line'><span class="o">{</span>
</span><span class='line'> BOOL <span class="nv">clockwise</span> <span class="o">=</span> startAngle < endAngle<span class="p">;</span>
</span><span class='line'>
</span><span class='line'> UIBezierPath *path <span class="o">=</span> <span class="o">[</span>UIBezierPath bezierPath<span class="o">]</span><span class="p">;</span>
</span><span class='line'> CGPoint <span class="nv">center</span> <span class="o">=</span> CGPointMake<span class="o">(</span>self.bounds.size.width / 2, self.bounds.size.height / 2<span class="o">)</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span><span class="o">(</span><span class="nb">type</span> <span class="o">==</span> kRMClosedIndicator<span class="o">)</span>
</span><span class='line'> <span class="o">{</span>
</span><span class='line'> <span class="o">[</span>path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise<span class="o">]</span><span class="p">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="k">else</span>
</span><span class='line'> <span class="o">{</span>
</span><span class='line'> <span class="o">[</span>path moveToPoint:center<span class="o">]</span><span class="p">;</span>
</span><span class='line'> <span class="o">[</span>path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise<span class="o">]</span><span class="p">;</span>
</span><span class='line'> <span class="o">[</span>path closePath<span class="o">]</span><span class="p">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="k">return</span> path<span class="p">;</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'>// Function to convert degree to radians
</span><span class='line'>float degreeToRadian<span class="o">(</span>float degree<span class="o">)</span>
</span><span class='line'><span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="o">((</span>degree * M_PI<span class="o">)</span>/180.0f<span class="o">)</span><span class="p">;</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>once we have the array of paths, we can perform the animation</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='sh'><span class='line'> CAKeyframeAnimation *pathAnimation <span class="o">=</span> <span class="o">[</span>CAKeyframeAnimation animationWithKeyPath:@<span class="s2">"path"</span><span class="o">]</span><span class="p">;</span>
</span><span class='line'> <span class="o">[</span>pathAnimation setValues:self.paths<span class="o">]</span><span class="p">;</span>
</span><span class='line'> <span class="o">[</span>pathAnimation setDuration:self.animationDuration<span class="o">]</span><span class="p">;</span>
</span><span class='line'> <span class="o">[</span>pathAnimation setTimingFunction:<span class="o">[</span>CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut<span class="o">]]</span><span class="p">;</span>
</span><span class='line'> <span class="o">[</span>pathAnimation setRemovedOnCompletion:YES<span class="o">]</span><span class="p">;</span>
</span><span class='line'> <span class="o">[</span>self.animatingLayer addAnimation:pathAnimation forKey:@<span class="s2">"path"</span><span class="o">]</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>
<p>ta-da that is all need to create Apple’s loader animation. The complete source code is available <a href="https://github.com/MaheshRS/Download-Indicator">here on github</a>.</p>
<p><img src="http://ymedialabs.github.io/images/posts/2015-05-14/RMDownload_Indicator.gif" title=""Animation Gif"" alt=""Animation Gif""></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Draw a Bezier Curve Through a Set of 2D Points in iOS]]></title>
<link href="http://ymedialabs.github.io/blog/2015/05/12/draw-a-bezier-curve-through-a-set-of-2d-points-in-ios/"/>
<updated>2015-05-12T21:47:19+05:30</updated>
<id>http://ymedialabs.github.io/blog/2015/05/12/draw-a-bezier-curve-through-a-set-of-2d-points-in-ios</id>
<content type="html"><![CDATA[<p>We got this issue couple of days back where we needed to smooth a line graph like below. It seemed strange that this was not as trivial by the Bezier methods provided by Core Graphics. So we embarked on a journey to find a generic solution.</p>
<p><img src="http://ymedialabs.github.io/images/posts/2015-05-12/curve.png" title=""Curve"" alt=""Curve""></p>
<p>In the above figure, red line shows the points we have. blue line represents the cardinal curve we want to create. Cardinal curve goes through all the points.</p>
<h1>UIBezierPath</h1>
<p>First the basics. there are two kinds of curves in UIBezierPath. Both of the curves need additional control points to define the curvature. we will need to calculate the control points to generate a smooth curve passing through given points.</p>
<ul>
<li>Quadratic</li>
<li>Cubic</li>
</ul>
<h2>Quadratic</h2>
<p>Quadratic curve has one control point which defines how the curvature of the bezier should be.</p>
<pre><code>[bezierPath addQuadCurveToPoint:destPoint controlPoint:cp1];
</code></pre>
<p><img src="http://ymedialabs.github.io/images/posts/2015-05-12/quadratic.gif" title=""Quadratic"" alt=""Quadratic""></p>
<p>here P<sub>0</sub> is starting point and P<sub>2</sub> is ending point. P<sub>1</sub> is the Control Point.</p>
<h2>Cubic</h2>
<p>Cubic curve has two control ponints which define its curvature.</p>
<pre><code> [bezierPath addCurveToPoint:destPoint controlPoint1:cp1 controlPoint2:cp2];
</code></pre>
<p><img src="http://ymedialabs.github.io/images/posts/2015-05-12/cubic.gif" title=""Cubic"" alt=""Cubic""></p>
<p>here P<sub>0</sub> is starting point and P<sub>3</sub> is ending point. P<sub>1</sub> is the Control Point 1.P<sub>2</sub> is the Control Point 2.</p>
<h1>Cardinal Curves</h1>
<p>This curve passes through all given points and each segment can be composed of cubic spline segments. We will need to figure out all the control points for each of the points.</p>
<p>Approximating with Cubic is easier and we go with that direction.</p>
<h1>Solution</h1>
<p>consider the control points we need. see that at T2, the handle is paralled to the neighboring points. in order to calculate this, we will compute the derivatives at each point which would give us the tangents.</p>
<p><img src="http://ymedialabs.github.io/images/posts/2015-05-12/soln.png" title=""Soln"" alt=""Soln""></p>
<p>the green bars are the computed derivatives at each of the points. first and last points would go towards the neighboring points.</p>
<ul>
<li>each pair of points act as start and end of the curve</li>
<li>for each start and end of curve we will compute the control points required</li>
<li>for first and last point of the curve the control points will go towards the second and previous point respectively</li>
<li>for any given point, the control points(T2) are tangents , tangent is parallel to previous two points and hence we can compute using derivatives at each point</li>
</ul>
<h2>Equations</h2>
<p>Let P<sub>0</sub> , P<sub>1</sub> … P<sub>n</sub> be the points.</p>
<p>point derivatives can be computed by</p>
<p><img src="http://ymedialabs.github.io/images/posts/2015-05-12/point-derivatives.png" title=""Derivatives"" alt=""Derivatives""></p>
<p>we need to calculate the control points from these. we can calculate those using.</p>
<p><img src="http://ymedialabs.github.io/images/posts/2015-05-12/controlpoints.png" title=""ControlPoints"" alt=""ControlPoints""></p>
<p>Once we have the control points, we can easily compose the Bezier Path.</p>
<p>Alpha is a tension. which gives the amount of smoothness needed in the curve.</p>
<h2>ObjC Pseudo Code</h2>
<pre><code>NSMutableArray *points = [[NSMutableArray alloc] init];
//populate points with CGPoint
NSMutableArray *derivative = [[NSMutableArray alloc] init];
for(NSInteger j=0;j<points.count;j++) {
CGPoint prev = points[MAX(j-1,0)];
CGPoint next = points[MIN(j+1,points.count-1)];
derivative[j] = (next - prev) / 2;
}
UIBezierPath *path = [UIBezierPath bezierPath];
for(NSUInteger i=0;i<points.count;i++) {
if(i==0) {
[path moveToPoint:points[i]]
} else {
CGPoint endPoint = points[i];
CGPoint cp1 = points[i-1]+ derivative[i-1]/tension;
CGPoint cp2 = points[i] + derivatives[i]/tension;
[path addCurveToPoint:endPoint controlPoint1:cp1 controlPoint2:cp2];
}
}
return path;
</code></pre>
<h2>Code</h2>
<p>Complete project at : <a href="https://github.com/ymedialabs/ykbezieradditions.git">YKBezierAdditions</a></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Lets Roll!]]></title>
<link href="http://ymedialabs.github.io/blog/2015/05/11/lets-roll/"/>
<updated>2015-05-11T17:10:51+05:30</updated>
<id>http://ymedialabs.github.io/blog/2015/05/11/lets-roll</id>
<content type="html"><![CDATA[<p>Welcome to YMediaLabs Engineering blog! This is a public forum where we share the latest things we work on. Somethings we think is good to share with the world. Fun things, interesting things and some amazing code.</p>
<p>We are very exited to start this blog and lot of us have great things to share in the days to come.</p>
<p>YMediaLabs is a complete interactive mobile solutions agency. We specialize in mobile app design and development for all major platforms, including the Apple iPhone, iPad, and Android. YMediaLabs is an award-winning, San Francisco Bay Area-based agency that has worked with small-to-medium businesses, disruptive startups, and Fortune 500 companies. Some of our key clients include American Express, Apple, eBay, EMC, First Data, L’Oreal, Montessorium, Staples, and Western Union. Our work has been featured by Apple, TechCrunch, Forbes and Mashable among others. To know more visit <a href="http://ymedialabs.com">YMediaLabs Website</a></p>
]]></content>
</entry>
</feed>