<?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>Aaron Jefferson Villanueva&#039;s Blog Site&#187; Cocoa Touch</title>
	<atom:link href="http://malebolgia.shadowsonawall.net/tag/cocoa-touch/feed/" rel="self" type="application/rss+xml" />
	<link>http://malebolgia.shadowsonawall.net</link>
	<description>Programming / Designing Blogging from windows to mac.</description>
	<lastBuildDate>Fri, 06 Aug 2010 02:40:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Animating View</title>
		<link>http://malebolgia.shadowsonawall.net/2010/01/make-view-move-upward-uitextfield-begin-editing/</link>
		<comments>http://malebolgia.shadowsonawall.net/2010/01/make-view-move-upward-uitextfield-begin-editing/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 01:55:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Coding/Programming]]></category>
		<category><![CDATA[Objective C / Cocoa Touch [iPhone]]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://malebolgia.shadowsonawall.net/?p=181</guid>
		<description><![CDATA[
  Make View move upward - UITextField Begin Editing


Attached is the video - what I was trying to do, it happens that my first iPhone app has UITextField positioned where the text keypad will appear also, which will hide the current UITextField. Well I realized that I will encounter this often, so I guess [...]]]></description>
			<content:encoded><![CDATA[<p>
  <strong>Make View move upward - UITextField Begin Editing</strong>
</p>
<br />
<p>Attached is the video - what I was trying to do, it happens that my first <strong><span style="color: #808000;">iPhone</span></strong> app has <span style="color: #003300;">UITextField</span> positioned where the text keypad will appear also, which will hide the current <span style="color: #003300;">UITextField</span>. Well I realized that I will encounter this often, so I guess it's <span style="color: #333333;"><span style="text-decoration: underline;"><strong>important</strong></span></span> to have this <em><span style="color: #333333;">piece of code</span></em>.</p>
<br />
<p>in your interface builder assign the UITextField as <u>delegate</u></p>

<div id="attachment_187" class="wp-caption aligncenter" style="width: 299px"><a href="http://malebolgia.shadowsonawall.net/wp-content/uploads/2010/01/delegate.png"><img src="http://malebolgia.shadowsonawall.net/wp-content/uploads/2010/01/delegate.png" alt="UITextField as delegate" title="delegate" width="289" height="98" class="size-full wp-image-187" /></a><p class="wp-caption-text">UITextField as delegate</p></div>

<p>
on your .m file declare the static constant at the top of the code. </p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #a61390;">const</span> CGFloat KEYBOARD_ANIMATION_DURATION <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.3</span>;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">const</span> CGFloat MINIMUM_SCROLL_FRACTION <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.2</span>;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">const</span> CGFloat MAXIMUM_SCROLL_FRACTION <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.8</span>;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">const</span> CGFloat PORTRAIT_KEYBOARD_HEIGHT <span style="color: #002200;">=</span> <span style="color: #2400d9;">216</span>;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">const</span> CGFloat LANDSCAPE_KEYBOARD_HEIGHT <span style="color: #002200;">=</span> <span style="color: #2400d9;">140</span>;</pre></div></div>




<p>after at the @implementation put this global variable to be used for all events </p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"> CGFloat animatedDistance;</pre></div></div>




<p>at the textFieldDidBeginEditing event </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>textFieldDidBeginEditing<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITextField <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>textField <span style="color: #002200;">&#123;</span>
&nbsp;
	CGRect textFieldRect <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.view.window convertRect<span style="color: #002200;">:</span>textField.bounds fromView<span style="color: #002200;">:</span>textField<span style="color: #002200;">&#93;</span>;
    CGRect viewRect <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.view.window convertRect<span style="color: #002200;">:</span>self.view.bounds fromView<span style="color: #002200;">:</span>self.view<span style="color: #002200;">&#93;</span>;
&nbsp;
    CGFloat midline <span style="color: #002200;">=</span> textFieldRect.origin.y <span style="color: #002200;">+</span> <span style="color: #2400d9;">0.5</span> <span style="color: #002200;">*</span> textFieldRect.size.height;
    CGFloat numerator <span style="color: #002200;">=</span>
	midline <span style="color: #002200;">-</span> viewRect.origin.y <span style="color: #002200;">-</span> MINIMUM_SCROLL_FRACTION <span style="color: #002200;">*</span> viewRect.size.height;
    CGFloat denominator <span style="color: #002200;">=</span>
	<span style="color: #002200;">&#40;</span>MAXIMUM_SCROLL_FRACTION <span style="color: #002200;">-</span> MINIMUM_SCROLL_FRACTION<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> viewRect.size.height;
    CGFloat heightFraction <span style="color: #002200;">=</span> numerator <span style="color: #002200;">/</span> denominator;
&nbsp;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>heightFraction &lt; <span style="color: #2400d9;">0.0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        heightFraction <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>heightFraction &gt; <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        heightFraction <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.0</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
	UIInterfaceOrientation orientation <span style="color: #002200;">=</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> statusBarOrientation<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>orientation <span style="color: #002200;">==</span> UIInterfaceOrientationPortrait ||
        orientation <span style="color: #002200;">==</span> UIInterfaceOrientationPortraitUpsideDown<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        animatedDistance <span style="color: #002200;">=</span> <span style="color: #a61390;">floor</span><span style="color: #002200;">&#40;</span>PORTRAIT_KEYBOARD_HEIGHT <span style="color: #002200;">*</span> heightFraction<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span>
    <span style="color: #002200;">&#123;</span>
        animatedDistance <span style="color: #002200;">=</span> <span style="color: #a61390;">floor</span><span style="color: #002200;">&#40;</span>LANDSCAPE_KEYBOARD_HEIGHT <span style="color: #002200;">*</span> heightFraction<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
	CGRect viewFrame <span style="color: #002200;">=</span> self.view.frame;
    viewFrame.origin.y <span style="color: #002200;">-=</span> animatedDistance;
&nbsp;
    <span style="color: #002200;">&#91;</span>UIView beginAnimations<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> context<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>UIView setAnimationBeginsFromCurrentState<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>UIView setAnimationDuration<span style="color: #002200;">:</span>KEYBOARD_ANIMATION_DURATION<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self.view setFrame<span style="color: #002200;">:</span>viewFrame<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>




<p>at textFieldDidEndEditing event </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>textFieldDidEndEditing<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITextField <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>textField <span style="color: #002200;">&#123;</span>
    CGRect viewFrame <span style="color: #002200;">=</span> self.view.frame;
    viewFrame.origin.y <span style="color: #002200;">+=</span> animatedDistance;
&nbsp;
    <span style="color: #002200;">&#91;</span>UIView beginAnimations<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> context<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>UIView setAnimationBeginsFromCurrentState<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>UIView setAnimationDuration<span style="color: #002200;">:</span>KEYBOARD_ANIMATION_DURATION<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self.view setFrame<span style="color: #002200;">:</span>viewFrame<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>




<p>This event is textfield resign responder, once you press the return keypad it will hide the iPhone keyboard, this is important to test if the view will come back to its previous state.  </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;">BOOL</span><span style="color: #002200;">&#41;</span>textFieldShouldReturn<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITextField <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> textField
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>textField resignFirstResponder<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>




Demo.

<div id="container_UIText"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>
<script type="text/javascript">// <![CDATA[
var s3UIText = new SWFObject("http://malebolgia.shadowsonawall.net/flash/mediaplayer.swf","mediaplayer","388","794","8"); 
            s3UIText.addParam("allowfullscreen","true"); 		
            s3UIText.addVariable("width","388"); 		
            s3UIText.addVariable("height","794"); 		
             s3UIText.addVariable("file","http://media.shadowsonawall.net/videos/iphone_0001.flv");            
            s3UIText.addVariable("backcolor","0x000000");             
            s3UIText.addVariable("frontcolor","0xFFFFFF");             
                s3UIText.addVariable("lightcolor","0x840D16");             
                s3UIText.addVariable("screencolor","red"); 		
                s3UIText.write("container_UIText");
// ]]&gt;</script>
<br /><br />
<p> <strong><a href="http://media.shadowsonawall.net/downloads/KeyBoardTrickStudy.zip">Download the Source</a></strong> </p>]]></content:encoded>
			<wfw:commentRss>http://malebolgia.shadowsonawall.net/2010/01/make-view-move-upward-uitextfield-begin-editing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
