<?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>iOS Developer Tips Blog &#187; Events and Touch</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/event-handling/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:40:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Gestures Recognizers &#8211; Tap, Pinch/Zoom, Rotate, Swipe, Pan, Long Press</title>
		<link>http://iPhoneDeveloperTips.com/event-handling/gestures-recognizers-tap-pinchzoom-rotate-swipe-pan-long-press.html</link>
		<comments>http://iPhoneDeveloperTips.com/event-handling/gestures-recognizers-tap-pinchzoom-rotate-swipe-pan-long-press.html#comments</comments>
		<pubDate>Wed, 06 Apr 2011 11:03:36 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Events and Touch]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=8147</guid>
		<description><![CDATA[The UIGestureRecognizer class is available to help with detecting and responding to the various UI gestures common on iOS devices. UIGestureRecognizer is an abstract class, with the following concrete subclasses, one for each type of available recognizer: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UISwipeGestureRecognizer UIPanGestureRecognizer UILongPressGestureRecognizer Depending on the type of recognizer, there are various behaviors that you [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>UIGestureRecognizer</strong> class is available to help with detecting and responding to the various UI gestures common on iOS devices. <strong>UIGestureRecognizer</strong> is an abstract class, with the following concrete subclasses, one for each type of available recognizer: </p>
<blockquote>
<ol>
<li>UITapGestureRecognizer </li>
<li>UIPinchGestureRecognizer </li>
<li>UIRotationGestureRecognizer </li>
<li>UISwipeGestureRecognizer </li>
<li>UIPanGestureRecognizer </li>
<li>UILongPressGestureRecognizer </li>
</ol>
</blockquote>
<p><span id="more-8147"></span><br />
Depending on the type of recognizer, there are various behaviors that you can configure. For instance, with the <strong>UITapGestureRecognizer</strong>, you can specify the number of taps and number of touches. As an example, you could specify that two fingers are required, tapping twice with both fingers.</p>
<p>In response to recognized gestures, an action messages is sent to a target object that you specify. Depending on the type of gesture additional information about the gesture may be available in the action method, for example, the start location of a swipe, or the scale factor of a pinch/zoom. </p>
<p>Once you&#8217;ve defined a gesture, you add the recognizer to the view. From there, the view is responsible for recognizing when a gesture has occurrred and calling the relevant action.</p>
<h5>Gesture Recognizer Examples</h5>
<p>Let&#8217;s walk through configuration of a few gestures to see how all this works.</p>
<p>Below I&#8217;ve defined a recognizer for managing taps, notice how I&#8217;ve specified that there are two taps required, with just one touch (finger). The <strong>@selector</strong> specifies the method that will be called when the gesture is recognized.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// -----------------------------</span>
<span style="color: #11740a; font-style: italic;">// One finger, two taps</span>
<span style="color: #11740a; font-style: italic;">// -----------------------------</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create gesture recognizer</span>
UITapGestureRecognizer <span style="color: #002200;">*</span>oneFingerTwoTaps <span style="color: #002200;">=</span> 
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITapGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>oneFingerTwoTaps<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Set required taps and number of touches</span>
<span style="color: #002200;">&#91;</span>oneFingerTwoTaps setNumberOfTapsRequired<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>oneFingerTwoTaps setNumberOfTouchesRequired<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Add the gesture to the view</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addGestureRecognizer<span style="color: #002200;">:</span>oneFingerTwoTaps<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Below is a recognizer for two fingers, two taps:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// -----------------------------</span>
<span style="color: #11740a; font-style: italic;">// Two fingers, two taps</span>
<span style="color: #11740a; font-style: italic;">// -----------------------------</span>
UITapGestureRecognizer <span style="color: #002200;">*</span>twoFingersTwoTaps <span style="color: #002200;">=</span> 
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITapGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>twoFingersTwoTaps<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>twoFingersTwoTaps setNumberOfTapsRequired<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>twoFingersTwoTaps setNumberOfTouchesRequired<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addGestureRecognizer<span style="color: #002200;">:</span>twoFingersTwoTaps<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The following gestures recognize swipes up and down:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// -----------------------------</span>
<span style="color: #11740a; font-style: italic;">// One finger, swipe up</span>
<span style="color: #11740a; font-style: italic;">// -----------------------------</span>
UISwipeGestureRecognizer <span style="color: #002200;">*</span>oneFingerSwipeUp <span style="color: #002200;">=</span> 
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UISwipeGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>oneFingerSwipeUp<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>oneFingerSwipeUp setDirection<span style="color: #002200;">:</span>UISwipeGestureRecognizerDirectionUp<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addGestureRecognizer<span style="color: #002200;">:</span>oneFingerSwipeUp<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// -----------------------------</span>
<span style="color: #11740a; font-style: italic;">// One finger, swipe down</span>
<span style="color: #11740a; font-style: italic;">// -----------------------------</span>
UISwipeGestureRecognizer <span style="color: #002200;">*</span>oneFingerSwipeDown <span style="color: #002200;">=</span> 
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UISwipeGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>oneFingerSwipeDown<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>oneFingerSwipeDown setDirection<span style="color: #002200;">:</span>UISwipeGestureRecognizerDirectionDown<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addGestureRecognizer<span style="color: #002200;">:</span>oneFingerSwipeDown<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Two recognize two fingers rotating on a view:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// -----------------------------</span>
<span style="color: #11740a; font-style: italic;">// Two finger rotate  </span>
<span style="color: #11740a; font-style: italic;">// -----------------------------</span>
UIRotationGestureRecognizer <span style="color: #002200;">*</span>twoFingersRotate <span style="color: #002200;">=</span> 
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIRotationGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>twoFingersRotate<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addGestureRecognizer<span style="color: #002200;">:</span>twoFingersRotate<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>And finally, two finger pinch:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// -----------------------------</span>
<span style="color: #11740a; font-style: italic;">// Two finger pinch</span>
<span style="color: #11740a; font-style: italic;">// -----------------------------</span>
UIPinchGestureRecognizer <span style="color: #002200;">*</span>twoFingerPinch <span style="color: #002200;">=</span> 
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIPinchGestureRecognizer alloc<span style="color: #002200;">&#93;</span> initWithTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>twoFingerPinch<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addGestureRecognizer<span style="color: #002200;">:</span>twoFingerPinch<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>There are two additional gestures not shown above that you can also tinker with, <strong>UIPanGestureRecognizer</strong> and <strong>UILongPressGestureRecognizer</strong>.</p>
<h5>Gesture Recognizer Action Methods</h5>
<p>For each of the above definitions, below are the associated action methods.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*--------------------------------------------------------------
* One finger, two taps 
*-------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>oneFingerTwoTaps
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Action: One finger, two taps&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*--------------------------------------------------------------
* Two fingers, two taps
*-------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>twoFingersTwoTaps <span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Action: Two fingers, two taps&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span> 
&nbsp;
<span style="color: #11740a; font-style: italic;">/*--------------------------------------------------------------
* One finger, swipe up
*-------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>oneFingerSwipeUp<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UISwipeGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>recognizer 
<span style="color: #002200;">&#123;</span> 
  CGPoint point <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>recognizer locationInView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Swipe up - start location: %f,%f&quot;</span>, point.x, point.y<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*--------------------------------------------------------------
* One finger, swipe down
*-------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>oneFingerSwipeDown<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UISwipeGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>recognizer 
<span style="color: #002200;">&#123;</span> 
  CGPoint point <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>recognizer locationInView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Swipe down - start location: %f,%f&quot;</span>, point.x, point.y<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*--------------------------------------------------------------
* Two finger rotate   
*-------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>twoFingersRotate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIRotationGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>recognizer 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Convert the radian value to show the degree of rotation</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Rotation in degrees since last change: %f&quot;</span>, <span style="color: #002200;">&#91;</span>recognizer rotation<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">180</span> <span style="color: #002200;">/</span> M_PI<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*--------------------------------------------------------------
* Two finger pinch
*-------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>twoFingerPinch<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIPinchGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>recognizer 
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Pinch scale: %f&quot;</span>, recognizer.scale<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Source Code</h5>
<p>To get a feel for how gestures and the corresponding action methods work, download the Xcode project below and install the application on a device from within Xcode &#8211; look at the console output as you perform the various gestures to see the log messages for each gesture as it&#8217;s recognized.</p>
<p><a href="http://iPhoneDevelopertips.com/wp-content/uploads/2011/04/XcodeGestureRecognizers.zip">Gesture Recognizer Xcode Project</a></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/event-handling/gestures-recognizers-tap-pinchzoom-rotate-swipe-pan-long-press.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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[Events and Touch]]></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>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></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[Events and Touch]]></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>

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></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[Events and Touch]]></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>

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></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[Events and Touch]]></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>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/event-handling/determining-if-touch-or-point-is-within-rectangle.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

