<?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>ZenBrains &#187; quartz</title>
	<atom:link href="http://www.zenbrains.com/blog/tag/quartz/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zenbrains.com/blog</link>
	<description>IPhone Development Team</description>
	<lastBuildDate>Tue, 17 May 2011 21:06:43 +0000</lastBuildDate>
	<language>es</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Vistas personalizadas</title>
		<link>http://www.zenbrains.com/blog/2010/06/vistas-personalizadas/</link>
		<comments>http://www.zenbrains.com/blog/2010/06/vistas-personalizadas/#comments</comments>
		<pubDate>Sat, 05 Jun 2010 20:32:48 +0000</pubDate>
		<dc:creator>Aram Julhakyan</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Programación]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[quartz]]></category>
		<category><![CDATA[UIKit]]></category>

		<guid isPermaLink="false">http://www.zenbrains.com/blog/?p=454</guid>
		<description><![CDATA[Hoy vamos a ver como se pueden crear vistas (UIViews) personalizados. En muchas ocasiones no tenemos suficiente con los controles que vienen con el UIKit y entonces tenemos que crear nuestros propios controles. En este artículo vamos a ver un ejemplo muy simple de creación de vistas propias. Al final del artículo podréis descargar el [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.zenbrains.com%2Fblog%2F2010%2F06%2Fvistas-personalizadas%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.zenbrains.com%2Fblog%2F2010%2F06%2Fvistas-personalizadas%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Hoy vamos a ver como se pueden crear vistas (UIViews) personalizados.<br />
En muchas ocasiones no tenemos suficiente con los controles que vienen con el UIKit y entonces tenemos que crear nuestros propios controles.<br />
En este artículo vamos a ver un ejemplo muy simple de creación de vistas propias.<br />
Al final del artículo podréis descargar el proyecto de ejemplo donde implementamos nuestra vista personalizada y hacemos uso de ella.<br />
En el ejemplo vamos a intentar crear una barra de progreso simple.<br />
Entonces, vamos a crear una nueva clase que heredará de UIView, la llamaremos &#8220;VistaPorcentaje&#8221;. Nuestra VistaPorcentaje tendrá dos propiedades: el porcentaje y el color de la barra.<br />
Así queda la declaración de la clase:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import </span>
&nbsp;
<span style="color: #a61390;">@interface</span> VistaPorcentaje <span style="color: #002200;">:</span> UIView <span style="color: #002200;">&#123;</span>
	UIColor <span style="color: #002200;">*</span>barColor;
	CGFloat porcentaje;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span>UIColor <span style="color: #002200;">*</span>barColor;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, setter<span style="color: #002200;">=</span>setPorcentaje<span style="color: #002200;">&#41;</span>CGFloat porcentaje;
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setPorcentaje<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>p;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Ahora que ya tenemos definida nuestra clase vamos a ver su implementación:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;VistaPorcentaje.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> VistaPorcentaje
<span style="color: #a61390;">@synthesize</span> barColor, porcentaje;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>frame <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		self.barColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor greenColor<span style="color: #002200;">&#93;</span>;
		self.porcentaje <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>;
&nbsp;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<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>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect <span style="color: #002200;">&#123;</span>
   CGContextRef context <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    context <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    CGContextSetFillColorWithColor<span style="color: #002200;">&#40;</span>context, self.barColor.CGColor<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Calculamos el ancho de la barra dependiendo del porcentaje</span>
	CGFloat ancho <span style="color: #002200;">=</span> self.frame.size.width <span style="color: #002200;">*</span> porcentaje<span style="color: #002200;">/</span><span style="color: #2400d9;">100.0</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Dibujamos la barra del porcentaje</span>
	CGRect r <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, ancho, self.frame.size.height<span style="color: #002200;">&#41;</span>;
	CGContextAddRect<span style="color: #002200;">&#40;</span>context, r<span style="color: #002200;">&#41;</span>;
    CGContextFillRect<span style="color: #002200;">&#40;</span>context, r<span style="color: #002200;">&#41;</span>;
&nbsp;
<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>setPorcentaje<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>p<span style="color: #002200;">&#123;</span>
	porcentaje <span style="color: #002200;">=</span> p;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>porcentaje <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">100.0</span><span style="color: #002200;">&#41;</span>
		porcentaje <span style="color: #002200;">=</span> <span style="color: #2400d9;">100.0</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>porcentaje <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">0.0</span><span style="color: #002200;">&#41;</span>
		porcentaje <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplay<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>dealloc <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>barColor release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Aquí los métodos claves son el drawRect y setPorcentaje. Dentro de drawRect implementamos toda la funcionalidad de pintura de nuestra vista. Básciamente en nuestro caso sólo pintamos un rectangulo de un tamaño equivalente al porcentaje respecto al ancho total de la vista.<br />
Y el otro método, setPorcentaje, se llama cada vez que el usuario cambia la propiedad porcentaje. Fijaros que en este método tenemos una llamada [self setNeedsDisplay]. Con esa llamada decismos que se repinte (nunca llamamos directamente a drawRect) ya que se ha cambiado el porcentaje.</p>
<p>A continuación podemos ver un ejemplo de nuestro control (estamos en un view controller):</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>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
	VistaPorcentaje <span style="color: #002200;">*</span>v <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>VistaPorcentaje alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">200</span>, <span style="color: #2400d9;">50</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	v.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;
	v.barColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor greenColor<span style="color: #002200;">&#93;</span>;
	v.porcentaje <span style="color: #002200;">=</span> <span style="color: #2400d9;">67.0</span>;
	<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>v<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>v release<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>A base de este simple ejemplo se puede construir controles propios para mostrar histogramas, crear controles propios que respondan a las acciones de usuario y miles de cosas más.</p>
<p><a href="http://www.zenbrains.com/blog/wp-content/uploads/2010/06/EjemploVistaPersonalizada.zip">EjemploVistaPersonalizada</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.zenbrains.com/blog/2010/06/vistas-personalizadas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

