{"title":"Save Time and Code With JMWhenTapped","html_url":"https://deallocatedobjects.com/posts/jmwhentapped","source_url":"","markdown_content":"I've just released a new little helper library for iOS development. It's called `JMWhenTapped`. It's is a simple little syntactical-sugar addition to all `UIView` objects, as well as any class that inherits from `UIView`. It allows you to assign touch-up, touch-down, and tapped (touched down then up) actions to a `UIView` object using a convenient blocks-style syntax.\r\n\r\n### Installation\r\n\r\nClone [the repo](http://github.com/jakemarsh/JMWhenTapped). Add the `JMWhenTapped` folder to your iOS 4 project. `#import \"JMWhenTapped.h\"` wherever you'd like to use the syntax.\r\n\r\n### Examples & Usage\r\n\r\nUse it like this:\r\n\r\n``` objc\t\r\n[myView whenTapped:^{\r\n\tNSLog(@\"I was tapped!\");\r\n}];\r\n```\r\n\t\r\nOr like this:\r\n\r\n``` objc\r\n[myView whenTouchedDown:^{\r\n\tNSLog(@\"I was touched down!\");\r\n}];\r\n```\r\n\t\r\nAnd also like this:\r\n\r\n``` objc\r\n[myView whenTouchedUp:^{\r\n\tNSLog(@\"I was touched up!\");\t\t\r\n}];\r\n```\r\n\r\n### The Different Actions\r\n\r\nThe `whenTapped:` method should be used in cases where you simply want something to happen when the user taps on a view (i.e. you are concerned with performing some action when their finger is down then up, like changing to a \"pressed\" state.)\r\n\r\nThe `whenTouchedDown:` method should be used when you want to trigger some action when the user touches down on your view.\r\n\r\nThe `whenTouchedUp:` method should be used when you want to trigger some action when the user touches up on your view.\r\n\r\n### Demo\r\n\r\nIncluded in [the repo](http://github.com/jakemarsh/JMWhenTapped) is a demo Xcode project that illustrates a quick example of how to use `JMWhenTapped`.","html_content":"<p>I’ve just released a new little helper library for iOS development. It’s called <code>JMWhenTapped</code>. It’s is a simple little syntactical-sugar addition to all <code>UIView</code> objects, as well as any class that inherits from <code>UIView</code>. It allows you to assign touch-up, touch-down, and tapped (touched down then up) actions to a <code>UIView</code> object using a convenient blocks-style syntax.</p>\n\n<h2 id=\"installation\" data-level=\"3\">Installation</h2>\n\n<p>Clone <a href=\"http://github.com/jakemarsh/JMWhenTapped\">the repo</a>. Add the <code>JMWhenTapped</code> folder to your iOS 4 project. <code>#import &quot;JMWhenTapped.h&quot;</code> wherever you’d like to use the syntax.</p>\n\n<h2 id=\"examples-usage\" data-level=\"3\">Examples &amp; Usage</h2>\n\n<p>Use it like this:</p>\n<figure class=\"code\" data-lang=\"objective-c\"><figcaption><span class=\"lang\">Objective-C</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">[myView </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">whenTapped:</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">^</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">{</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">    NSLog</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"I was tapped!\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">);</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">}];</span></span></code></pre></figure>\n<p>Or like this:</p>\n<figure class=\"code\" data-lang=\"objective-c\"><figcaption><span class=\"lang\">Objective-C</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">[myView </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">whenTouchedDown:</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">^</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">{</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">    NSLog</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"I was touched down!\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">);</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">}];</span></span></code></pre></figure>\n<p>And also like this:</p>\n<figure class=\"code\" data-lang=\"objective-c\"><figcaption><span class=\"lang\">Objective-C</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">[myView </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">whenTouchedUp:</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">^</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">{</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">    NSLog</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">@\"I was touched up!\"</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">);        </span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">}];</span></span></code></pre></figure>\n<h2 id=\"the-different-actions\" data-level=\"3\">The Different Actions</h2>\n\n<p>The <code>whenTapped:</code> method should be used in cases where you simply want something to happen when the user taps on a view (i.e. you are concerned with performing some action when their finger is down then up, like changing to a “pressed” state.)</p>\n\n<p>The <code>whenTouchedDown:</code> method should be used when you want to trigger some action when the user touches down on your view.</p>\n\n<p>The <code>whenTouchedUp:</code> method should be used when you want to trigger some action when the user touches up on your view.</p>\n\n<h2 id=\"demo\" data-level=\"3\">Demo</h2>\n\n<p>Included in <a href=\"http://github.com/jakemarsh/JMWhenTapped\">the repo</a> is a demo Xcode project that illustrates a quick example of how to use <code>JMWhenTapped</code>.</p>","created_at":"2013-11-16T05:10:25.686Z","updated_at":"2013-11-16T05:10:25.686Z"}