<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Matthew Cheok]]></title><description><![CDATA[Random ramblings about code and design]]></description><link>http://blog.matthewcheok.com/</link><generator>Ghost 0.6</generator><lastBuildDate>Thu, 17 Feb 2022 20:53:06 GMT</lastBuildDate><atom:link href="http://blog.matthewcheok.com/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Writing a parser in Swift (II)]]></title><description><![CDATA[<p>This is a follow up to <a href="http://blog.matthewcheok.com/writing-a-parser-in-swift/">Writing a Parser in Swift (I)</a>, I'd highly suggest reading that first if you haven't already.</p>

<p>Last time, we looked at <strong>Context-free Grammars</strong>, <strong>Recursive Descent Parsing</strong> and <strong>Operator Precedence Parsing</strong>. Let's complete the missing pieces in our trivial language, Kaleidoscope.</p>

<p>Here's some grammar that</p>]]></description><link>http://blog.matthewcheok.com/writing-a-parser-in-swift-part-2/</link><guid isPermaLink="false">eea843d8-8931-421e-8778-4aa55f4d47c1</guid><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Sat, 28 Nov 2015 00:30:48 GMT</pubDate><content:encoded><![CDATA[<p>This is a follow up to <a href="http://blog.matthewcheok.com/writing-a-parser-in-swift/">Writing a Parser in Swift (I)</a>, I'd highly suggest reading that first if you haven't already.</p>

<p>Last time, we looked at <strong>Context-free Grammars</strong>, <strong>Recursive Descent Parsing</strong> and <strong>Operator Precedence Parsing</strong>. Let's complete the missing pieces in our trivial language, Kaleidoscope.</p>

<p>Here's some grammar that describes declaring a function:  </p>

<pre><code>Function -&gt; Prototype Expression  
Prototype -&gt; Define Identifier ( Arguments )  
Arguments -&gt; Arguments, Identifier | Identifier | ε  
</code></pre>

<p>Functions are declared with a <code>Prototype</code> followed by a single <code>Expression</code>. The <code>Prototype</code> consists of the keyword <code>def</code>, which is represented by the <code>Define</code> symbol, followed by an <code>Identifier</code> and a pair of parenthesis. Any arguments are included comma-separated within the parenthesis. In the grammar above, we denote the empty string <code>""</code> with epsilon, <code>ε</code>.</p>

<p>Similarly we can write production rules for calling a function:  </p>

<pre><code>PrimaryExpression -&gt; Call | Identifier | Number | ( Expression )  
Call -&gt; Identifier ( Parameters )  
Parameters -&gt; Parameters, Expression | Expression | ε  
</code></pre>

<p>To call a function, we first have an <code>Identifier</code> that represents the name of the function, followed by any parameters comma-separated within the parenthesis.</p>

<p>Let's look at how we can create the corresponding nodes in our <strong>AST</strong>:</p>

<pre><code class="language-swift">struct PrototypeNode: CustomStringConvertible {  
    let name: String
    let argumentNames: [String]
    var description: String {
        return "PrototypeNode(name: \(name), argumentNames: \(argumentNames))"
    }
}

struct FunctionNode: CustomStringConvertible {  
    let prototype: PrototypeNode?
    let body: ExprNode
    var description: String {
        return "FunctionNode(prototype: \(prototype), body: \(body))"
    }
}
</code></pre>

<p>We'll explain why the <code>prototype</code> property is <strong>optional</strong> later. Similarly, here's how function calls are represented:</p>

<pre><code class="language-swift">struct CallNode: ExprNode {  
    let name: String
    let arguments: [ExprNode]
    var description: String {
        return "CallNode(name: \(name), arguments: \(arguments))"
    }
}
</code></pre>

<p>Notice the symmetry between function declarations and function calls. This would hint to us that we should write code that share logic in some way. In particular, we can factor the logic that parses a comma-separated list into a method that takes as input the logic for generating the list elements (specifically <code>ExprNode</code> for parameters and <code>String</code> for argument names.)</p>

<p>Here's the parsing logic for identifiers and function calls:  </p>

<pre><code class="language-swift">func readIdentifier() throws -&gt; String {  
    guard case let Token.Identifier(name) = popCurrentToken() else {
        throw ParseError.ExpectedIdentifier
    }
    return name
}

func parseIdentifierOrFunctionCall() throws -&gt; ExprNode {  
    let name = try readIdentifier()

    guard tokensAvailable else {
        return VariableNode(name: name)            
    }

    guard case Token.ParensOpen = peekCurrentToken() else {
        return VariableNode(name: name)
    }

    let arguments = try parseParensList(parseExpression)
    return CallNode(name: name, arguments: arguments)
}
</code></pre>

<p>If there is a <code>(</code> symbol after the <code>Identifier</code>, we assume it to be a function call. We also assume the method <code>func parseParensList</code> will parse a comma-separated list of something. In this case, we have it return parameters which are just <code>Expression</code>s via the <code>func parseExpression()</code> method defined earlier.</p>

<p>Next, let's take a look at parsing function declarations:  </p>

<pre><code class="language-swift">enum ParseError: ErrorType {  
    ...
    case ExpectedFunctionDeclaration
    case ExpectedFunctionName
}

func parsePrototype() throws -&gt; PrototypeNode {  
    guard case let Token.Identifier(name) = popCurrentToken() else {
        throw ParseError.ExpectedFunctionName
    }

    let argumentNames = try parseParensList(readIdentifier)
    return PrototypeNode(name: name, argumentNames: argumentNames)
}

func parseDefinition() throws -&gt; FunctionNode {  
    guard case Token.Define = popCurrentToken() else {
        throw ParseError.ExpectedFunctionDeclaration
    }

    let prototype = try parsePrototype()
    let body = try parseExpression()
    return FunctionNode(prototype: prototype, body: body)
}
</code></pre>

<p>This time, our customisation to <code>func parseParensList()</code> is <code>func readIdentifier()</code> which returns the <code>String</code> of the name of an <code>Identifier</code>. The parsing of the function definition is rather trivial.</p>

<p>Now, we're ready to talk about how we can actually parse the list of <code>ExprNode</code>s or <code>String</code>s in each case. We allow customisation with 2 swift features:</p>

<p>1) We take a closure as input to perform the actual reading <br>
2) We use a generic method that allows us to have the correct return type depending on the input closure</p>

<pre><code class="language-swift">enum ParseError: ErrorType {  
    ...
    case ExpectedCommaSeparatedList
}

func parseParensList&lt;T&gt;(@noescape read: () throws -&gt; T) throws -&gt; [T] {  
    guard case Token.ParensOpen = popCurrentToken() else {
        throw ParseError.ExpectedCharacter("(")
    }

    if case Token.ParensClose = peekCurrentToken() {
        return []
    }

    var list = [T]()
    while true {
        let element = try read()
        list.append(element)

        if case Token.ParensClose = peekCurrentToken() {
            break
        }

        guard case Token.Comma = popCurrentToken() else {
            throw ParseError.ExpectedCommaSeparatedList
        }
    }

    guard case Token.ParensClose = popCurrentToken() else {
        throw ParseError.ExpectedCharacter(")")
    }

    return list
}
</code></pre>

<p>We're using the <code>@noescape</code> attribute here to tell the Swift compiler that this closure will not be used outside of this method and allows us to ignore any reference semantics for capturing variables.</p>

<p>We begin by looking for <code>(</code> followed by <code>)</code> which essentially means we have an empty list. If we don't find the closing parenthesis, we should read the element and add it to our list. Each element should be followed by the closing parenthesis or a comma if there are more elements after. Finally, we discard the closing <code>)</code> and return the list.</p>

<p>Next, we also need to update our method <code>func parsePrimary()</code> to understand function calls:  </p>

<pre><code class="language-swift">func parsePrimary() throws -&gt; ExprNode {  
    switch (peekCurrentToken()) {
    case .Identifier:
        return try parseIdentifierOrFunctionCall()
    case .Number:
        return try parseNumber()
    case .ParensOpen:
        return try parseParens()
    default:
        throw ParseError.ExpectedExpression
    }
}
</code></pre>

<p>Now, we are ready to finish our <code>Parser</code> class. We're going to simplify things by representing top-level expressions as a <code>FunctionNode</code> without a <code>prototype</code> (we made this optional earlier.)</p>

<pre><code class="language-swift">func parseTopLevelExpr() throws -&gt; FunctionNode {  
    let body = try parseExpression()
    return FunctionNode(prototype: nil, body: body)
}

func parse() throws -&gt; [Any] {  
    index = 0

    var nodes = [Any]()
    while index &lt; tokens.count {
        switch peekCurrentToken() {
        case .Define:
            let node = try parseDefinition()
            nodes.append(node)
        default:
            let expr = try parseTopLevelExpr()
            nodes.append(expr)
        }
    }

    return nodes
}
</code></pre>

<p>Now we can parse a series of function definitions or expressions:</p>

<pre><code class="language-swift">let parser = Parser(tokens: tokens)  
do {  
    print(try parser.parse())
}
catch {  
    print(error)
}
</code></pre>

<p>If we pass in the following input:  </p>

<pre><code>def sum(x, y) x+y  
sum(4, 3)  
</code></pre>

<p>We get a nice list of <code>FunctionNode</code>s:  </p>

<pre><code>[
FunctionNode(  
    prototype: Optional(PrototypeNode(
        name: sum, 
        argumentNames: ["x", "y"])), 
    body: BinaryOpNode(
        +, 
        lhs: VariableNode(x), 
        rhs: VariableNode(y))), 
FunctionNode(  
    prototype: nil, 
    body: CallNode(
        name: sum, 
        argument: [NumberNode(4.0), NumberNode(3.0)]))
]
</code></pre>

<p>Coming up next, we'll talk about how we can use LLVM to run this program!</p>]]></content:encoded></item><item><title><![CDATA[Writing a parser in Swift (I)]]></title><description><![CDATA[<p>This is part two to <a href="http://blog.matthewcheok.com/writing-a-lexer-in-swift/">Writing a lexer in Swift</a>. If you haven't caught that, you might get more context following that first.</p>

<p>In this post, we'll look at how to build an <strong>Abstract Syntax Tree</strong> from a series of tokens.</p>

<p>Firstly, we'll look at how to construct a <strong>grammar</strong></p>]]></description><link>http://blog.matthewcheok.com/writing-a-parser-in-swift/</link><guid isPermaLink="false">d4cbdf4f-8480-49e8-9f29-dc381c723c53</guid><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Sun, 22 Nov 2015 04:01:23 GMT</pubDate><content:encoded><![CDATA[<p>This is part two to <a href="http://blog.matthewcheok.com/writing-a-lexer-in-swift/">Writing a lexer in Swift</a>. If you haven't caught that, you might get more context following that first.</p>

<p>In this post, we'll look at how to build an <strong>Abstract Syntax Tree</strong> from a series of tokens.</p>

<p>Firstly, we'll look at how to construct a <strong>grammar</strong> that describes what is allowed in the language. One of the useful tools we have is the <strong>Context-free Grammar</strong>. This allows us to describe the language as a set of rules called <strong>productions</strong>. In fact, the <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/zzSummaryOfTheGrammar.html">Swift Reference</a> describes the language in this form! </p>

<p>Here's an example:</p>

<pre><code>Expression -&gt; Identifier | Number  
</code></pre>

<p>Production rules allow us to replace symbols on the left (which by definition are non-terminal) by a sequences of symbols of the right (which may or may not be terminal symbols.) This particular rule says that an <code>Expression</code> symbol can be replaced by either an <code>Identifier</code> symbol or a <code>Number</code> symbol. For our purposes, these symbols are equivalent to tokens returned from our lexer. </p>

<p>Of course, production rules in practice are never so simple. Recall the following expression from the previous post:  </p>

<pre><code>x + y * 2 + (4 + 5) / 3  
</code></pre>

<p>There's no way we can describe this using the above production rule. Let's try a slightly more exhaustive version:</p>

<pre><code>Expression -&gt; Identifier | Number | ( Expression )  
</code></pre>

<p>This version of the production rule lets us deal with parenthesised expressions, but we're still missing operators.</p>

<pre><code>Expression -&gt;  
  PrimaryExpression Operator Expression | PrimaryExpression
PrimaryExpression -&gt; Identifier | Number | ( Expression )  
</code></pre>

<p>Another feature of production rules in Context-free Grammars is that they can be recursive. This allows us to describe more complex language features (especially arithmetic) which we are familiar with in common programming languages.</p>

<p>We now have a production rule which describes a <code>PrimaryExpression</code> which can be either an <code>Identifier</code>, a <code>Number</code> or a parenthesised <code>Expression</code>. This is in turn used to describe <code>Expression</code> which may or may not include <code>Operator</code>s with <code>PrimaryExpression</code>s.</p>

<p>This is probably enough theory. Let's take a look at how we can implement this parser in code.</p>

<pre><code class="language-swift">class Parser {  
    let tokens: [Token]
    var index = 0

    init(tokens: [Token]) {
        self.tokens = tokens
    }

    var tokensAvailable: Bool {
        return index &lt; tokens.count
    }

    func peekCurrentToken() -&gt; Token {
        return tokens[index]
    }

    func popCurrentToken() -&gt; Token {
        return tokens[index++]
    }
}
</code></pre>

<p>We initialise the parser with a list of <code>Token</code>s. This essentially sets up the initial state. Next, we have a couple of convenient helper methods which allow us to inspect tokens. The method <code>func peekCurrentToken()</code> returns the next available token. This is commonly known as <a href="https://en.wikipedia.org/wiki/LALR_parser">look-ahead</a> in parser terminology. </p>

<p>The method <code>func popCurrentToken()</code> also returns the current token but moves the <code>index</code> pointer one over, "consuming" the token. As we build our AST, we will generate tree nodes and consume the corresponding tokens as we go.</p>

<p>Let's define our nodes:</p>

<pre><code>protocol ExprNode: CustomStringConvertible {  
}
</code></pre>

<p>Firstly, we have a base protocol <code>ExprNode</code> which allows us to describe nodes in <code>Expression</code>s. For convenience, we also inherit <code>CustomStringConvertible</code> so we can have nice <code>print()</code>s when debugging.</p>

<pre><code class="language-swift ">struct NumberNode: ExprNode {  
    let value: Float
    var description: String {
        return "NumberNode(\(value))"
    }
}

struct VariableNode: ExprNode {  
    let name: String
    var description: String {
        return "VariableNode(\(name))"
    }
}
</code></pre>

<p>For our first 2 nodes, we have <code>NumberNode</code> and <code>VariableNode</code>. As you might expect, <code>NumberNode</code> corresponds with the <code>Token.Number</code> and <code>VariableNode</code> with <code>Token.Identifier</code>. We could have simply used our <code>Token</code> enumeration as nodes in our tree but you will soon see that tree nodes also include more context than there is in our lexical tokens.</p>

