<?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>Chris-Software.com &#187; touch begin</title>
	<atom:link href="http://chris-software.com/index.php/tag/touch-begin/feed/" rel="self" type="application/rss+xml" />
	<link>http://chris-software.com</link>
	<description>iPhone, iPod touch games, Objective-C Tutorials, krzysztofrutkowski.com</description>
	<lastBuildDate>Tue, 29 Mar 2011 09:58:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Touches</title>
		<link>http://chris-software.com/index.php/2009/04/26/touches/</link>
		<comments>http://chris-software.com/index.php/2009/04/26/touches/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 00:38:29 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Obj-C Tutorials]]></category>
		<category><![CDATA[coordinates]]></category>
		<category><![CDATA[handling touches events]]></category>
		<category><![CDATA[touch]]></category>
		<category><![CDATA[touch begin]]></category>
		<category><![CDATA[touch begins]]></category>
		<category><![CDATA[touch coordinates]]></category>
		<category><![CDATA[touch end]]></category>
		<category><![CDATA[touch ends]]></category>
		<category><![CDATA[touch move]]></category>
		<category><![CDATA[touches]]></category>

		<guid isPermaLink="false">http://chris-software.com/?page_id=594</guid>
		<description><![CDATA[Touches are handled by three methods:

touchesBegan:
touchesMoved:
touchesEnded:

To have access to touches you need to code these 3 methods (or one/two of them).
The code is very simple:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
} 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
touchesBegan: will be called whenever user touch the screen with one finger, and each time new touch [...]]]></description>
			<content:encoded><![CDATA[<p>Touches are handled by three methods:</p>
<ul>
<li><em>touchesBegan:</em></li>
<li><em>touchesMoved:</em></li>
<li><em>touchesEnded:</em></li>
</ul>
<p>To have access to touches you need to code these 3 methods (or one/two of them).</p>
<p>The code is very simple:</p>
<pre>- (<span>void</span>)touchesBegan:(<span>NSSet</span> *)touches withEvent:(<span>UIEvent</span> *)event {
} 
- (<span>void</span>)touchesMoved:(<span>NSSet</span> *)touches withEvent:(<span>UIEvent</span> *)event {
}
- (<span>void</span>)touchesEnded:(<span>NSSet</span> *)touches withEvent:(<span>UIEvent</span> *)event {
}</pre>
<p><em>touchesBegan:</em> will be called whenever user touch the screen with one finger, and each time new touch is declared for example with the second finger, nose ear, battery, foot&#8230;</p>
<p><em>touchesEnded</em>: will be called whenever any single touch is ended</p>
<p><em>touchesMoved:</em> hmmm it&#8217;s not so clear as you might think. Yes, this method will be called if any finger moves while touching the screen, but on real device this method is called almost immidietely after <em>touchesBegan</em>. Why is that? On iPhone simulator mouse plays role of the finger, so you can press and release the mouse/touch-pad button and both <em>touchesBegan</em> and <em>touchesEnded</em> will be called, but not <em>touchesMoved</em> provided that you won&#8217;t move a pointer. If you touch an iPhone / iPod screen in 99% the coordinates of where the touch started and ended will be different, so meanwhile <em>touchesMoved<span style="font-style: normal;"> will be called.</span></em></p>
<p><em><span style="font-style: normal;">You probably woud like to know what are the touch coordinates, it&#8217;s very easy to get them. Extend any of above three methods with this code:</span></em></p>
<pre><span>	</span><span>UITouch</span> *touch = [touches <span>anyObject</span>];
<span>	CGPoint</span> touchCoordinates = [touch <span>locationInView</span>:<span>self</span>.<span>view</span>];</pre>
<p><em>touchCoordinates.x</em> and <em>touchCoordinates.y</em> give you information you need.</p>
<p>If you would like to have details of more than single touch (you can touch the screen with as many fingers you want), this might be helpful:</p>
<pre><span>	</span><span>NSSet</span> *allTouches = [event <span>allTouches</span>];
<span>	</span><span>UITouch</span> *singleTouch;
<span>	</span><span>for</span> (<span>int</span> i = <span>0</span>; i &lt; [allTouches <span>count</span>]; i++) {
<span>		</span>singleTouch = [[allTouches <span>allObjects</span>] <span>objectAtIndex</span>:i];
<span><span>		</span></span>// whatever you need goes here
<span>	</span>}</pre>
<p>If you would like to simply know how many fingers are touching the screen use <em>[allTouches count];</em> with first line of above example.</p>
<p>If you want to do something when users double tap the screen for example try:</p>
<pre><span>	</span><span>if</span> ([touch <span>tapCount</span>] == <span>2</span>) {
<span><span>		</span></span>// whatever you need goes here
<span>	</span>}</pre>
<p>You can use only <em>tapCount</em> for the single touch. Taps aren&#8217;t counted for multiple touches.</p>
<p>Now something more interesting. How to check if user touched an object like image, label&#8230;? Before you enter any code in the <em>attributes</em> in <em>Interface Builder</em> of your item enable option: <em>User Interaction Enabled</em> and <em>Multiple Touches</em> if needed. <em>views</em> have <em>User Iteraction </em>enabled by default. You can also do it in <em>XCode</em>:</p>
<pre><span>	</span>myObject.userInteractionEnabled = <span>YES</span>;
<span><span>	</span></span><span>myObject</span><span>.</span>multipleTouchEnabled<span> = </span><span>YES</span><span>;</span></pre>
<p>Once <em>User Interaction</em> is enabled you can use <em>if statement</em> to perform any task:</p>
<pre><span>	</span><span>if</span> ([touch <span>view</span>] == myObject) {
<span><span>		</span>// whatever you need goes here</span><span>
<span>	</span>}</span></pre>
<p><a href="http://chris-software.com/wp-content/uploads/2009/04/touchessimulator.png"><img class="alignleft size-medium wp-image-595" style="margin-left: 30px; margin-right: 30px;" title="touchessimulator" src="http://chris-software.com/wp-content/uploads/2009/04/touchessimulator-161x300.png" alt="touchessimulator" width="161" height="300" /></a> I guess it&#8217;s all you need to know about touches. I prepared for you a project you can download. It gives you all information:</p>
<ul>
<li>where the touch began (x,y)</li>
<li>where the touch moved (x,y)</li>
<li>where the touch ended (x,y)</li>
<li>how many fingers touched the screen (in touch began)</li>
<li>the tap count</li>
<li>and a <em>Move Me</em> square you moves by moving your finger</li>
</ul>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/touches.zip"><img class="alignnone size-full wp-image-564" title="xcodeproj" src="http://chris-software.com/wp-content/uploads/2009/04/xcodeproj.png" alt="xcodeproj" width="128" height="128" /></a></p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/touches.zip">Download the project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chris-software.com/index.php/2009/04/26/touches/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>

