<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>[iPhoneiOS dev:tips]; &#187; Event Handling</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/event-handling/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:59:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Encapsulating Begin Ignoring Events and End Ignoring Events</title>
		<link>http://iPhoneDeveloperTips.com/event-handling/encapsulating-begin-ignoring-events-and-end-ignoring-events.html</link>
		<comments>http://iPhoneDeveloperTips.com/event-handling/encapsulating-begin-ignoring-events-and-end-ignoring-events.html#comments</comments>
		<pubDate>Mon, 19 Oct 2009 07:53:52 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Event Handling]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4301</guid>
		<description><![CDATA[There are times when it&#8217;s really handy to stop event processing momentarily so you can perform an update to the UI (e.g. sliding a custom view into place using a timed animation) or you otherwise need to save the user from themselves by momentarily stopping touch events. I&#8217;ve written previously on How to Suspend / [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when it&#8217;s really handy to stop event processing momentarily so you can perform an update to the UI (e.g. sliding a custom view into place using a timed animation) or you otherwise need to save the user from themselves by momentarily stopping touch events.</p>
<p>I&#8217;ve written previously on <a  target="_blank"  href="http://iphonedevelopertips.com/event-handling/suspend-touch-events.html">How to Suspend / Ignore Touch Events</a>, which is as simple as:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Begin ignoring events</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplicationsharedApplication<span style="color: #002200;">&#93;</span> beginIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Stop ignoring events</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplicationsharedApplication<span style="color: #002200;">&#93;</span> endIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><span id="more-4301"></span><br />
The problem with the above is that depending on when and where you make these calls, you can end up with the lines sprinkled about inside your code, which can make it troublesome to match one call with the other.</p>
<p>For example, if I make a call to begin ignoring events, and then move to a new view in which I then make a request to stop ignoring events, I now have the begin ignorning call in a different class from the end ignorning call. A big deal? Definitely not, however, when you see one call without the other, it can be both hard to know if you have a matching set, not to mention the questions that may arise when you revisit your code a few months down the road.</p>
<p>I get around this by writing a very simple set of methods for stopping and stopping the ignore events process. Here&#8217;s how this looks:</p>
<h5>Interface Definition for SomeClass</h5>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> SomeClass <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> 
<span style="color: #002200;">&#123;</span>
  ...
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>enableEvents;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startIgnoringEvents;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>stopIgnoringEventsAfter<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>seconds;</pre></div></div>

<p>Notice that the three methods shown are class methods. The implementation for SomeClass follows:</p>
<h5>Implementation for SomeClass</h5>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> SomeClass
&nbsp;
...
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>enableEvents
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> endIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startIgnoringEvents
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> beginIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>stopIgnoringEventsAfter<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>seconds
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>enableEvents<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> afterDelay<span style="color: #002200;">:</span>seconds<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>How to Start and Stop Ingoring Events</h5>
<p>Now that I have these helper methods, I can keep all references to start and stop ignoring of events in one place, here&#8217;s how this might look:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Stop events momentarily...</span>
<span style="color: #002200;">&#91;</span>SomeClass startIgnoringEvents<span style="color: #002200;">&#93;</span>;    
&nbsp;
<span style="color: #11740a; font-style: italic;">// This is the activity I don't want interrupted</span>
<span style="color: #002200;">&#91;</span>dropDownViewController showMsg<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Stop ignoring events</span>
<span style="color: #002200;">&#91;</span>ShoppingListViewController stopIgnoringEventsAfter<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Again, these extra steps are not required, however, keeping related code together can be a good thing.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/event-handling/encapsulating-begin-ignoring-events-and-end-ignoring-events.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How Long was a Touch Event &#8211; Detecting When a Touch Event Starts and Ends</title>
		<link>http://iPhoneDeveloperTips.com/event-handling/how-long-was-a-touch-event-detecting-when-a-touch-event-starts-and-ends.html</link>
		<comments>http://iPhoneDeveloperTips.com/event-handling/how-long-was-a-touch-event-detecting-when-a-touch-event-starts-and-ends.html#comments</comments>
		<pubDate>Wed, 14 Oct 2009 08:43:41 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Event Handling]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4201</guid>
		<description><![CDATA[Here&#8217;s a quick tip that shows how to determine the length of time from a begin touch event to an end touch event. I used a similar snippet of code recently when I had to determine if a user tapped on an image or held their finger on the image for a pre-determined amount of [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick tip that shows how to determine the length of time from a begin touch event to an end touch event. I used a similar snippet of code recently when I had to determine if a user tapped on an image or held their finger on the image for a pre-determined amount of time.</p>
<p>Start by creating a variable of type <strong>NSTimeInterval</strong> to hold the time the touch event began.<br />
<span id="more-4201"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Define this as an instance variable</span>
NSTimeInterval touchStartTime;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// When the touch event was detected</span>
  touchStartTime <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>event timestamp<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Inside the <strong>touchesBegan</strong> event, with some simple math, one can determine how long from the start event to the end.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesEnded<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Calculate how long touch lasted</span>
  NSTimeInterval touchTimeDuration <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>event timestamp<span style="color: #002200;">&#93;</span> <span style="color: #002200;">-</span> touchStartTime;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Touch duration: %3.2f seconds&quot;</span>, touchTimeDuration<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/event-handling/how-long-was-a-touch-event-detecting-when-a-touch-event-starts-and-ends.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Suspend or Ignore Touch Events</title>
		<link>http://iPhoneDeveloperTips.com/event-handling/suspend-touch-events.html</link>
		<comments>http://iPhoneDeveloperTips.com/event-handling/suspend-touch-events.html#comments</comments>
		<pubDate>Tue, 19 May 2009 12:34:34 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Event Handling]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2279</guid>
		<description><![CDATA[Temporarily stop/suspend touch events on the iPhone.]]></description>
			<content:encoded><![CDATA[<p>There are times when you may need to temporarily stop receiving touch events. For example, when doing an animation or a screen transition rather than disable user enabled views (buttons, images, etc), you can tell the application to ignore user interaction events as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> beginIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Turning event interaction back on is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> endIgnoringInteractionEvents<span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/event-handling/suspend-touch-events.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Where Was Touch &#8211; Determine if Touch/Point is Within Rectangle</title>
		<link>http://iPhoneDeveloperTips.com/event-handling/determining-if-touch-or-point-is-within-rectangle.html</link>
		<comments>http://iPhoneDeveloperTips.com/event-handling/determining-if-touch-or-point-is-within-rectangle.html#comments</comments>
		<pubDate>Sat, 25 Apr 2009 23:11:23 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Event Handling]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2149</guid>
		<description><![CDATA[I recently had to write a short chunk of code to determine when a user touches a specific rectangular region on the screen. Detecting this is nothing more than getting a reference to a CGPoint (x and y coordinates) of the location touched and checking to see if the point is within the rectangular bounds [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to write a short chunk of code to determine when a user touches a specific rectangular region on the screen. Detecting this is nothing more than getting a reference to a CGPoint (x and y coordinates) of the location touched and checking to see if the point is within the rectangular bounds of interest.</p>
<p>The following example shows one approach, the workhorse is <strong>CGRectContainsPoint</strong>. </p>
<p><span id="more-2149"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Touches began
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Detect touch anywhere</span>
  UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Get the specific point that was touched</span>
  CGPoint point <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span>self.view<span style="color: #002200;">&#93;</span>; 
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pointx: %f pointy:%f&quot;</span>, point.x, point.y<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// See if the point touched is within these rectangular bounds</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span>, <span style="color: #2400d9;">5</span>, <span style="color: #2400d9;">40</span>, <span style="color: #2400d9;">130</span><span style="color: #002200;">&#41;</span>, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">do</span> something...
  <span style="color: #002200;">&#125;</span> 
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>I use <strong>CGRectMake</strong> to define the bounds of the rectangle and pass in the variable <strong>point</strong> that was obtained from the touch location. </p>
<p>As with <strong>CGRectContainsPoint</strong>, <strong>CGRectMake</strong> is a C function, which is defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGRect CGRectMake<span style="color: #002200;">&#40;</span>CGFloat x, CGFloat y, CGFloat width, GFloat height<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The C structures you&#8217;ll need to put all the pieces together, are shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> CGRect 
<span style="color: #002200;">&#123;</span>
   CGPoint origin;
   CGSize size;
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #a61390;">struct</span> CGPoint 
<span style="color: #002200;">&#123;</span>
   CGFloat x;
   CGFloat y;
<span style="color: #002200;">&#125;</span>;
&nbsp;
<span style="color: #a61390;">struct</span> CGSize 
<span style="color: #002200;">&#123;</span>
   CGFloat width;
   CGFloat height;
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<p><strong>CGFloat</strong> is nothing more than a floating point number that is defined as a typedef, based on the system architecture. In other words, it&#8217;s just a float. </p>
<p>For more information about CGRect, CGSize and CGPoint, you can <a href="http://iphonedevelopertips.com/c/cgrect-cgsize-and-cgpoint-functions.html">read this post</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/event-handling/determining-if-touch-or-point-is-within-rectangle.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