<pre><code>struct BinaryOpNode: ExprNode {  
    let op: String
    let lhs: ExprNode
    let rhs: ExprNode
    var description: String {
        return "BinaryOpNode(\(op), lhs: \(lhs), rhs: \(rhs))"
    }
}
</code></pre>

<p>The next node, <code>BinaryOpNode</code>, describes the infix operation. It has <code>lhs</code>, <code>rhs</code> and <code>op</code> as properties which correspond to the left-hand side, right-hand side and operator in a binary operation. Now it's evident why we created the <code>ExprNode</code> protocol. Either sides of the operation can contain <code>NumberNode</code>, <code>VariableNode</code> or <code>BinaryOpNode</code> (or future nodes we might introduce.)</p>

<p>With this, we can describe the complex expression we had at the beginning (repeated below):</p>

<pre><code>x + y * 2 + (4 + 5) / 3  
</code></pre>

<p>Here's how we parse <code>Token.Number</code> and <code>Token.Identifier</code>:</p>

<pre><code class="language-swift">enum ParseError: ErrorType {  
    case ExpectedNumber
    case ExpectedIdentifier
}

func parseNumber() throws -&gt; ExprNode {  
    guard case let Token.Number(value) = popCurrentToken() else {
        throw ExpectedNumber
    }
    return NumberNode(value: value)
}

func parseIdentifier() throws -&gt; ExprNode {  
    guard case let Token.Identifier(name) = popCurrentToken() else {
        throw ParseError.ExpectedIdentifier
    }
    return VariableNode(name: name)
}
</code></pre>

<p>We read the current token (consuming it), check if it's what we expect, and return the corresponding node.</p>

<p>Next, we'll first show the declaration for parsing the <code>Expression</code> symbol because we need to use this recursively (also known as <a href="https://en.wikipedia.org/wiki/Recursive_descent_parser">Recursive Descent Parsing</a>.) We'll fill this out later:  </p>

<pre><code class="language-swift">func parseExpression() throws -&gt; ExprNode  
</code></pre>

<p>Now let's try to parse the parenthesised <code>Expression</code>:  </p>

<pre><code>enum ParseError: ErrorType {  
    ...
    case ExpectedCharacter(Character)
}

func parseParens() throws -&gt; ExprNode {  
    guard case Token.ParensOpen = popCurrentToken() else {
        throw ParseError.ExpectedCharacter("(")
    }

    let exp = try parseExpression()

    guard case Token.ParensClose = popCurrentToken() else {
        throw ParseError.ExpectedCharacter(")")
    }

    return exp
}
</code></pre>

<p>With this, we have all the ingredients we need to write <code>func parsePrimary()</code>:</p>

<pre><code>enum ParseError: ErrorType {  
    ...
    case ExpectedExpression
}

func parsePrimary() throws -&gt; ExprNode {  
    switch (peekCurrentToken()) {
    case .Identifier:
        return try parseIdentifier()
    case .Number:
        return try parseNumber()
    case .ParensOpen:
        return try parseParens()
    default:
        throw ParseError.ExpectedExpression
    }
}
</code></pre>

<p>If we create a parse tree for the expression in terms of binary operations naively, you might notice we could potentially create different versions of the tree with any of the operators at the root node. Of course, mathematically not all versions would be deemed <strong>correct</strong>. We need a way to encode our mathematical rules about which operators act first (also known as the <a href="https://en.wikipedia.org/wiki/Order_of_operations">order of operations</a>.) </p>

<p>This can be done with <a href="https://en.wikipedia.org/wiki/Operator-precedence_parser">Operator Precedence Parsing</a>, in which we give each operator a precedence (read priority level.)</p>

<pre><code class="language-swift">let operatorPrecedence: [String: Int] = [  
    "+": 20,
    "-": 20,
    "*": 40,
    "/": 40
]

enum ParseError: ErrorType {  
    ...
    case UndefinedOperator(String)
}

func getCurrentTokenPrecedence() throws -&gt; Int {  
    guard tokensAvailable else {
        return -1
    }

    guard case let Token.Other(op) = peekCurrentToken() else {
        return -1
    }

    guard let precedence = operatorPrecedence[op] else {
        throw ParseError.UndefinedOperator(op)
    }

    return precedence
}
</code></pre>

<p>Let's look at the code and then explain how it works:  </p>

<pre><code class="language-swift">enum ParseError: ErrorType {  
    ...
    case ExpectedOperator
}

func parseBinaryOp(node: ExprNode, exprPrecedence: Int = 0) throws -&gt; ExprNode {  
    var lhs = node
    while true {
        let tokenPrecedence = try getCurrentTokenPrecedence()
        if tokenPrecedence &lt; exprPrecedence {
            return lhs
        }

        guard case let Token.Other(op) = popCurrentToken() else {
            throw ParseError.ExpectedOperator
        }

        var rhs = try parsePrimary()
        let nextPrecedence = try getCurrentTokenPrecedence()

        if tokenPrecedence &lt; nextPrecedence {
            rhs = try parseBinaryOp(rhs, exprPrecedence: tokenPrecedence+1)
        }
        lhs = BinaryOpNode(op: op, lhs: lhs, rhs: rhs)
    }
}
</code></pre>

<p>We begin with a node which is potentially the beginning of a binary operation and repeat the following:</p>

<ul>
<li>Read the next token; </li>
<li>If it's not an operator, we're done</li>
<li>If it has lower precedence than the current one, we're done (it would have been handled earlier in the call stack)</li>
<li>Consume and read the next <code>PrimaryExpression</code> as <code>rhs</code></li>
<li>If the operator after has a higher precedence, recursively make a sub tree starting at <code>rhs</code> at the next precedence level</li>
<li>Form a sub-tree with the current operator</li>
</ul>

<p>The intuition behind this is that lower precedence operators tend to appear closer to the root of the tree because higher precedence operators form sub-trees with ever higher precedence operators.</p>

<p>Finally, we can finish <code>func parseExpression()</code> and you will see it's rather trivial:</p>

<pre><code>func parseExpression() throws -&gt; ExprNode {  
    let node = try parsePrimary()
    return try parseBinaryOp(node)
}
</code></pre>

<p>Now we can make a call to our parser:  </p>

<pre><code>let parser = Parser(tokens: tokens)  
do {  
    print(try parser.parseExpression())
}
catch {  
    print(error)
}
</code></pre>

<p>And we should get the following output (with indention added):</p>

<pre><code>BinaryOpNode(  
  +, 
  lhs: BinaryOpNode(
    +, 
    lhs: VariableNode(x), 
    rhs: BinaryOpNode(
      *, 
      lhs: VariableNode(y), 
      rhs: NumberNode(2.0)
      )
    ), 
  rhs: BinaryOpNode(
    /, 
    lhs: BinaryOpNode(
      +, 
      lhs: NumberNode(4.0), 
      rhs: NumberNode(5.0)
      ), 
    rhs: NumberNode(3.0)
    )
  )
</code></pre>

<p>Here's a more beautiful version of that tree with ASCII art:  </p>

<pre><code>        +
     ___|___
   /         \
  +          (/)
 / \         / \
x   *       +   3  
   / \     / \
  y   2   4   5
</code></pre>

<p>Once again here's what we were trying to parse:  </p>

<pre><code>x + y * 2 + (4 + 5) / 3  
</code></pre>

<p>Because this post is getting rather long, we'll examine other nodes in the AST (such as function calls and declarations) in the next post! You can find all this code on <a href="https://github.com/matthewcheok/Kaleidoscope">Github</a>. Stay tuned...</p>]]></content:encoded></item><item><title><![CDATA[A better NSNotificationCenter]]></title><description><![CDATA[<p><code>NSNotificationCenter</code> has been around for a long time. It let's you post arbitrary notifications and decouples the source of the action from the destination. There are a few flaws though:</p>

<ol>
<li>The notification name is a <code>String</code> type which is subject to typing errors or enforces a design pattern where you</li></ol>]]></description><link>http://blog.matthewcheok.com/a-better-nsnotificationcenter/</link><guid isPermaLink="false">2f4ab6f5-d1d1-4464-b7e4-0e902a9b263d</guid><category><![CDATA[swift]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Thu, 19 Nov 2015 02:08:38 GMT</pubDate><content:encoded><![CDATA[<p><code>NSNotificationCenter</code> has been around for a long time. It let's you post arbitrary notifications and decouples the source of the action from the destination. There are a few flaws though:</p>

<ol>
<li>The notification name is a <code>String</code> type which is subject to typing errors or enforces a design pattern where you would declare constants to avoid this.  </li>
<li>Consumers of the API are required to unregister when they are no longer interested in notifications.  </li>
<li>Parameters are passed via a <code>userInfo</code> dictionary and type information is lost in the process.</li>
</ol>

<p>We can solve problems <strong>1</strong> and <strong>3</strong> easily but simply requiring users to declare a <code>protocol</code>. This gives us a name to refer to this bit of functionality and also allows consumers to define the requirements (read parameters) of the notification. </p>

<p>The delegate design pattern makes uses of protocols but is usually a one-to-one relationship between the delegate and the consumer (instead of enabling action at a distance.) The second problem we can solve by storing the observers weakly via <code>NSHashTable</code>.</p>

<p>Here's how we would use such a solution:</p>

<pre><code class="language-swift">protocol TestProtocol {  
  func hello()
}
</code></pre>

<p>We'll declare the <code>protocol</code> that defines the functionality we want to expose.</p>

<pre><code class="language-swift">class TestClass: TestProtocol {  
  let name: String

  init(name: String) {
    self.name = name
  }

  func hello() {
    print("hello \(name)")
  }
}
</code></pre>

<p>Next, we implement the protocol in a <code>class</code>.</p>

<pre><code class="language-swift">let object = TestClass(name: "First")  
let object2 = TestClass(name: "Second")  
</code></pre>

<p>Suppose we instantiate several instances of the class.</p>

<pre><code class="language-swift">NotificationManager.sharedManager.add(TestProtocol.self, observer: object)  
NotificationManager.sharedManager.add(TestProtocol.self, observer: object2)  
</code></pre>

<p>We'll subscribe to notifications via the <code>func add()</code> method.</p>

<pre><code class="language-swift">NotificationManager.sharedManager.make(TestProtocol.self) { (item) -&gt; Void in  
  item.hello()
}
</code></pre>

<p>Here's how we can fire off notifications. We call the methods in the <code>protocol</code> inside of a closure and it gets fired on all observers.</p>

<pre><code>hello First  
hello Second  
</code></pre>

<p>The output shows the desired result.</p>

<p>Let's explore how we can build such a solution:  </p>

<pre><code class="language-swift">class NotificationManager  
{
  static let sharedManager = NotificationManager()
  var observersMap = [String:NSHashTable]()
}
</code></pre>

<p>We start by having a similar way to access a singleton if necessary. Next, we need a way to add observers to our dictionary, <code>observersMap</code>.</p>

<pre><code class="language-swift">func add&lt;T&gt;(type: T.Type, observer: T) {  
  let typeString = "\(T.self)"
  let observers: NSHashTable

  // get the weak set of observers matching this type
  if let set = observersMap[typeString] {
    observers = set
  } else {
    observers = NSHashTable.weakObjectsHashTable()
    observersMap[typeString] = observers
  }

  observers.addObject(observer as? AnyObject)
}
</code></pre>

<p>We can do this by writing a generic method. It takes as parameters a type <code>T</code> and an observer conforming to <code>T</code>. Next, we produce a <code>String</code> matching the name of <code>T</code> because the key to a dictionary needs to be <code>Hashable</code> and <code>String</code> conforms to that.</p>

<p>We check to see if we already have a <code>NSHashTable</code> matching that type. If we don't we'll just instantiate one and add it to the dictionary. Finally we add our observer to it. Note that our observer needs to be an object, because the hash table is an Objective-C class.</p>

<p>Now we're ready to write our <code>func make()</code> method. This method makes the observers registered perform a given closure. We've specified the closure to be <code>@noescape</code>. This means it will not out-live the <code>func make()</code> method and we won't have to worry about capturing references.</p>

<pre><code class="language-swift">func make&lt;T&gt;(type: T.Type, @noescape closure: T -&gt; Void) {  
  let typeString = "\(T.self)"
  if let set = observersMap[typeString] {
    for observer in set.allObjects {
      closure(observer as! T)
    }
  }
}
</code></pre>

<p>Let me know what you think and if this is a better solution than <code>NSNotificationCenter</code>!</p>]]></content:encoded></item><item><title><![CDATA[Writing a lexer in Swift]]></title><description><![CDATA[<p>In this series, we'll attempt to write parts of a compiler for a simplified language, <strong>Kaleidoscope</strong>. If you're interested in following along in <strong>C++</strong> or <strong>Objective Caml</strong>, you can find the original tutorial <a href="http://llvm.org/docs/tutorial/index.html">here</a> on the LLVM website. </p>

<p>The goal here is not to write the most efficient implementation but</p>]]></description><link>http://blog.matthewcheok.com/writing-a-lexer-in-swift/</link><guid isPermaLink="false">92859e8c-84d6-4005-97ab-a95ed7e56590</guid><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Tue, 17 Nov 2015 04:19:27 GMT</pubDate><content:encoded><![CDATA[<p>In this series, we'll attempt to write parts of a compiler for a simplified language, <strong>Kaleidoscope</strong>. If you're interested in following along in <strong>C++</strong> or <strong>Objective Caml</strong>, you can find the original tutorial <a href="http://llvm.org/docs/tutorial/index.html">here</a> on the LLVM website. </p>

<p>The goal here is not to write the most efficient implementation but how we can leverage features in Swift to make ourselves easily understood.</p>

<p>A compiler can typically be thought of as a series of modular steps including lexical analysis, parsing, semantic analysis, optimisation and code generation.</p>

<p>In the lexical analysis phase, we simply try to break up the input (source code) into the small units called <strong>lexemes</strong>. These units carry specific meaning which we can categorise into groups of <strong>tokens</strong>.</p>

<p>Consider the following code snippet:  </p>

<pre><code># Compute the x'th fibonacci number.
def fib(x)  
  if x &lt; 3 then
    1
  else
    fib(x-1)+fib(x-2)

# This expression will compute the 40th number.
fib(40)  
</code></pre>

<p>If you've seen any sort of programming language before, you'll immediately recognise the <strong>keywords</strong> such as <code>def</code>, <code>if</code>, <code>then</code>, and <code>else</code>. In addition, you might also notice <code>fib</code> and <code>x</code> are <strong>identifiers</strong>. Even though, they are not the same string, we see that they are names for things (namely functions and variables.)</p>

<p>Here's a simpler snippet:  </p>

<pre><code>def foo(x, y)  
  x + y * 2 + (4 + 5) / 3

foo(3, 4)  
</code></pre>

<p>Our lexer should be able to scan through code and return a list of tokens that describe it. At this stage, it's not necessary to interpret the code in any way. We just want to identify different parts of the source and label them.</p>

<p>Let's jump in and try to determine what tokens we need to describe the language:</p>

<pre><code class="language-swift">enum Token {  
    case Define
    case Identifier(String)
    case Number(Float)
    case ParensOpen
    case ParensClose
    case Comma
    case Other(String)
}
</code></pre>

<p>Because each group of lexemes or token has a clear purpose, we can represent them via the <code>enum</code> in Swift. Not all tokens are equal, however. Certain structures like <code>Identifier</code>s and <code>Number</code>s, also carry additional information.</p>

<p>Next, we'll try to write a program to turn the above snippet into the following output:  </p>

<pre><code>[
.Define,
.Identifier("foo"),
.ParensOpen,
.Identifier("x"),
.Comma,
.Identifier("y"),
.ParensClose,
.Identifier("x"),
.Other("+"),
.Identifier("y"),
.Other("*"),
.Number(2.0),
.Other("+"),
.ParensOpen,
.Number(4.0),
.Other("+"),
.Number(5.0),
.ParensClose,
.Other("/"),
.Number(3.0),
.Identifier("foo"),
.ParensOpen,
.Number(3.0),
.Comma,
.Number(4.0),
.ParensClose,
]
</code></pre>

<p>One way we can try to do this is via <strong>regular expressions</strong>. Each token can be recognised by characters it is made out of. For instance, the <code>Define</code> token is always the characters <code>def</code> in that order. <code>Identifier</code>s, however, follow a rule such as 1) any sequence of alphanumeric characters 2) not beginning with a digit.</p>

<p>For each token, we'll write a regular expression capable of matching its corresponding lexeme. Next, we need to generate the <code>enum</code> that matches the token. We can put this all together rather concisely as an array of tuples.</p>

<p>The first parameter in the tuple represents the regular expression we want to match at the beginning of the context and the second parameter is a closure that will generate the relevant token enumeration.</p>

<pre><code>typealias TokenGenerator = (String) -&gt; Token?  
let tokenList: [(String, TokenGenerator)] = [  
    ("[ \t\n]", { _ in nil }),
    ("[a-zA-Z][a-zA-Z0-9]*", { $0 == "def" ? .Define : .Identifier($0) }),
    ("[0-9.]+", { (r: String) in .Number((r as NSString).floatValue) }),
    ("\\(", { _ in .ParensOpen }),
    ("\\)", { _ in .ParensClose }),
    (",", { _ in .Comma }),
]
</code></pre>

<p>The first rule captures whitespace which can be <code></code> spaces, <code>\t</code> tabs or <code>\n</code> line breaks.</p>

<p>Finally, we are ready to write our lexer. We'll try to match against any of the rules in our <code>tokenList</code>, failing which we will just assign the character to <code>.Other</code>.</p>

<p>Here's the method of interest:  </p>

<pre><code>func tokenize(input: String) -&gt; [Token] {  
    var tokens = [Token]()
    var content = input

    while (content.characters.count &gt; 0) {
        var matched = false

        for (pattern, generator) in tokenList {
            if let m = content.match(pattern) {
                if let t = generator(m) {
                    tokens.append(t)
                }

                content = content.substringFromIndex(content.startIndex.advancedBy(m.characters.count))
                matched = true
                break
            }
        }

        if !matched {
            let index = content.startIndex.advancedBy(1)
            tokens.append(.Other(content.substringToIndex(index)))
            content = content.substringFromIndex(index)
        }
    }
    return tokens
}
</code></pre>

<p>That's it for now! If you want to take a look at more code, this is all on <a href="https://github.com/matthewcheok/Kaleidoscope">Github</a>. In the next post, we'll look at parsing and how we can build the <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">AST</a>.</p>

<hr>

<p>To keep things simple we used a <code>String</code> extension to provide regex functionality via <code>func match(String) -&gt; String?</code>. This is simply syntactic sugar over the <code>NSRegularExpression</code> API in <code>Foundation</code>:</p>

<pre><code>var expressions = [String: NSRegularExpression]()  
public extension String {  
    public func match(regex: String) -&gt; String? {
        let expression: NSRegularExpression
        if let exists = expressions[regex] {
            expression = exists
        } else {
            expression = try! NSRegularExpression(pattern: "^\(regex)", options: [])
            expressions[regex] = expression
        }

        let range = expression.rangeOfFirstMatchInString(self, options: [], range: NSMakeRange(0, self.utf16.count))
        if range.location != NSNotFound {
            return (self as NSString).substringWithRange(range)
        }
        return nil
    }
}
</code></pre>]]></content:encoded></item><item><title><![CDATA[A better <img> tag]]></title><description><![CDATA[<p>There's usually two solutions to embedding images into your web content:</p>

<p>1) Use the HTML <code>&lt;img&gt;</code> tag <br>
This means you can set its <code>height</code> and <code>width</code> which could result in a non-uniform scaling applied to each dimension.</p>

<p>2) Apply <code>background-image</code> via CSS <br>
You have more control over how</p>]]></description><link>http://blog.matthewcheok.com/a-better-img-tag/</link><guid isPermaLink="false">e4fab99a-e417-46a3-97d1-b80ca90e7578</guid><category><![CDATA[web]]></category><category><![CDATA[html]]></category><category><![CDATA[css]]></category><category><![CDATA[react]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Sat, 18 Jul 2015 12:53:07 GMT</pubDate><content:encoded><![CDATA[<p>There's usually two solutions to embedding images into your web content:</p>

<p>1) Use the HTML <code>&lt;img&gt;</code> tag <br>
This means you can set its <code>height</code> and <code>width</code> which could result in a non-uniform scaling applied to each dimension.</p>

<p>2) Apply <code>background-image</code> via CSS <br>
You have more control over how the image is displayed but now you have to consider classes and how you would apply them to your HTML content.</p>

<p>With React, we can simplify this by abstracting the implementation details into a reusable component. Ideally we want to use our component as if it was regular HTML markup:</p>

<pre><code>&lt;Image src={url} width={500} height={300} mode='fit' /&gt;  
</code></pre>

<p>This bit of JSX suggests we want to be able to set the image <code>src</code>, and dimensions via <code>width</code> and <code>height</code>. We also have <code>mode</code> which may be specified as <code>fit</code> or <code>fill</code>. This allows us to scale proportionally and crop the image appropriately.</p>

<p>Conveniently, CSS also exposes a similar attribute via <code>background-size</code> which can be specified as <code>contain</code> or <code>cover</code>.</p>

<p>Let's start with a typical React component template:</p>

<pre><code>import React, {Component} from 'react';

export default class Image extends Component {  
  render() {
    return &lt;div /&gt;
  }
}
</code></pre>

<p>Next we'll extract the necessary props and setup the CSS attributes as inline styles on our <code>&lt;div /&gt;</code>:</p>

<pre><code>import React, {Component} from 'react';

export default class Image extends Component {  
  render() {
    let {mode, src, height, width, style, ...props} = this.props;
    let modes = {
      'fill': 'cover',
      'fit': 'contain'
    };
    let size = modes[mode] || 'contain';

    let important = {
      backgroundImage: `url("${src}")`,
      backgroundSize: size,
      backgroundPosition: 'center center',
      backgroundRepeat: 'no-repeat'
    };

    return &lt;div {...props} style={{...style, ...important}} /&gt;
  }
}
</code></pre>

<p>Here we're doing a couple of things:</p>

<p>1) We're using ES6's <strong>destructuring assignment</strong> feature to extract the props we're specifically interested to handle and coalesce the remaining props into the <code>props</code> variable.</p>

<p>2) We setup the CSS styles we require (mostly <code>background-</code> attributes.) ES6's <strong>template strings</strong> allow us to interpolate the url from <code>src</code> easily.</p>

<p>3) We use the <strong>spread operator</strong> <code>...</code> to expand <code>props</code> into the <code>&lt;div /&gt;</code> element and also to merge <code>style</code>, which contains inline styles the parent component might have specified, with <code>important</code>, which contains the CSS styles needed for displaying the image. The order here is crucial because we want keys from <code>important</code> to override the same keys if they are specified in <code>style</code>.</p>

<p>Finally to make our component a bit more robust, let's add some default styles if they are not specified by the parent component:</p>

<pre><code>import React, {Component} from 'react';

export default class Image extends Component {  
  render() {
    let {mode, src, height, width, style, ...props} = this.props;
    let modes = {
      'fill': 'cover',
      'fit': 'contain'
    };
    let size = modes[mode] || 'contain';

    let defaults = {
      height: height || 100,
      width: width || 100,
      backgroundColor: 'gray'
    };

    let important = {
      backgroundImage: `url("${src}")`,
      backgroundSize: size,
      backgroundPosition: 'center center',
      backgroundRepeat: 'no-repeat'
    };

    return &lt;div {...props} style={{...defaults, ...style, ...important}} /&gt;
  }
}
</code></pre>

<p>This fixes the issue whereby if no <code>height</code> and <code>width</code> (or corresponding styles) are specified, we would end up with a zero-by-zero element. And there we have a functional and complete React element!</p>]]></content:encoded></item><item><title><![CDATA[Building generic components in React]]></title><description><![CDATA[<p>Communication between components in React is typically in one direction, i.e. from parent components to child components. We largely use <code>props</code> to pass information like this:</p>

<pre><code>&lt;MyVeryAwesomeComponent title="This is simply amazing!" /&gt;  
</code></pre>

<p>We can use <code>props</code> to solve the problem of building more general components by abstracting</p>]]></description><link>http://blog.matthewcheok.com/building-generic-components-in-react/</link><guid isPermaLink="false">02ffb0dd-65e5-456f-b849-8b88ea8f6636</guid><category><![CDATA[web]]></category><category><![CDATA[react]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Fri, 10 Jul 2015 04:02:27 GMT</pubDate><content:encoded><![CDATA[<p>Communication between components in React is typically in one direction, i.e. from parent components to child components. We largely use <code>props</code> to pass information like this:</p>

<pre><code>&lt;MyVeryAwesomeComponent title="This is simply amazing!" /&gt;  
</code></pre>

<p>We can use <code>props</code> to solve the problem of building more general components by abstracting away more specific markup. Say we have a list of items - we can hide the implementation details by building a child component like this:</p>

<pre><code>export default React.createClass({  
  render() {
    let items = this.props.items || []
    let rows = items.map(
      item =&gt; {
        return (
          &lt;tr&gt;
            &lt;td&gt;{item.title}&lt;/td&gt;
          &lt;/tr&gt;
        );
      }
    );

    return (
      &lt;table&gt;
        {rows}
      &lt;/table&gt;
    );
  }
});
</code></pre>

<p>This way we can use this component like this:</p>

<pre><code>&lt;List items={items} /&gt;  
</code></pre>

<p>Now let's say we want to have 2 similar components like follows:</p>

<p class="shadowed" align="center">  
<img src="http://blog.matthewcheok.com/content/images/2015/07/custom.png" alt="">
</p>

<p>What do we do? We could duplicate this component and make the change in the one column, but that would violate <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>.</p>

<p>Instead, we can pass in the customizable portion in via <code>props</code>:</p>

<pre><code>export default React.createClass({  
  render() {
    let items = this.props.items || []    
    let customComponent = this.props.customComponent || &lt;span /&gt;

    let rows = items.map(
      item =&gt; {
        return (
          &lt;tr&gt;
            &lt;td&gt;{item.title}&lt;/td&gt;
            &lt;td&gt;{customComponent}&lt;/td&gt;
          &lt;/tr&gt;
        );
      }
    );

    return (
      &lt;table&gt;
        {rows}
      &lt;/table&gt;
    );
  }
});
</code></pre>

<p>Now we can create components like this:</p>

<pre><code>&lt;List items={items} customComponent={  
  &lt;button&gt;ADD&lt;/button&gt;
} /&gt;

&lt;List items={items} customComponent={  
  &lt;div&gt;
    &lt;button&gt;APPROVE&lt;/button&gt;&lt;br /&gt;
    &lt;button&gt;REJECT&lt;/button&gt;
  &lt;/div&gt;
} /&gt;
</code></pre>

<p>This is an improvement over plain copy and paste, but now we've got a different problem. We can add an event handler (like <code>onClick</code>) to our <code>customComponent</code> but we've lost a the reference to the <code>item</code> in the table row.</p>

<p>To solve this, we need a way to communicate back from our child component to our parent component, since we map over items within the child.</p>

<p>We can do that by turning our <code>prop</code> into a <code>callback</code> like this:</p>

<pre><code>export default React.createClass({  
  render() {
    let items = this.props.items || [];
    let customComponentGenerator = this.props.customComponentGenerator || function(){ return &lt;span /&gt; };

    let rows = items.map(
      item =&gt; {
        return (
          &lt;tr key={item.title}&gt;
            &lt;td&gt;{item.title}&lt;/td&gt;
            &lt;td&gt;{customComponentGenerator(item)}&lt;/td&gt;
          &lt;/tr&gt;
        );
      }
    );

    return (
      &lt;table&gt;
        {rows}
      &lt;/table&gt;
    );
  }
});
</code></pre>

<p>Now we can adjust the parent like this:</p>

<pre><code>&lt;List items={items} customComponentGenerator={  
  (item) =&gt; {
    return (
      &lt;button onClick={
        ()=&gt;console.log('add '+item.title)}&gt;
        ADD
      &lt;/button&gt;
      );
  }
} /&gt;

&lt;List items={items} customComponentGenerator={  
  (item) =&gt; {
    return (
      &lt;div&gt;
        &lt;button onClick={
         ()=&gt;console.log('approve '+item.title)}&gt;
         APPROVE
        &lt;/button&gt;&lt;br /&gt;
        &lt;button onClick={
          ()=&gt;console.log('reject '+item.title)}&gt;
          REJECT
        &lt;/button&gt;
      &lt;/div&gt;
      );
  }
} /&gt;
</code></pre>

<p>And that's a couple of ways we can build more generic components in React!</p>]]></content:encoded></item><item><title><![CDATA[From UIKit to Web]]></title><description><![CDATA[<p>In this short series, we explore how to tackle some UI questions for the web with respect to how things are done on iOS.</p>

<p>Let's revisit a loading spinner animation we've worked on before. If you want to see how this is implemented on iOS, check it out <a href="http://blog.matthewcheok.com/design-teardown-spinning-indicator/">here</a>.</p>

<p>Here's</p>]]></description><link>http://blog.matthewcheok.com/from-uikit-to-web/</link><guid isPermaLink="false">db39745d-d466-45da-a5b9-cb6423d17ac8</guid><category><![CDATA[web]]></category><category><![CDATA[html]]></category><category><![CDATA[css]]></category><category><![CDATA[UI]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Thu, 09 Jul 2015 00:46:24 GMT</pubDate><content:encoded><![CDATA[<p>In this short series, we explore how to tackle some UI questions for the web with respect to how things are done on iOS.</p>

<p>Let's revisit a loading spinner animation we've worked on before. If you want to see how this is implemented on iOS, check it out <a href="http://blog.matthewcheok.com/design-teardown-spinning-indicator/">here</a>.</p>

<p>Here's what we're aiming for:</p>

<div class="centered">  
  <div class="container container-rotate inlined">
    <div class="masking pushed">
      <div class="wedge wedge1 wedge1-animation-delayed"></div>
    </div>
    <div class="masking">
      <div class="wedge wedge2 wedge2-animation-delayed"></div>
    </div>
    <div class="overlay"></div>
  </div>
</div>

<h3 id="circles">Circles</h3>

<p>Let's start by rendering something circular on the screen. We can do this using the <code>border-radius</code> attribute:</p>

<p><code>HTML</code></p>

<pre><code>&lt;div class="circle"&gt;&lt;/div&gt;  
</code></pre>

<p><code>CSS</code></p>

<pre><code>.circle {
  display: inline-block;
  background: #444;
  height: 80px;
  width: 80px;
  border-radius: 40px;
}
</code></pre>

<p>Here's what we get:  </p>

<div class="centered">  
  <div class="circle inlined"></div>
</div>

<p>By setting the border radius of each quarter to the half the size of the element, we've rounded the square into a circle.</p>

<p>This is a great start but we don't have any ability to animate its appearance. To do any useful animation at all, we should consider semi-circle and apply some clipping to it. (If you want some additional reading, check out <a href="https://cssanimation.rocks/watch/">this link</a>.)</p>

<h3 id="semicircles">Semi-circles</h3>

<p>Let's create a semi-cirle:</p>

<p><code>HTML</code></p>

<pre><code>&lt;div class="container"&gt;  
  &lt;div class="wedge"&gt;&lt;/div&gt;
&lt;/div&gt;  
</code></pre>

<p><code>CSS</code></p>

<pre><code>.container {
  height: 80px;
  width: 80px;
  position: relative;
  display: inline-block;
}

.wedge {
  background: #444;
  height: 80px;
  width: 40px;
  border-radius: 0 40px 40px 0;
}
</code></pre>

<p>Here's what we get:</p>

<div class="centered">  
  <div class="container inlined">
    <div class="absolutely pushed">
      <div class="wedge wedge1"></div>
    </div>
  </div>
</div>

<p>Again we use the <code>border-radius</code> attribute, but we've specified <code>0</code> for the top-left and bottom-left.</p>

<h3 id="animatingsemicircles">Animating semi-circles</h3>

<p>Next let's apply an animation:</p>

<p><code>HTML</code></p>

<pre><code>&lt;div class="container"&gt;  
  &lt;div class="wedge"&gt;&lt;/div&gt;
&lt;/div&gt;  
</code></pre>

<p><code>CSS</code></p>

<pre><code>.container {
  ...
}

.wedge {
  background: #444;
  height: 80px;
  width: 40px;
  border-radius: 0 40px 40px 0;
  animation: wedge-animation 1.5s infinite linear;
  transform-origin: 0 50%;
}

@keyframes wedge-animation {
  0% {
    transform: rotateZ(-180deg);
  }
  50% {
    transform: rotateZ(0deg);
  }
  100% {
    transform: rotateZ(180deg);    
  }
}
</code></pre>

<p>Here's what we get:</p>

<div class="centered">  
  <div class="container inlined">
    <div class="absolutely pushed">
      <div class="wedge wedge1 wedge1-animation-nodelay"></div>
    </div>
  </div>
</div>

<p>We've setup a key-frame animation with the <code>@keyframes</code> keyword. To produce this rotation, we start at <code>-180</code> degrees initially (<code>0%</code>), rotate to <code>0</code> degrees halfway through (<code>50%</code>) and finally end at <code>180</code> degrees (<code>100%</code>).</p>

<p>Next, we add this animation to our <code>wedge</code> class with a <code>duration</code> of <code>1.5</code> seconds, with a <code>linear</code> curve. We also move the <code>transform-origin</code> to the left-most position so it seems to rotate about the center of our "circle".</p>

<h3 id="clippingthesemicircle">Clipping the semi-circle</h3>

<p>We'll apply a clipping mask. We do this by wrapping our semi-circle in a parent <code>&lt;div/&gt;</code>:</p>

<p><code>HTML</code></p>

<pre><code>&lt;div class="container"&gt;  
  &lt;div class="masking"&gt;
    &lt;div class="wedge"&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;  
</code></pre>

<p><code>CSS</code></p>

<pre><code>.container {
  ...
}

.masking {
  width: 40px;
  overflow: hidden;
}

.wedge {
  ...
}

@keyframes wedge-animation {
  ...
}
</code></pre>

<p>Here's what we get:</p>

<div class="centered">  
  <div class="container inlined">
    <div class="masking pushed">
      <div class="wedge wedge1 wedge1-animation-nodelay"></div>
    </div>
  </div>
</div>

<p>Because we've made the parent <code>&lt;div/&gt;</code> as wide as the radius of the circle, as the semi-circle rotates past the origin it gets clipped. This is the <strong>crucial</strong> bit that makes the animation work - the shape seems to grow and shrink as it gets clipped.</p>

<h3 id="fullcircle">Full circle</h3>

<p>Next, we'll add the other half semi-circle:</p>

<p><code>HTML</code></p>

<pre><code>&lt;div class="container"&gt;  
  &lt;div class="masking pushed"&gt;
    &lt;div class="wedge wedge1"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="masking"&gt;
    &lt;div class="wedge wedge2"&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;  
</code></pre>

<p><code>CSS</code></p>

<pre><code>...

.pushed {
  left: 40px;
}

.wedge {
  background: #444;
  height: 80px;
  width: 40px;
}

.wedge1 {
  border-radius: 0 40px 40px 0;
  animation: wedge-animation 1.5s infinite linear;
  transform-origin: 0 50%;
}

.wedge2 {
  border-radius: 40px 0 0 40px;
  animation: wedge-animation 1.5s infinite linear;
  transform-origin: 100% 50%;
}

@keyframes wedge-animation {
  ...
}
</code></pre>

<p>Here's what we get:</p>

<p><div class="centered">
   <div class="container inlined">
     <div class="masking pushed">
       <div class="wedge wedge1 wedge1-animation-nodelay"></div>
     </div>
     <div class="masking">
       <div class="wedge wedge2 wedge2-animation-nodelay"></div>
     </div>
   </div>
 </div></p>

<p>So we've duplicated our <code>masking</code> and <code>wedge</code> classes and specialized the <code>wedge</code> animation into <code>wedge1</code> and <code>wedge2</code> for each half of the circle. There's also a <code>pushed</code> class applied to <code>wedge1</code> to move it over by the radius (it is the right semi-circle.)</p>

<h3 id="animationtiming">Animation timing</h3>

<p>The problem now is that we're using the exact same animation of each semi-circle. We need to delay the left semi-circle so both halves animate together seamlessly.</p>

<p><code>HTML</code></p>

<pre><code>&lt;div class="container"&gt;  
  &lt;div class="masking pushed"&gt;
    &lt;div class="wedge wedge1"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="masking"&gt;
    &lt;div class="wedge wedge2"&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;  
</code></pre>

<p><code>CSS</code></p>

<pre><code>...

.wedge1 {
  border-radius: 0 40px 40px 0;
  animation: wedge1-animation 3.0s infinite linear;
  transform-origin: 0 50%;
}

.wedge2 {
  border-radius: 40px 0 0 40px;
  animation: wedge2-animation 3.0s infinite linear;
  transform-origin: 100% 50%;
}

@keyframes wedge1-animation {
  0% {
    transform: rotateZ(-180deg);
  }
  25%, 50% {
    transform: rotateZ(0deg);
  }
  75%, 100% {
    transform: rotateZ(180deg);
  }
}

@keyframes wedge2-animation {
  0%, 25% {
    transform: rotateZ(-180deg);
  }
  50%, 75% {
    transform: rotateZ(0deg);
  }
  100% {
    transform: rotateZ(180deg);
  }
}
</code></pre>

<p>Here's what we get:</p>

<p><div class="centered">
   <div class="container inlined">
     <div class="masking pushed">
       <div class="wedge wedge1 wedge1-animation-delayed"></div>
     </div>
     <div class="masking">
       <div class="wedge wedge2 wedge2-animation-delayed"></div>
     </div>
   </div>
 </div></p>

<p>What we've done is have the left semi-circle do nothing the first quarter duration and the right half animate in. In the next quarter, the right half does nothing and the left animates in. Finally, we animate the right half out, followed by the left. We've also doubled the animation duration to account for the delays.</p>

<h3 id="fromsectortoedge">From sector to edge</h3>

<p>Next, we want to remove part of the circle such that we see only a thin border around the edge. We can't apply a circular mask, however. We can fake this by positioning another slightly smaller circle over the top of this shape.</p>

<p><code>HTML</code></p>

<pre><code>&lt;div class="container"&gt;  
  &lt;div class="masking pushed"&gt;
    &lt;div class="wedge wedge1"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="masking"&gt;
    &lt;div class="wedge wedge2"&gt;&lt;/div&gt;
  &lt;/div&gt;
   &lt;div class="overlay"&gt;&lt;/div&gt;
&lt;/div&gt;  
</code></pre>

<p><code>CSS</code></p>

<pre><code>...

.overlay {
  background: white;
  border-radius: 50%;
  width: 76px;
  height: 76px;
  position: absolute;
  top: 2px;
  left: 2px;
}
</code></pre>

<div class="centered">  
 <div class="container inlined">
   <div class="masking pushed">
     <div class="wedge wedge1 wedge1-animation-delayed"></div>
   </div>
   <div class="masking">
     <div class="wedge wedge2 wedge2-animation-delayed"></div>
   </div>
   <div class="overlay"></div>
 </div>
</div>

<p>We've added another <code>&lt;div/&gt;</code> which is <code>4px</code> smaller in size. The <code>border-radius</code> is set to <code>50%</code> which is half of the size. Finally we've set the <code>background-color</code> and positioned it <code>absolute</code>ly to center it with respect to the circle behind.</p>

<h3 id="rotatingeverything">Rotating everything</h3>

<p>Finally, we'll apply a rotation to the entire hierarchy to get the look we're after.</p>

<p><code>HTML</code></p>

<pre><code>&lt;div class="container container-rotate"&gt;  
  &lt;div class="masking pushed"&gt;
    &lt;div class="wedge wedge1"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="masking"&gt;
    &lt;div class="wedge wedge2"&gt;&lt;/div&gt;
  &lt;/div&gt;
   &lt;div class="overlay"&gt;&lt;/div&gt;
&lt;/div&gt;  
</code></pre>

<p><code>CSS</code></p>

<pre><code>...

.container-rotate {
  animation: container-rotate 3.0s infinite linear;
}

@keyframes container-rotate {
  0% {
    transform: rotateZ(0deg);
  }
  100% {
    transform: rotateZ(360deg);
  }
}
</code></pre>

<div class="centered">  
  <div class="container container-rotate inlined">
    <div class="masking pushed">
      <div class="wedge wedge1 wedge1-animation-delayed"></div>
    </div>
    <div class="masking">
      <div class="wedge wedge2 wedge2-animation-delayed"></div>
    </div>
    <div class="overlay"></div>
  </div>
</div>

<p>That's it! I hope you've enjoyed this post. In the following posts, we'll see how we can clean up our CSS and wrap our markup into a reusable component.</p>

<style>  
  .centered {
    text-align: center;
    margin: 20px;
  }

  .inlined {
    display: inline-block;
  }

  .absolutely {
    position: absolute !important;
  }

  .circle {
    background: #444;
    height: 80px;
    width: 80px;
    border-radius: 40px;
  }

  .container {
    height: 80px;
    width: 80px;
    position: relative;
  }

  .masking {
    height: 80px;
    width: 40px;
    overflow: hidden;
    position: absolute;
  }

  .pushed {
    left: 40px;
  }

  .wedge {
    background: #444;
    height: 80px;
    width: 40px;
  }

  .wedge1 {
    border-radius: 0 40px 40px 0;
  }

  .wedge1-animation-nodelay {
    animation: wedge-animation-nodelay 1.5s infinite linear;
    -webkit-animation: wedge-animation-nodelay 1.5s infinite linear;
    transform-origin: 0 50%;
    -webkit-transform-origin: 0 50%;
  }

  @keyframes wedge-animation-nodelay {
    0% {
      transform: rotateZ(-180deg);
    }
    50% {
      transform: rotateZ(0deg);
    }
    100% {
      transform: rotateZ(180deg);    
    }
  }

  @-webkit-keyframes wedge-animation-nodelay {
    0% {
      -webkit-transform: rotateZ(-180deg);
    }
    50% {
      -webkit-transform: rotateZ(0deg);
    }
    100% {
      -webkit-transform: rotateZ(180deg);    
    }
  }

  .wedge1-animation-delayed {
    animation: wedge1-animation-delayed 3.0s infinite linear;
    -webkit-animation: wedge1-animation-delayed 3.0s infinite linear;
    transform-origin: 0 50%;
    -webkit-transform-origin: 0 50%;
  }

  @keyframes wedge1-animation-delayed {
    0% {
      transform: rotateZ(-180deg);
    }
    25%, 50% {
      transform: rotateZ(0deg);
    }
    75%, 100% {
      transform: rotateZ(180deg);    
    }
  }

  @-webkit-keyframes wedge1-animation-delayed {
    0% {
      -webkit-transform: rotateZ(-180deg);
    }
    25%, 50% {
      -webkit-transform: rotateZ(0deg);
    }
    75%, 100% {
      -webkit-transform: rotateZ(180deg);    
    }
  }

  .wedge2 {
    border-radius: 40px 0 0 40px;
  }

  .wedge2-animation-nodelay {
    animation: wedge-animation-nodelay 1.5s infinite linear;
    -webkit-animation: wedge-animation-nodelay 1.5s infinite linear;
    transform-origin: 100% 50%;
    -webkit-transform-origin: 100% 50%;
  }

  .wedge2-animation-delayed {
    animation: wedge2-animation-delayed 3.0s infinite linear;
    -webkit-animation: wedge2-animation-delayed 3.0s infinite linear;
    transform-origin: 100% 50%;
    -webkit-transform-origin: 100% 50%;
  }

  @keyframes wedge2-animation-delayed {
    0%, 25% {
      transform: rotateZ(-180deg);
    }
    50%, 75% {
      transform: rotateZ(0deg);
    }
    100% {
      transform: rotateZ(180deg);    
    }
  }

  @-webkit-keyframes wedge2-animation-delayed {
    0%, 25% {
      -webkit-transform: rotateZ(-180deg);
    }
    50%, 75% {
      -webkit-transform: rotateZ(0deg);
    }
    100% {
      -webkit-transform: rotateZ(180deg);    
    }
  }

  .overlay {
    background: white;
    border-radius: 50%;
    width: 76px;
    height: 76px;
    position: absolute;
    top: 2px;
    left: 2px;
  }

  .container-rotate {
    animation: container-rotate 3.0s infinite linear;
    -webkit-animation: container-rotate 3.0s infinite linear;
  }

  @keyframes container-rotate {
    0% {
      transform: rotateZ(0deg);
    }
    100% {
      transform: rotateZ(360deg);
    }
  }

  @-webkit-keyframes container-rotate {
    0% {
      -webkit-transform: rotateZ(0deg);
    }
    100% {
      -webkit-transform: rotateZ(360deg);
    }
  }
</style>]]></content:encoded></item><item><title><![CDATA[Working with URLs]]></title><description><![CDATA[<p>It's cumbersome to work with <code>NSURL</code> with all its verbosity. Here's a sweet way to make that all better with Swift.</p>

<pre><code>import Foundation

func +(lhs: NSURL, rhs:String) -&gt; NSURL {  
    return lhs.URLByAppendingPathComponent(rhs)
}
</code></pre>

<p>This way you can simply use:</p>

<pre><code>let url = NSURL(string: "http://google.com")!  
url \\ "http:</code></pre>]]></description><link>http://blog.matthewcheok.com/working-with-urls/</link><guid isPermaLink="false">5af27996-87e9-4206-86d4-fceed7c4b17d</guid><category><![CDATA[swift]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Wed, 08 Jul 2015 01:56:06 GMT</pubDate><content:encoded><![CDATA[<p>It's cumbersome to work with <code>NSURL</code> with all its verbosity. Here's a sweet way to make that all better with Swift.</p>

<pre><code>import Foundation

func +(lhs: NSURL, rhs:String) -&gt; NSURL {  
    return lhs.URLByAppendingPathComponent(rhs)
}
</code></pre>

<p>This way you can simply use:</p>

<pre><code>let url = NSURL(string: "http://google.com")!  
url \\ "http://google.com"  
url + "pages" \\ "http://google.com/pages"  
</code></pre>]]></content:encoded></item><item><title><![CDATA[JSON and Swift]]></title><description><![CDATA[<p>Many have tried to tackle the issue and there're now some really good solutions. Here are just a handful of them and there are 2 main categories.</p>

<p>Some solutions make it really easy to make sense of JSON data. This is significant because Swift's strong type-safety makes it hard to</p>]]></description><link>http://blog.matthewcheok.com/json-and-swift/</link><guid isPermaLink="false">3c7f763a-939f-4172-9ffd-7098164adc6c</guid><category><![CDATA[swift]]></category><category><![CDATA[json]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Wed, 24 Jun 2015 09:00:17 GMT</pubDate><content:encoded><![CDATA[<p>Many have tried to tackle the issue and there're now some really good solutions. Here are just a handful of them and there are 2 main categories.</p>

<p>Some solutions make it really easy to make sense of JSON data. This is significant because Swift's strong type-safety makes it hard to work with JSON without much explicit casting:</p>

<ul>
<li><a href="https://github.com/owensd/json-swift"><code>json-swift</code></a></li>
<li><a href="https://github.com/SwiftyJSON/SwiftyJSON"><code>SwiftyJSON</code></a></li>
</ul>

<p>Others go a step further and help make it easy to work with actual data models. They help to decode JSON strings or objects into native types (read <code>Structs</code> or <code>Classes</code>):</p>

<ul>
<li><a href="https://github.com/thoughtbot/Argo"><code>Argo</code></a></li>
<li><a href="https://github.com/Hearst-DD/ObjectMapper"><code>ObjectMapper</code></a></li>
<li><a href="https://github.com/isair/JSONHelper"><code>JSONHelper</code></a></li>
</ul>

<p>After a brief survey of available options, it felt like something was missing. We didn't have a complete solution that made the round trip - one that is able to produce native types from JSON and also JSON from native types.</p>

<p>Part of the reason, I suspected was the difficulty of extending types of data models, of which value types (read Structs) are becoming increasingly popular. This, however, changed in <strong>Swift 2.0</strong> with the ability to write extensions on protocols.</p>

<p>So here's my take on the problem - <a href="https://github.com/matthewcheok/JSONCodable"><code>JSONCodable</code></a>.</p>

<p>It's by no means perfect - but I promise it will keep evolving as we discover better means of working with Swift.</p>

<p>Here's a quick run through of its features:</p>

<ul>
<li>It provides capabilities for <strong>encoding</strong> and <strong>decoding</strong> JSON</li>
<li>It leverages features to expose an API as Swift-like as possible</li>
<li>No operators were harmed in the making of this project</li>
</ul>

<hr>

<p>Still here? Let's take a look at how it works (you can also find this information on <a href="https://github.com/matthewcheok/JSONCodable">Github</a> but I've tried to provide reasons for some of the implementation details here.)</p>

<p><code>JSONCodable</code> is made of two seperate protocols <code>JSONEncodable</code> and <code>JSONDecodable</code>.</p>

<p><code>JSONEncodable</code> generates <code>Dictionarys</code> (compatible with <code>NSJSONSerialization</code>) and <code>Strings</code> from your types while <code>JSONDecodable</code> creates structs (or classes) from compatible <code>Dictionarys</code> (from an incoming network request for instance.)</p>

<p>Suppose we have the following data models:</p>

<pre><code>struct User {  
    var id: Int = 0
    var name: String = ""
    var email: String?
    var company: Company?
    var friends: [User] = []
}

struct Company {  
    var name: String = ""
    var address: String?
}
</code></pre>

<hr>

<h3 id="encodingserialization">Encoding/Serialization</h3>

<p>To encode JSON, we'll add conformance to <code>JSONEncodable</code>. You may also add conformance to <code>JSONCodable</code>.</p>

<pre><code>extension User: JSONEncodable {  
    func JSONEncode() throws -&gt; AnyObject {
        var result: [String: AnyObject] = [:]
        try result.archive(id, key: "id")
        try result.archive(name, key: "full_name")
        try result.archive(email, key: "email")
        try result.archive(company, key: "company")
        try result.archive(friends, key: "friends")
        return result
    }
}

extension Company: JSONEncodable {}  
</code></pre>

<p>The default implementation of <code>func JSONEncode()</code> inspects the properties of your type using reflection (see <code>Company</code>.) If you need a different mapping, you can provide your own implementation (see <code>User</code>.)</p>

<blockquote>
  <p>Using protocol extensions we can provide a default implementation that seems reasonable! That's great.</p>
</blockquote>

<hr>

<h3 id="decodingdeserialization">Decoding/Deserialization</h3>

<p>To decode JSON, we'll add conformance to <code>JSONDecodable</code>. You may also add conformance to <code>JSONCodable</code>.</p>

<pre><code>extension User: JSONDecodable {  
    mutating func JSONDecode(JSONDictionary: [String : AnyObject]) {
        JSONDictionary.restore(&amp;id, key: "id")
        JSONDictionary.restore(&amp;name, key: "full_name")
        JSONDictionary.restore(&amp;email, key: "email")
        JSONDictionary.restore(&amp;company, key: "company")
        JSONDictionary.restore(&amp;friends, key: "friends")
    }
}

extension Company: JSONDecodable {  
    mutating func JSONDecode(JSONDictionary: [String : AnyObject]) {
        JSONDictionary.restore(&amp;name, key: "name")
        JSONDictionary.restore(&amp;address, key: "address")
    }
}
</code></pre>

<p>Unlike in <code>JSONEncodable</code>, you <strong>must</strong> provide the implementations for <code>func JSONDecode()</code>. As before, you can use this to configure the mapping between keys in the <code>Dictionary</code> to properties in your <code>Structs</code> and <code>Classes</code>.</p>

<blockquote>
  <p>We can't provide a default implementation of this because there's no way in Swift to dynamically set property values at runtime. The best we can do is use <code>inout</code> parameters (some have tried to obfuscate this using operators - Sigh)</p>
</blockquote>

<p><strong>Limitations</strong></p>

<ul>
<li><p>Your types must be initializable without any parameters, i.e. implement <code>init()</code>. You can do this by either providing a default value for all your properties or implement <code>init()</code> directly and configuring your properties at initialization.</p></li>
<li><p>You must use <code>var</code> instead of <code>let</code> when declaring properties.</p></li>
</ul>

<blockquote>
  <p><code>JSONDecodable</code> needs to be able to create new instances of your types and set their values thereafter. So we need to a standard way to initialize these types via <code>init()</code> and the properties must be mutable to allow setting their values.</p>
</blockquote>

<hr>

<h3 id="whatsnext">What's next?</h3>

<ul>
<li><p>Ideally we'd allow for properties to be constant - which really might make sense for some situations. However, we then need some way of performing a complete initialization of the type (which introduces some ugliness in the API because consumers need to provide a non-trivial initializer.)</p></li>
<li><p>More symmetry in the API regarding errors when encoding and decoding JSON.</p></li>
</ul>

<p>If you have any thoughts, let me know here or on twitter <code>@matthewcheok</code>!</p>]]></content:encoded></item><item><title><![CDATA[Using proportional fonts]]></title><description><![CDATA[<p>In case you've wanted to create interfaces that mimic the precision of the clock on the iOS lock screen. Here's snippet that allows you to activate font features that enable a cleaner look:</p>

<pre><code>extension UIFont {  
    var proportionalFont: UIFont {
        let settings = [
            [
                UIFontFeatureTypeIdentifierKey: kNumberSpacingType,
                UIFontFeatureSelectorIdentifierKey: kProportionalNumbersSelector
            ],
            [
                UIFontFeatureTypeIdentifierKey: kCharacterAlternativesType,
                UIFontFeatureSelectorIdentifierKey: 1
            ]
        ]
        let descriptor</code></pre>]]></description><link>http://blog.matthewcheok.com/using-proportional-fonts/</link><guid isPermaLink="false">6aa27516-41e4-46a1-9c9a-61fd0c6a99fa</guid><category><![CDATA[swift]]></category><category><![CDATA[fonts]]></category><category><![CDATA[UI]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Tue, 14 Apr 2015 14:18:01 GMT</pubDate><content:encoded><![CDATA[<p>In case you've wanted to create interfaces that mimic the precision of the clock on the iOS lock screen. Here's snippet that allows you to activate font features that enable a cleaner look:</p>

<pre><code>extension UIFont {  
    var proportionalFont: UIFont {
        let settings = [
            [
                UIFontFeatureTypeIdentifierKey: kNumberSpacingType,
                UIFontFeatureSelectorIdentifierKey: kProportionalNumbersSelector
            ],
            [
                UIFontFeatureTypeIdentifierKey: kCharacterAlternativesType,
                UIFontFeatureSelectorIdentifierKey: 1
            ]
        ]
        let descriptor = self.fontDescriptor().fontDescriptorByAddingAttributes([
            UIFontDescriptorFeatureSettingsAttribute: settings
            ])
        return UIFont(descriptor: descriptor, size: self.pointSize)
    }
}
</code></pre>

<p>The above category generates a font (where available) that has proportionally spaced digits and uses alternative glyphs for certain punctuation symbols where appropriate.</p>]]></content:encoded></item><item><title><![CDATA[Parsing query parameters]]></title><description><![CDATA[<p>While working my new app <a href="http://www.rebel.tools">Jibber</a>, over the last couple of weeks, I needed to quickly parse a url and obtain the query parameters.</p>

<p>We could do this by breaking up the <code>String</code> representation of the url and creating a <code>Dictionary</code>. I've wrapped that up in a convenient extension to</p>]]></description><link>http://blog.matthewcheok.com/parsing-query-parameters/</link><guid isPermaLink="false">24623fcf-e505-4317-abe2-bea5633f43a1</guid><category><![CDATA[swift]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Wed, 04 Mar 2015 05:33:31 GMT</pubDate><content:encoded><![CDATA[<p>While working my new app <a href="http://www.rebel.tools">Jibber</a>, over the last couple of weeks, I needed to quickly parse a url and obtain the query parameters.</p>

<p>We could do this by breaking up the <code>String</code> representation of the url and creating a <code>Dictionary</code>. I've wrapped that up in a convenient extension to <code>NSURL</code>:</p>

<pre><code>extension NSURL {  
    var queryDictionary: [String:String]? {
        if let query = self.query {
            var dict = [String:String]()
            for parameter in query.componentsSeparatedByString("&amp;") {
                let components = parameter.componentsSeparatedByString("=")
                if components.count == 2 {
                    let key = components[0].stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
                    let value = components[1].stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
                    dict[key] = value
                }
            }
            return dict
        }
        else {
            return nil
        }
    }
}
</code></pre>

<p>I built <a href="http://www.rebel.tools">Jibber</a> because debugging JSON requests in Xcode was infuriatingly painful. If you work with RESTful API, do check it out!</p>]]></content:encoded></item><item><title><![CDATA[Design Teardown: Spinning Indicator]]></title><description><![CDATA[<p>Having worked on a Material Design themed project lately, I wanted to create the loading spinner seen now in many Google apps. Here's how I experimented to create the following effect.</p>

<p class="shadowed" align="center">  
<img src="http://blog.matthewcheok.com/content/images/2015/Jan/cropped-animation-3.gif" alt="Final Animation">
</p>

<p>Starting with a new project, add a <code>UIView</code> to the storyboard and added subclass called <code>SpinningView</code>. Next, we need</p>]]></description><link>http://blog.matthewcheok.com/design-teardown-spinning-indicator/</link><guid isPermaLink="false">89d5467f-a15b-45ff-9e55-c82730a803e2</guid><category><![CDATA[swift]]></category><category><![CDATA[UI]]></category><category><![CDATA[design-teardowns]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Fri, 16 Jan 2015 02:51:44 GMT</pubDate><content:encoded><![CDATA[<p>Having worked on a Material Design themed project lately, I wanted to create the loading spinner seen now in many Google apps. Here's how I experimented to create the following effect.</p>

<p class="shadowed" align="center">  
<img src="http://blog.matthewcheok.com/content/images/2015/Jan/cropped-animation-3.gif" alt="Final Animation">
</p>

<p>Starting with a new project, add a <code>UIView</code> to the storyboard and added subclass called <code>SpinningView</code>. Next, we need to show a circle. I'll opt to use a <code>CAShapeLayer</code> as that gives us more flexibility with animation.</p>

<pre><code>class SpinningView: UIView {

    let circleLayer = CAShapeLayer()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
}
</code></pre>

<p>Most of the above code is boilerplate. We create a constant property, <code>circleLayer</code> and initialize it right away so we can forget about working with Optionals. Next we need to find a hook to add it to the view/layer hierarchy. For views instantiated programmatically, this is clearly <code>init(frame:)</code>. But if we happen to add this in the storyboard and set the <strong>Custom Class</strong> in Interface Builder, this won't work. So another option is to use <code>awakeFromNib()</code>. </p>

<pre><code>func setup() {  
    circleLayer.lineWidth = 4
    circleLayer.fillColor = nil
    circleLayer.strokeColor = UIColor(red: 0.8078, green: 0.2549, blue: 0.2392, alpha: 1.0).CGColor
    layer.addSublayer(circleLayer)
}
</code></pre>

<p>I've refactored the setup into a method called (you guessed it) <code>setup()</code> and added the call to it in both methods.</p>

<pre><code>override func layoutSubviews() {  
    super.layoutSubviews()

    let center = CGPoint(x: bounds.midX, y: bounds.midY)
    let radius = min(bounds.width, bounds.height) / 2 - circleLayer.lineWidth/2

    let startAngle = CGFloat(-M_PI_2)
    let endAngle = startAngle + CGFloat(M_PI * 2)
    let path = UIBezierPath(arcCenter: CGPointZero, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)

    circleLayer.position = center
    circleLayer.path = path.CGPath
}
</code></pre>

<p>Next, we configure some layer properties to show the circle. Because we're drawing the circle using a path, we can simply set <code>lineWidth</code> to however thick we want the line to be. We also clear out the <code>fillColor</code> and set the <code>strokeColor</code>. Note the two properties take a <code>CGColor</code> not <code>UIColor</code>.</p>

<p>All we need to do now is configure the path object itself. We want to the circle to be as big as the view so it will draw itself at the right size when the view is resized. We do this in <code>layoutSubviews()</code>. We calculate the <code>center</code> position halfway between the width and height of the bounds. Because we want the circle to fit within the view, the radius can't be bigger than the view's width or height. We take the smaller of them and halve it. Finally we need to subtract half of the <code>lineWidth</code> because the stroke draws outward from the center of the path.</p>

<p>To make things simple later on, let's have the path draw from the top-most position and run clockwise. If you remember enough geometry, angles are measured from the positive x-axis and go clockwise. So we start at <code>-M_PI_2</code> or -90 degrees.</p>

<p>Build and run and you should see a red circle:</p>

<p class="shadowed" align="center">  
<img src="http://blog.matthewcheok.com/content/images/2015/Jan/cropped-circle.png" alt="Circle">
</p>

<p>Let's take advantage of some of the new features and make debugging our custom view a bit more manageable. Let's have the view display in  Interface Builder:</p>

<pre><code>@IBDesignable
class SpinningView: UIView {  
    ...

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
    }
}
</code></pre>

<p>By providing the <code>@IBDesignable</code> attribute and the <code>prepareForInterfaceBuilder()</code> method, we tell Xcode to draw our view's custom appearance in Interface Builder. You can test this out by going to the storyboard.</p>

<p>Next we can adopt the <code>tintColor</code> attribute by implementing the following:</p>

<pre><code>func setup() {  
    ...
    tintColorDidChange()
}

override func tintColorDidChange() {  
    super.tintColorDidChange()
    circleLayer.strokeColor = tintColor.CGColor
}
</code></pre>

<p>Go into the storyboard and see the circle change color. Change the <code>Tint</code> in the Attributes Inspector and notice how the color changes.</p>

<p>We'll expose one more property to control the <code>lineWidth</code>:</p>

<pre><code>@IBInspectable var lineWidth: CGFloat = 4 {
    didSet {
        circleLayer.lineWidth = lineWidth  
        setNeedsLayout()        
    }
}

func setup() {  
    circleLayer.lineWidth = lineWidth
    ...
}
</code></pre>

<p>Have a go at changing the property in the Attributes Inspector. The <code>@IBDesignable</code> and <code>@IBInspectable</code> attributes make it really easy to debug custom views!</p>

<p>Alright, let's get into the meat of the experiment and have a try at animating the circle. The animation can be broken down into a few parts.</p>

<ul>
<li>The stroke is animated from the beginning of the path to the end of the path. This animation can be broken up further.
<ul><li>The end position of the stroke is animated from the beginning of the path to the end of the path.</li>
<li>The start position of the stroke is animated from the beginning of the path to the end of the path.</li></ul></li>
<li>The circle itself is put into rotation.</li>
</ul>

<p>Let's start with the <code>strokeEnd</code> animation:</p>

<pre><code>let strokeEndAnimation: CAAnimation = {  
    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = 0
    animation.toValue = 1
    animation.duration = 2
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    animation.repeatCount = MAXFLOAT
    return animation
    }()
</code></pre>

<p>The <code>strokeEnd</code> and <code>strokeStart</code> properties take a value from <code>0</code> (beginning of the path) to <code>1</code> (end of the path). We're also using a timing function of ease-in-ease-out to make the animation a bit more natural-looking. Finally, the <code>repeatCount</code> is the number of times the animation will repeat. We'll just set this to a large number.</p>

<p>Next we'll add a property to control this growing animation:</p>

<pre><code>@IBInspectable var animating: Bool = true {
    didSet {
        updateAnimation()
    }
}

func setup() {  
    ...

    tintColorDidChange()
    updateAnimation()
}

func updateAnimation() {  
    if animating {
        circleLayer.addAnimation(strokeEndAnimation, forKey: "strokeEnd")
    }
    else {
        circleLayer.removeAnimationForKey("strokeEnd")
    }
}
</code></pre>

<p>Build and run and you should see the following:</p>

<p class="shadowed" align="center">  
<img src="http://blog.matthewcheok.com/content/images/2015/Jan/cropped-animation-1.gif" alt="Animation with strokeEnd">
</p>

<p>Next, we'll add animation for <code>strokeStart</code> so the stroke shrinks at the other end as it grows clockwise. However, we need to sort of delay the shrinking so it won't shrink at the same rate it grows. We need to create an animation group to add this delay.</p>

<pre><code>let strokeEndAnimation: CAAnimation = {  
    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = 0
    animation.toValue = 1
    animation.duration = 2
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

    let group = CAAnimationGroup()
    group.duration = 2.5
    group.repeatCount = MAXFLOAT
    group.animations = [animation]

    return group
    }()

let strokeStartAnimation: CAAnimation = {  
    let animation = CABasicAnimation(keyPath: "strokeStart")
    animation.beginTime = 0.5
    animation.fromValue = 0
    animation.toValue = 1
    animation.duration = 2
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

    let group = CAAnimationGroup()
    group.duration = 2.5
    group.repeatCount = MAXFLOAT
    group.animations = [animation]

    return group
    }()

func updateAnimation() {  
    if animating {
        circleLayer.addAnimation(strokeEndAnimation, forKey: "strokeEnd")
        circleLayer.addAnimation(strokeStartAnimation, forKey: "strokeStart")
    }
    else {
        circleLayer.removeAnimationForKey("strokeEnd")
        circleLayer.removeAnimationForKey("strokeStart")
    }
}    
</code></pre>

<p>Build and run and you should see the following:</p>

<p class="shadowed" align="center">  
<img src="http://blog.matthewcheok.com/content/images/2015/Jan/cropped-animation-2.gif" alt="Animation with strokeStart and strokeEnd">
</p>

<p>Finally, we'll add the rotation for a last bit of polish.</p>

<pre><code>let rotationAnimation: CAAnimation = {  
      let animation = CABasicAnimation(keyPath: "transform.rotation.z")
      animation.fromValue = 0
      animation.toValue = M_PI * 2
      animation.duration = 4
      animation.repeatCount = MAXFLOAT
      return animation
      }()

func updateAnimation() {  
    if animating {
        circleLayer.addAnimation(strokeEndAnimation, forKey: "strokeEnd")
        circleLayer.addAnimation(strokeStartAnimation, forKey: "strokeStart")
        circleLayer.addAnimation(rotationAnimation, forKey: "rotation")
    }
    else {
        circleLayer.removeAnimationForKey("strokeEnd")
        circleLayer.removeAnimationForKey("strokeStart")
        circleLayer.removeAnimationForKey("rotation")
    }
}
</code></pre>

<p class="shadowed" align="center">  
<img src="http://blog.matthewcheok.com/content/images/2015/Jan/cropped-animation-3.gif" alt="Final Animation">
</p>

<p>And there we have it - a Material Design-styled loading spinner!</p>]]></content:encoded></item><item><title><![CDATA[Creating animation sequences]]></title><description><![CDATA[<p>Thanks to <a href="https://github.com/borndangerous">@borndangerous</a> and <a href="https://github.com/bradjasper">@bradjasper</a> for their suggestions.</p>

<hr>

<p>Creating natural looking animations has always been hard. But with <a href="https://github.com/facebook/pop">Pop</a> and its extension <a href="https://github.com/matthewcheok/POP-MCAnimate">MCAnimate</a>, it doesn't have to be. (Disclaimer: I maintain the MCAnimate category.)</p>

<p>Today, we'll go ahead to recreate an animation by Andy Barnard from his post <a href="https://medium.com/ios-development-1/animation-timing-on-ios-910e6a58098b">Animation Timing</a></p>]]></description><link>http://blog.matthewcheok.com/creating-animation-sequences/</link><guid isPermaLink="false">392dfaa5-ff1d-4a97-9c53-b985fa84a4b3</guid><category><![CDATA[UI]]></category><category><![CDATA[objc]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Wed, 12 Nov 2014 02:27:44 GMT</pubDate><content:encoded><![CDATA[<p>Thanks to <a href="https://github.com/borndangerous">@borndangerous</a> and <a href="https://github.com/bradjasper">@bradjasper</a> for their suggestions.</p>

<hr>

<p>Creating natural looking animations has always been hard. But with <a href="https://github.com/facebook/pop">Pop</a> and its extension <a href="https://github.com/matthewcheok/POP-MCAnimate">MCAnimate</a>, it doesn't have to be. (Disclaimer: I maintain the MCAnimate category.)</p>

<p>Today, we'll go ahead to recreate an animation by Andy Barnard from his post <a href="https://medium.com/ios-development-1/animation-timing-on-ios-910e6a58098b">Animation Timing on iOS</a> (if you're into serious animation on iOS, it's definitely worth a read.)</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Nov/pop-stagger-1.gif" alt="Preview of animation">
</p>

<p>Firstly, we'll create views for each of the red circles and then store them away in an array:</p>

<pre><code>CGPoint center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2, CGRectGetHeight(self.view.bounds) / 2);

NSMutableArray *circles = [NSMutableArray array];

for (int i = 0; i &lt; kNumberOfCircles; i++) {  
    UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kCircleSize, kCircleSize)];
    circle.backgroundColor = [UIColor colorWithRed:0.945 green:0.439 blue:0.416 alpha:1];
    circle.layer.cornerRadius = kCircleSize / 2;
    circle.center = center;
    circle.alpha = 0;
    circle.scaleXY = CGPointMake(0.5, 0.5);

    [self.view addSubview:circle];
    [circles addObject:circle];
}

self.circles = [circles copy];  
</code></pre>

<p>We're simply using the <code>cornerRadius</code> property on <code>CALayer</code> to give the impression of a circle. We then start  the circles off with a smaller scale and make them fully transparent.</p>

<p>In the first stage of the animation, we move the circles out to a certain radius, scale them up to full size, and make them visible. But each circle is slightly delayed to create a staggered animation. This is achieved using the <code>beginTime</code> property (specifying the delay in seconds) in <strong>Core Animation</strong> and <strong>Pop</strong>. In <strong>MCAnimate</strong>, this is really trivial:</p>

<pre><code>[circles sequenceWithInterval:0.1 animations:^(UIView *circle, NSInteger index){
    CGPoint position = center;
    position.x += kCircleRadius * sin(angleIncrement * index);
    position.y -= kCircleRadius * cos(angleIncrement * index);

    circle.spring.center = position;
    circle.spring.alpha = 1;
    circle.spring.scaleXY = CGPointMake(1, 1);
} completion:^(BOOL finished){
}];
</code></pre>

<p>Having specified the interval between animations, the library knows how to iterate through the array applying animations to each element. For convenience, the element and its index in the array is provided as parameters to the animation block.</p>

<p>What's left is essentially a trigonometric problem - using <code>sin()</code> and <code>cos()</code> to determine the position around a circle's center.</p>

<p>In the next stage, we move the circle elements out further still (at twice the initial radius):</p>

<pre><code>[circles sequenceWithInterval:0 animations:^(UIView *circle, NSInteger index){
    CGPoint position = center;
    position.x += 2 * kCircleRadius * sin(angleIncrement * index);
    position.y -= 2 * kCircleRadius * cos(angleIncrement * index);

    circle.spring.center = position;
} completion:^(BOOL finished){            
}];            
</code></pre>

<p>This time we are using an interval of <code>0</code> to have the elements animate all at the same time.</p>

<p>Next, we animate the elements a step anti-clockwise and then back:</p>

<pre><code>[circles sequenceWithInterval:0 animations:^(UIView *circle, NSInteger index){
    CGPoint position = center;
    position.x += 2 * kCircleRadius * sin(angleIncrement * (index-1));
    position.y -= 2 * kCircleRadius * cos(angleIncrement * (index-1));

    circle.spring.center = position;
} completion:^(BOOL finished){
    [circles sequenceWithInterval:0 animations:^(UIView *circle, NSInteger index){
        CGPoint position = center;
        position.x += 2 * kCircleRadius * sin(angleIncrement * index);
        position.y -= 2 * kCircleRadius * cos(angleIncrement * index);

        circle.spring.center = position;
    } completion:^(BOOL finished){
    }];
}];
</code></pre>

<p>Here's where the <code>index</code> parameter comes in handy. We simply subtract <code>1</code> to calculate the position of the previous element (which gives the effect of moving anti-clockwise).</p>

<p>And finally, we reverse the initial animation, moving all elements back to the center, scaling them down and making them transparent:</p>

<pre><code>[circles sequenceWithInterval:0.1 animations:^(UIView *circle, NSInteger index){
    circle.spring.center = center;
    circle.spring.alpha = 0;
    circle.spring.scaleXY = CGPointMake(0.5, 0.5);
} completion:nil];
</code></pre>

<p>I hope you enjoyed the short breakdown. Check out the <a href="https://github.com/matthewcheok/POP-MCAnimate">demo project</a> to see this in action. </p>

<style>  
p img {  
   box-shadow: 0px 5px 15px 0px rgba(0, 0, 0, 0.5);
}
</style>]]></content:encoded></item><item><title><![CDATA[Offbeat: Playlists for Every Mood]]></title><description><![CDATA[<p>I've recently released <strong>Offbeat</strong>, a music app that helps create playlists based on a given tempo. It's great for exploring your music but it's also a terrific design exercise.</p>

<p align="center">  
<a href="https://itunes.apple.com/sg/app/offbeat/id915368037?mt=8">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/screenshot.png" alt="Screenshot of Offbeat">
</a>  
</p>

<p align="center">  
Download it for free on the App Store  
</p>  

<p align="center">  
<a href="https://itunes.apple.com/sg/app/offbeat/id915368037?mt=8">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/appstore-2x.png" alt="Offbeat on the App Store">
</a>  
</p>

<hr>

<h2 id="designdecisions">Design Decisions</h2>

<h3 id="thescreenisyourplaylist">The screen is your playlist.</h3>

<p>For Offbeat, I wanted an extremely</p>]]></description><link>http://blog.matthewcheok.com/offbeat-playlists-for-every-mood/</link><guid isPermaLink="false">fe0cd79b-4ea3-4d8c-ac72-77f2f1be7442</guid><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Tue, 28 Oct 2014 07:37:02 GMT</pubDate><content:encoded><![CDATA[<p>I've recently released <strong>Offbeat</strong>, a music app that helps create playlists based on a given tempo. It's great for exploring your music but it's also a terrific design exercise.</p>

<p align="center">  
<a href="https://itunes.apple.com/sg/app/offbeat/id915368037?mt=8">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/screenshot.png" alt="Screenshot of Offbeat">
</a>  
</p>

<p align="center">  
Download it for free on the App Store  
</p>  

<p align="center">  
<a href="https://itunes.apple.com/sg/app/offbeat/id915368037?mt=8">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/appstore-2x.png" alt="Offbeat on the App Store">
</a>  
</p>

<hr>

<h2 id="designdecisions">Design Decisions</h2>

<h3 id="thescreenisyourplaylist">The screen is your playlist.</h3>

<p>For Offbeat, I wanted an extremely simple UI where the screen is simply the playlist. Tap on an item to play.</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/playlist-play.gif" alt="Tap on an item to play">
</p>

<p>Swipe an item to left or right to remove from the playlist.</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/playlist-delete.gif" alt="Swipe item to remove">
</p>

<h3 id="afunwaytorandomizeaplaylist">A fun way to randomize a playlist.</h3>

<p>Because the concept is rather simple, the most obvious way to select a tempo might be to use a slider:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/-Slider-2.png" alt="iOS Default Slider">
</p>

<p>However, this probably might have been no fun at all and made for a poor user experience. Instead I opted to use color to give a sense of speed (magnitude of the tempo). The circular motion also might be familar to those who used the <a href="http://en.wikipedia.org/wiki/IPod_click_wheel">Click Wheel</a> on the classic iPod.</p>

<p>Here's how you can scrub through to find a playlist with the right tempo:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/interaction-scrub.gif" alt="Scrub to generate a playlist">
</p>

<p>And to take the interactions up a notch, you can also start tapping on the scrubbing control (or Tempo Dial) to physically tell the app your preferred tempo:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/interaction-tap.gif" alt="Tap to generate a playlist">
</p>

<h3 id="providehintsfornewinteractions">Provide hints for new interactions.</h3>

<p>Because the Tempo Dial is something unfamiliar to users (they are not likely to have seen it before in other apps.) I decided to provide a fun-looking hint in the form a moving glow around the control to encourage the scrubbing interaction:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/interaction-hint.gif" alt="Scrubbing control hint">
</p>

<p>I also fade between the verbs "scrub" and "tap" to show the different interactions that are possible.</p>

<hr>

<p>I hope you liked this short discussion. Let me know what you think in the comments.</p>]]></content:encoded></item><item><title><![CDATA[Design Teardown: Spinning Tips]]></title><description><![CDATA[<p><a href="https://itunes.apple.com/sg/app/tweetbot-3-for-twitter-iphone/id722294701?mt=8">Tweetbot</a> is hands down my favorite Twitter app on iOS. It features tons of interesting UX elements all around. In particular, there's an awesome effect in the on-boarding process we'll discuss in this post.</p>

<p>The following is a chapter from my upcoming book, <a href="http://www.designteardowns.com">Design Teardowns: Step-by-step iOS interface design walkthroughs</a></p>]]></description><link>http://blog.matthewcheok.com/design-teardown-spinning-tips/</link><guid isPermaLink="false">b4346235-ad40-4ce6-8ddf-bd66e0cd02a0</guid><category><![CDATA[swift]]></category><category><![CDATA[UI]]></category><category><![CDATA[design-teardowns]]></category><dc:creator><![CDATA[Matthew Cheok]]></dc:creator><pubDate>Tue, 21 Oct 2014 05:51:57 GMT</pubDate><content:encoded><![CDATA[<p><a href="https://itunes.apple.com/sg/app/tweetbot-3-for-twitter-iphone/id722294701?mt=8">Tweetbot</a> is hands down my favorite Twitter app on iOS. It features tons of interesting UX elements all around. In particular, there's an awesome effect in the on-boarding process we'll discuss in this post.</p>

<p>The following is a chapter from my upcoming book, <a href="http://www.designteardowns.com">Design Teardowns: Step-by-step iOS interface design walkthroughs</a>. If you like this or have a suggestion for another UI teardown, <a href="mailto:designteardowns@matthewcheok.com">let me know</a> or join my <a href="http://blog.matthewcheok.com/design-teardown-spinning-tips/#mc_embed_signup">mailing list</a> and get access to the <strong>sample code</strong>.</p>

<hr>

<h2 id="goal">Goal</h2>

<p>We are going to recreate the spinning tips effect from the onboarding of <a href="https://itunes.apple.com/sg/app/tweetbot-3-for-twitter-iphone/id722294701?mt=8">Tweetbot</a>. The final result looks something like this:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/interaction-final-2.gif" alt="Spinning Tips preview">
</p>

<hr>

<h2 id="observations">Observations</h2>

<h3 id="thetipbouncesinfromtheside">The tip bounces in from the side.</h3>

<p>The onboarding process begins by having the first tip bounce in from the right. The tip also seems to rotate as it comes into view.</p>

<h3 id="touchinganywhereonthescreenpullsatthetip">Touching anywhere on the screen pulls at the tip.</h3>

<p>Pulling anywhere on the screen seems to pull the tip in that direction. Also it seems like the tip moves in such a way that seems to suggest its rotating around a point which is somewhere off-screen.</p>

<h3 id="movingbetweentipsisfluid">Moving between tips is fluid.</h3>

<p>As one tip moves off-screen, the next appears from the opposite direction in what makes for a really fluid interaction.</p>

<hr>

<h2 id="prerequisites">Prerequisites</h2>

<p>This teardown requires <strong>Xcode 6</strong> and above and resulting project works on <strong>iOS 8</strong> and above. You should also know the following concepts:</p>

<h3 id="xcodebasics">Xcode Basics</h3>

<p>You should know your way around <strong>Xcode</strong> including how to create projects and classes, and access files, navigators and inspectors. You should also know how to create outlets and actions in interface builder (check out the <a href="http://blog.matthewcheok.com/design-teardown-spinning-tips/">previous teardown</a> if you don't already know this.)</p>

<h3 id="uikitbasics">UIKit Basics</h3>

<p>You should know how to create views (in code and in Interface Builder) as well as how to manipulate properties on <code>UIView</code> and its subclasses. You should also know how to use <strong>Auto Layout</strong>.</p>

<hr>

<h2 id="gameplan">Game Plan</h2>

<p>We'll adopt <code>UIKit</code>'s built-in physics system, <code>UIDynamicAnimator</code> and experiment with some of the behaviors available such as <code>UISnapBehavior</code> and <code>UIAttachmentBehavior</code>.</p>

<hr>

<h2 id="blueprint">Blueprint</h2>

<h3 id="creatingthemodel">Creating the Model</h3>

<p>Create a new Xcode project with the <strong>Single View Application</strong> template. Next we're going to create the data model that represents each tip. Create a new swift file named <code>Tip.swift</code> and add the following definition:</p>

<pre><code class="language-swift">import UIKit

struct Tip {  
    let title: String
    let summary: String
    let image: UIImage?
}
</code></pre>

<p>This simple model includes properties for the tip's title, summary and image to be displayed. We've chosen to make the <code>image</code> property optional which will make it easier to work with <strong>UIKit</strong>.</p>

<h3 id="creatingtheui">Creating the UI</h3>

<p>Next we will create the view hierarchy to display the above model properties. Create a new file, select the template from iOS, User Interface, View.</p>

<p>Add 2 labels, an image view and a page control as follows:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/layout-1.png" alt="Interface Builder layout">
</p>

<p>Note the mode for the image view has been set to <code>Aspect Fill</code> and the vertical <code>Content Compression Resistance</code> in the <strong>Size Inspector</strong> to be lower than that of the next label (this will allow the image view to shrink before the label does). </p>

<p>The number of lines in the second label is set to 0 to allow word wrap. Add the relevant <strong>Auto Layout</strong> constraints to allow the view to adapt to any arbitrary size.</p>

<p>Next we will create a backing subclass for this view. Create a new subclass of <code>UIView</code> called <code>TipView</code>.</p>

<p>Add the following outlets to the class by setting up the connections with <strong>Interface Builder</strong>:</p>

<pre><code class="language-swift">@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var summaryLabel: UILabel!

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var pageControl: UIPageControl!
</code></pre>

<p>Add the following property of our data model <code>Tip</code> and the corresponding property observer to update our subviews accordingly:</p>

<pre><code class="language-swift">var tip: Tip? {  
    didSet {
        titleLabel.text = tip?.title ?? "No Title"
        summaryLabel.text = tip?.summary ?? "No Summary"
        imageView.image = tip?.image
    }
}
</code></pre>

<blockquote>
  <p>Note the ?? operator (also known as the coalescing operator). We can use this operator to provide default values when there is no model available.</p>
</blockquote>

<p>Next we'll add rounded corners to the this view:</p>

<pre><code class="language-swift">override func layoutSubviews() {  
    super.layoutSubviews()
    layer.cornerRadius = 10
    layer.masksToBounds = true
}
</code></pre>

<h3 id="settinguptheviewcontroller">Setting up the View Controller</h3>

<p>Now we are ready to present our tips. We want a general way to do this so we can present tips from any screen in the application. In order to do this, we'll create a dedicated view controller for displaying tips and then present this view controller from wherever we require.</p>

<p>Create <code>TipViewController</code> as a subclass of <code>UIViewController</code>.</p>

<p>Next we'll add couple of constants to keep track of some layout attributes:</p>

<pre><code class="language-swift">private let kTipViewHeight: CGFloat = 400  
private let kTipViewWidth: CGFloat  = 300  
</code></pre>

<p>Now when the view controller loads, we will create a <code>TipView</code> and display it on the screen. First add this utility method that creates a tip view and configures it:</p>

<pre><code class="language-swift">func createTipView() -&gt; TipView? {  
    if let view = UINib(nibName: "TipView", bundle: nil).instantiateWithOwner(nil, options: nil).first as TipView? {
        view.frame = CGRect(x: 0, y: 0, width: kTipViewWidth, height: kTipViewHeight)
        return view
    }
    return nil
}
</code></pre>

<blockquote>
  <p><code>UINib</code> gives us programmatic access to the Interface Builder <strong>xib</strong> file we created earlier. Instantiating this file gives us an array of objects and we are interested in the first (and only) top level view.</p>
</blockquote>

<p>Next what we need to do is to simply center the view in the middle of the screen and add it to the view hierarchy:</p>

<pre><code class="language-swift">override func viewDidLoad() {  
    super.viewDidLoad()
    if let tipView = createTipView() {
        var center = CGPoint(x: CGRectGetWidth(view.bounds)/2, y: CGRectGetHeight(view.bounds)/2)
        tipView.center = center
        view.addSubview(tipView)
    }
}
</code></pre>

<p>We're almost ready to test out our work so far. Before that, we'll add a convenience method to present the tip view controller. Add the following below the entire class definition:</p>

<pre><code class="language-swift">extension UIViewController {  
    func presentTips(tips: [Tip], animated: Bool, completion: (() -&gt; Void)?) {
        let controller = TipViewController()
        controller.modalTransitionStyle = .CrossDissolve
        presentViewController(controller, animated: animated, completion: completion)
    }
}
</code></pre>

<blockquote>
  <p><strong>Extensions</strong> are a way of adding new functionality to existing types and classes in Swift. They are almost equivalent to <strong>Categories</strong> in Objective-C.</p>
</blockquote>

<p>This makes it as easy to present tips as presenting a regular modal view controller on <code>UIViewController</code>.</p>

<p>We're simply going to take an array of tips to display (but we are not yet doing anything to display them) and configure the type of model presentation we need. Using a presentation transition of <code>CrossDissolve</code> will simply fade the tips view controller into view.</p>

<p>Back in <code>ViewController.swift</code> we will display the tips view controller immediately on launch:</p>

<pre><code class="language-swift">override func viewDidAppear(animated: Bool) {  
    super.viewDidAppear(animated)
    presentTips([], animated: true, completion: nil)
}
</code></pre>

<p>Build and run the project and you should see the following:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/layout-final.png" alt="Spinning Tips preview">
</p>

<h3 id="addingtheanimations">Adding the animations</h3>

<p>To recreate the effect in <strong>Tweetbot</strong>, we will place the initial tip off-screen and rotated, and then have it snap back into the center of the screen:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/animator-diagram.png" alt="Diagram of rotation">
</p>

<p>To do this, we'll first add a couple of properties to our view controller:</p>

<pre><code class="language-swift">var tipView: TipView!  
var animator: UIDynamicAnimator!  
var attachmentBehavior: UIAttachmentBehavior!  
var snapBehavior: UISnapBehavior!  
</code></pre>

<p>These properties will hold references to the tip view and the necessary physics constraints.</p>

<p>We'll also add another constant to hold the offset parameter about which the tip view rotates around:</p>

<pre><code class="language-swift">private let kTipViewOffset: CGFloat = 500  
</code></pre>

<p>Next we will create an <strong>enumeration</strong> to hold the states of the tip view that we're interested in:</p>

<pre><code class="language-swift">enum TipViewPosition: Int {  
    case Default
    case RotatedLeft
    case RotatedRight

    func viewCenter(var center: CGPoint) -&gt; CGPoint {
        switch self {
        case .RotatedLeft:
            center.y += kTipViewOffset
            center.x -= kTipViewOffset

        case .RotatedRight:
            center.y += kTipViewOffset
            center.x += kTipViewOffset

        default:
            ()
        }

        return center
    }

    func viewTransform() -&gt; CGAffineTransform {
        switch self {
        case .RotatedLeft:
            return CGAffineTransformMakeRotation(CGFloat(-M_PI_2))

        case .RotatedRight:
            return CGAffineTransformMakeRotation(CGFloat(M_PI_2))

        default:
            return CGAffineTransformIdentity
        }
    }
}
</code></pre>

<p>The enumeration also has some convenience methods for calculating the center and transform of the tip view.</p>

<blockquote>
  <p>We're using a <code>CGAffineTransform</code> as a means of specifying the rotation. This is usually represented as a <a href="http://en.wikipedia.org/wiki/Transformation_matrix">transformation matrix</a>. However, for our purposes, we are going to treat this as an <a href="http://en.wikipedia.org/wiki/Opaque_data_type">opaque</a> value (and just use the provided functions to manipulate it.)</p>
</blockquote>

<p>Next we will add methods to place the tip view in position. The method <code>updateTipView()</code> simply sets the <code>center</code> and <code>transform</code> properties on the tip view:</p>

<pre><code class="language-swift">func updateTipView(tipView: UIView, position: TipViewPosition) {  
    var center = CGPoint(x: CGRectGetWidth(view.bounds)/2, y: CGRectGetHeight(view.bounds)/2)
    tipView.center = position.viewCenter(center)
    tipView.transform = position.viewTransform()
}
</code></pre>

<p>The next one <code>resetTipView()</code> removes all behaviors from the physics engine, updates the tip view position and updates the physics engine with new position. We then add back the necessary behaviors.</p>

<pre><code class="language-swift">func resetTipView(tipView: UIView, position: TipViewPosition) {  
    animator.removeAllBehaviors()

    updateTipView(tipView, position: position)
    animator.updateItemUsingCurrentState(tipView)

    animator.addBehavior(attachmentBehavior)
    animator.addBehavior(snapBehavior)
}
</code></pre>

<blockquote>
  <p>We use <code>updateItemUsingCurrentState</code> to reset the animator's state to the view's current state. This works best if no behaviors are attached.</p>
</blockquote>

<p>Next we will add a method to setup the physics engine:</p>

<pre><code class="language-swift">func setupAnimator() {  
    animator = UIDynamicAnimator(referenceView: view)

    var center = CGPoint(x: CGRectGetWidth(view.bounds)/2, y: CGRectGetHeight(view.bounds)/2)

    tipView = createTipView()
    view.addSubview(tipView)
    snapBehavior = UISnapBehavior(item: tipView, snapToPoint: center)

    center.y += kTipViewOffset
    attachmentBehavior = UIAttachmentBehavior(item: tipView, offsetFromCenter: UIOffset(horizontal: 0, vertical: kTipViewOffset), attachedToAnchor: center)

    resetTipView(tipView, position: .RotatedRight)
}
</code></pre>

<p>We've created the animator and assigned our view controller's root view to be the reference view (for calculation in the physics simulations.) </p>

<p>Next we've created a snap behavior that will bring the tip view back into the center of the root view. Most importantly we've created an attachment behavior (which you can imagine as sort of invisible string) that connects the tip view to an imaginary point of distance <code>kTipViewOffset</code> directly below it.</p>

<p>Now clear out the previous testing code in <code>viewDidLoad()</code> and add the following to <code>viewDidAppear()</code>:</p>

<pre><code class="language-swift">override func viewDidAppear(animated: Bool) {  
    super.viewDidAppear(animated)
    setupAnimator()
}
</code></pre>

<p>Build and run the project and you should see the following:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/animator-initial-4.gif" alt="Snap Animation">
</p>

<p>Now we notice a slight quirk. Auto Layout is resizing the contents of our tip view during the animation.</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/animator-frame.png" alt="Frame vs Bounds">
</p>

<p>As the tip view is rotating, its frame is significantly larger than its bounds (it's the smallest rectanglar region that will fit the view within it). Since Auto Layout sizes views according to their frames, it incorrectly assumes it has a larger space to lay things out.</p>

<p>To correct this we need to supply the method <code>alignmentRectForFrame()</code> in the view subclass. Add the following to <code>TipView</code>:</p>

<pre><code class="language-swift">override func alignmentRectForFrame(frame: CGRect) -&gt; CGRect {  
    return bounds
}
</code></pre>

<p>Build and run and things should look right:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/animator-autolayout.gif" alt="Animation with Auto Layout fixed">
</p>

<h3 id="addingtheinteractions">Adding the interactions</h3>

<p>To handle the dragging interaction, we'll create a pan gesture recognizer and add it to the view controller's root view. Add the following to the bottom of <code>setupAnimator</code>:</p>

<pre><code class="language-swift">let pan = UIPanGestureRecognizer(target: self, action: "panTipView:")  
view.addGestureRecognizer(pan)  
</code></pre>

<p>Add a new property to contain the attachment behavior managed by the pan gesture:</p>

<pre><code class="language-swift">var panBehavior: UIAttachmentBehavior!  
</code></pre>

<p>Next we'll add the method to handle updates when the pan gesture is triggered. We need to do a few things:</p>

<ol>
<li><p>When the pan gesture begins, we remove the snap behavior (so we can move the tip view without it snapping back to the center of the screen). We then create an attachment behavior anchored to the touch location.</p></li>
<li><p>As the pan gesture continues, we change the anchor of the previous attachment behavior to the current touch location. This will adjust the tip view position until it satisfies both attachment constraints (the initial one with the offset distance and this panning one).</p></li>
<li><p>When the pan gesture stops (it either ends or is cancelled by the system), we have to decide if we should proceed with the transition or cancel it. One way to do this is to just look at how far we have moved (the offset from the center).</p></li>
<li><p>If we cancel the transition, we remove the pan gesture's attachment behavior and add the snap behavior back to the view (which will snap it back to the center of the screen).</p></li>
<li><p>If we proceed with the transition, we first change the attachment's anchor to be off-screen in the right direction to animate the view away. Next we call <code>resetTipView</code> to bring it back on-screen from the opposite direction.</p></li>
</ol>

<pre><code class="language-swift">func panTipView(pan: UIPanGestureRecognizer) {  
    let location = pan.locationInView(view)

    switch pan.state {
    case .Began:
        animator.removeBehavior(snapBehavior)
        panBehavior = UIAttachmentBehavior(item: tipView, attachedToAnchor: location)
        animator.addBehavior(panBehavior)

    case .Changed:
        panBehavior.anchorPoint = location

    case .Ended:
        fallthrough
    case .Cancelled:
        let center = CGPoint(x: CGRectGetWidth(view.bounds)/2, y: CGRectGetHeight(view.bounds)/2)
        let offset = location.x - center.x
        if fabs(offset) &lt; 100 {
            animator.removeBehavior(panBehavior)
            animator.addBehavior(snapBehavior)
        }
        else {
            let position = offset &gt; 0 ? TipViewPosition.RotatedRight : TipViewPosition.RotatedLeft
            let nextPosition = offset &gt; 0 ? TipViewPosition.RotatedLeft : TipViewPosition.RotatedRight
            let duration = 0.4

            let center = CGPoint(x: CGRectGetWidth(view.bounds)/2, y: CGRectGetHeight(view.bounds)/2)

            panBehavior.anchorPoint = position.viewCenter(center)
            dispatch_after(dispatch_time(
                DISPATCH_TIME_NOW, Int64(duration * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
                    [self]
                    self.resetTipView(self.tipView, position: nextPosition)
            }
        }

    default:
        ()
    }
}
</code></pre>

<blockquote>
  <p>The empty parentheses <code>()</code> are the simpliest way to include a statement in <strong>Swift</strong>. We use this to provide no operation in the <code>default</code> case.</p>
</blockquote>

<p>Build and run and try to move the tip view around:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/interaction-initial.gif" alt="Panning effect">
</p>

<h3 id="makingthemodelwork">Making the Model Work</h3>

<p>Finally we're ready to use actual tips. Let's add some sample data to the our presenting view controller in <code>ViewController.swift</code>:</p>

<pre><code class="language-swift">presentTips([  
      Tip(title: "Tip #1: Don't Blink", summary: "Fantastic shot of Sarah for the ALS Ice Bucket Challenge - And yes, she tried her hardest not to blink!", image: UIImage(named: "als-ice-bucket-challenge")),
      Tip(title: "Tip #2: Explore", summary: "Get out of the house!", image: UIImage(named: "arch-architecture")),
      Tip(title: "Tip #3: Take in the Moment", summary: "Remember that each moment is unique and will never come again.", image: UIImage(named: "man-mountains"))
], animated: true, completion: nil)
</code></pre>

<p>Add the following properties to <code>TipViewController</code> to keep track of the tips and which one is being shown:</p>

<pre><code class="language-swift">var tips = [Tip]()  
var index = 0  
</code></pre>

<p>Now we've got to remember to save the tips away as our view controller is presented:</p>

<pre><code class="language-swift">func presentTips(tips: [Tip], animated: Bool, completion: (() -&gt; Void)?) {  
    let controller = TipViewController()
    controller.tips = tips

    ...
}
</code></pre>

<p>Next we need to setup the view given an instance of the <code>Tip</code> data model. We also need to adjust the page control accordingly. Add this method to the <code>TipViewController</code> to setup the tip view given an index into our list of tips:</p>

<pre><code class="language-swift">func setupTipView(tipView: TipView, index: Int) {  
    if index &lt; tips.count {
        let tip = tips[index]
        tipView.tip = tip

        tipView.pageControl.numberOfPages = tips.count
        tipView.pageControl.currentPage = index
    }
    else {
        tipView.tip = nil
    }
}
</code></pre>

<p>Let's load the very first tip when we setup the animator:</p>

<pre><code class="language-swift">func setupAnimator() {  
    ...

    setupTipView(tipView, index: 0)
    resetTipView(tipView, position: .RotatedRight)

    ...
}
</code></pre>

<p>Finally we need to handle moving between tips as we interact with the tip view. Add the following the <code>.Changed</code> state of the pan gesture method:</p>

<pre><code class="language-swift">...
else {  
    var nextIndex = self.index
    var position = TipViewPosition.RotatedRight
    var nextPosition = TipViewPosition.RotatedLeft

    if offset &gt; 0 {
        nextIndex -= 1
        nextPosition = .RotatedLeft
        position = .RotatedRight
    }
    else {
        nextIndex += 1
        nextPosition = .RotatedRight
        position = .RotatedLeft
    }

    if nextIndex &lt; 0 {
        nextIndex = 0
        nextPosition = .RotatedRight
    }

    let duration = 0.4
    let center = CGPoint(x: CGRectGetWidth(view.bounds)/2, y: CGRectGetHeight(view.bounds)/2)

    panBehavior.anchorPoint = position.viewCenter(center)

    dispatch_after(dispatch_time(
        DISPATCH_TIME_NOW, Int64(duration * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
            [self]
            if nextIndex &gt;= self.tips.count {
                self.dismissViewControllerAnimated(true, completion: nil)
            }
            else {
                self.index = nextIndex
                self.setupTipView(self.tipView, index: nextIndex)
                self.resetTipView(self.tipView, position: nextPosition)
            }
    }
}
</code></pre>

<blockquote>
  <p>If we try to swipe to the previous tip when we are already at the first tip, we simply want to bounce back. If we try to swipe after the last one, we'll dismiss the tip view controller.</p>
</blockquote>

<p>We can improve the visual effect by making the presenting view controller slightly visible. Make the tips view controller's background color semi-transparent:</p>

<pre><code class="language-swift">override func viewDidLoad() {  
    super.viewDidLoad()
    view.backgroundColor = UIColor(white: 0, alpha: 0.5)
}
</code></pre>

<p>Add the following bit to <code>presentTips()</code> in our <code>UIViewController</code> extension:</p>

<pre><code class="language-swift">func presentTips(tips: [Tip], animated: Bool, completion: (() -&gt; Void)?) {  
    ...

    controller.modalPresentationStyle = .OverFullScreen
    controller.modalTransitionStyle = .CrossDissolve
    presentViewController(controller, animated: animated, completion: completion)
}
</code></pre>

<blockquote>
  <p>By specifying <code>OverFullScreen</code> as the <code>modalPresentationStyle</code>, we are telling the system to keep the presenting view controller in the view hierarchy. Without this, you'd just see a black background.</p>
</blockquote>

<p>Now build and run and you should see the following:</p>

<p align="center">  
<img src="http://blog.matthewcheok.com/content/images/2014/Oct/interaction-final-1.gif" alt="Final preview">
</p>

<hr>

<h2 id="afterthoughts">Afterthoughts</h2>

<p>That's it, we've created the Spinning Tips effect. You've learnt how to:</p>

<ul>
<li>Setup and configure <code>UIDynamicAnimator</code></li>
<li>Use attachments and snap behaviors</li>
<li>Create a view controller that can be used generally</li>
</ul>

<h2 id="nextsteps">Next Steps</h2>

<p>To add more polish you can try the following:</p>

<ul>
<li>Experiment with <code>resistance</code> property of <code>UIDynamicItemBehavior</code> to adjust the speed of the animation according to the <code>velocity</code> of the pan gesture.</li>
</ul>

<hr>

<p>If you liked this teardown, want to receive updates on the book, or want to play with the <strong>sample code</strong> subscribe to my mailing list (your email will not be shared):</p>

<!-- Begin MailChimp Signup Form -->  

<p><link href="http://blog.matthewcheok.com//cdn-images.mailchimp.com/embedcode/classic-081711.css" rel="stylesheet" type="text/css">  </p>

<style type="text/css">  
    #mc_embed_signup{background:#f7f7f7; clear:left; font:14px Helvetica,Arial,sans-serif; -moz-border-radius: 10px; border-radius: 10px; }
    /* Add your own MailChimp form style overrides in your site stylesheet or in this style block.
       We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
</style>  

<div id="mc_embed_signup">  
<form action="//designteardowns.us9.list-manage.com/subscribe/post?u=2c950b0527d3787f905b841c7&amp;id=9c98bdb62c" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate="">  
<div id="mc_embed_signup_scroll">  
<h2>Subscribe to our mailing list</h2>  
<div class="indicates-required"><span class="asterisk">*</span> indicates required</div>  
<div class="mc-field-group">  
<label for="mce-EMAIL">Email Address  <span class="asterisk">*</span>  
</label>  
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">  
</div>  

<div class="mc-field-group">  
<label for="mce-NAME">Full Name </label>  
<input type="text" value="" name="NAME" class="" id="mce-NAME">  
</div>  

<div id="mce-responses" class="clear">  
<div class="response" id="mce-error-response" style="display:none"></div>  
<div class="response" id="mce-success-response" style="display:none"></div>  
</div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->  
<div style="position: absolute; left: -5000px;"><input type="text" name="b_2c950b0527d3787f905b841c7_9c98bdb62c" tabindex="-1" value=""></div>  
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>  
</div>  

<p></p></form> <br>
</div>  <p></p>

<script type="text/javascript" src="http://blog.matthewcheok.com//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js"></script><script type="text/javascript">(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='NAME';ftypes[1]='text';}(jQuery));var $mcj = jQuery.noConflict(true);</script>  

<!--End mc_embed_signup-->]]></content:encoded></item></channel></rss>