<?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>iPhoneGeek 爱疯极客 &#187; 用户界面</title>
	<atom:link href="http://www.iphone-geek.cn/category/%e7%bc%96%e7%a8%8b/%e7%94%a8%e6%88%b7%e7%95%8c%e9%9d%a2/feed" rel="self" type="application/rss+xml" />
	<link>http://www.iphone-geek.cn</link>
	<description>iPhone 新闻，编程，技巧与提示，代码，教程</description>
	<lastBuildDate>Thu, 08 Dec 2011 01:18:52 +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>iOS 5设备下UINavigationbar的背景修改</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/ios-5%e8%ae%be%e5%a4%87%e4%b8%8buinavigationbar%e7%9a%84%e8%83%8c%e6%99%af%e4%bf%ae%e6%94%b9</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/ios-5%e8%ae%be%e5%a4%87%e4%b8%8buinavigationbar%e7%9a%84%e8%83%8c%e6%99%af%e4%bf%ae%e6%94%b9#comments</comments>
		<pubDate>Thu, 08 Dec 2011 01:17:26 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[iOS 5]]></category>
		<category><![CDATA[UINavigationbar]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1198</guid>
		<description><![CDATA[最近我突然发现UINavigationbar背景修改的方法不起作用了，代码如下：
123456789@implementation UINavigationBar &#40;CustomImage&#41;

-&#40;void&#41;drawRect:&#40;CGRect&#41;rect 
&#123;
&#160; &#160; UIImage *image = &#91;UIImage imageNamed:@&#34;navigationbar.png&#34;&#93;;
&#160; &#160; &#91;image drawInRect:CGRectMake&#40;0,0,self.frame.size.width,self.frame.size.height&#41;&#93;;
&#125;

@end
发现原来是iOS 5的原因，如果运行在iOS 5以下的版本就没有问题了。经过实验以下方法适合iOS 5（放在ViewDidLoad中）：
123&#160; &#160; if &#40;&#91;self.navigationController.navigationBar respondsToSelector:@selector&#40; setBackgroundImage:forBarMetrics:&#41;&#93;&#41;&#123;
&#160; &#160; &#160; &#160; &#91;self.navigationController.navigationBar setBackgroundImage:&#91;UIImage imageNamed:@&#34;navigationbar.png&#34;&#93; forBarMetrics:UIBarMetricsDefault&#93;;
&#160; &#160; &#125;
第一条if语句的作用是防止程序在iOS 5以下的版本中崩溃。
这样，依靠这两段代码，我的UINavigationbar的背景问题在iOS 5及以下版本中得到了完美的解决。
]]></description>
			<content:encoded><![CDATA[<p>最近我突然发现UINavigationbar背景修改的方法不起作用了，代码如下：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@implementation</span> UINavigationBar <span style="color: #002200;">&#40;</span>CustomImage<span style="color: #002200;">&#41;</span><br />
<br />
<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 <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;navigationbar.png&quot;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>image drawInRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>0,0,self.frame.size.width,self.frame.size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>发现原来是iOS 5的原因，如果运行在iOS 5以下的版本就没有问题了。经过实验以下方法适合iOS 5（放在ViewDidLoad中）：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self.navigationController.navigationBar respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span> setBackgroundImage<span style="color: #002200;">:</span>forBarMetrics<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.navigationController.navigationBar setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;navigationbar.png&quot;</span><span style="color: #002200;">&#93;</span> forBarMetrics<span style="color: #002200;">:</span>UIBarMetricsDefault<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>第一条if语句的作用是防止程序在iOS 5以下的版本中崩溃。</p>
<p>这样，依靠这两段代码，我的UINavigationbar的背景问题在iOS 5及以下版本中得到了完美的解决。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/ios-5%e8%ae%be%e5%a4%87%e4%b8%8buinavigationbar%e7%9a%84%e8%83%8c%e6%99%af%e4%bf%ae%e6%94%b9/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>创建iOS 5 News Stand应用程序之一 &#8211; 外观</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%88%9b%e5%bb%baios-5-news-stand%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b9%8b%e4%b8%80-%e5%a4%96%e8%a7%82</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%88%9b%e5%bb%baios-5-news-stand%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b9%8b%e4%b8%80-%e5%a4%96%e8%a7%82#comments</comments>
		<pubDate>Fri, 21 Oct 2011 02:52:06 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[Newsstand]]></category>
		<category><![CDATA[提示与技巧]]></category>
		<category><![CDATA[教程]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1189</guid>
		<description><![CDATA[iOS 5提供了一个新的framework &#8211; Newsstand framework，它允许把应用程序运行于News Stand中。实际上，News Stand相当于一个特制的文件夹专门放置报纸，杂志类应用程序。由于时间有限，这里我分几个部分介绍这一技术。

首先介绍的就是怎样把一个应用程序改变成一个News Stand程序，这实际上有两步工作，一是让程序运行于News Stand，二是改变程序的图标。

1. 让程序运行于News Stand内

可以在Info.plist中添加

12&#60;key&#62;UINewsstandApp&#60;/key&#62;
&#160; &#60;true/&#62;

或者直接在Xcode中更改Info.plist（如图）：



就这么简单，运行！你的程序就运行在News Stand中了。


不过，出现在News Stand中的是一个非常丑陋的白色方框。这一定不是你需要的效果。那么，我们需要第二步。

2. 为你的News Stand程序添加图标

应用程序仍需定义标准图标，这些图标用于settings，search，Push等，（而且你的程序有可能运行于iOS 5以前的版本）。Newsstand 图标可以反应应用的内容，可以动态更新，另外还可以加一些修饰，使其看上去就像真正的杂志或者报纸。

你可以直接修改Info.plist

12345678910111213141516171819&#60;key&#62;CFBundleIcons&#60;/key&#62;
&#60;dict&#62;
&#160; &#160; &#60;key&#62;CFBundlePrimaryIcon&#60;/key&#62;
&#160; &#160; &#60;dict&#62;
&#160; &#160; &#160; &#160; &#60;key&#62;CFBundleIconFiles&#60;/key&#62;
&#160; &#160; &#160; &#160; &#60;array&#62;
&#160; &#160; &#160; &#160; &#160; &#160; &#60;string&#62;Icon.png&#60;/string&#62;
&#160; &#160; &#160; &#160; &#160; &#160; &#60;string&#62;Icon@2x.png&#60;/string&#62;
&#160; &#160; &#160; &#160; &#60;/array&#62;
&#160; &#160; &#60;/dict&#62;
&#160; &#160; &#60;key&#62;UINewsstandIcon&#60;/key&#62;
&#160; &#160; &#60;dict&#62;
&#160; &#160; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>iOS 5提供了一个新的framework &#8211; Newsstand framework，它允许把应用程序运行于News Stand中。实际上，News Stand相当于一个特制的文件夹专门放置报纸，杂志类应用程序。由于时间有限，这里我分几个部分介绍这一技术。<br />
<br class="spacer_" /><br />
首先介绍的就是怎样把一个应用程序改变成一个News Stand程序，这实际上有两步工作，一是让程序运行于News Stand，二是改变程序的图标。<br />
<span id="more-1189"></span></p>
<h2>1. 让程序运行于News Stand内</h2>
<p><br class="spacer_" /><br />
可以在Info.plist中添加</p>
<blockquote>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UINewsstandApp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></td></tr></tbody></table></div>
</blockquote>
<p>或者直接在Xcode中更改Info.plist（如图）：<br />
<br class="spacer_" /><br />
<a href="http://www.iphone-geek.cn/wp-content/uploads/2011/10/s1.jpg"><img class="aligncenter size-full wp-image-1190" title="s1" src="http://www.iphone-geek.cn/wp-content/uploads/2011/10/s1.jpg" alt="s1" width="680" height="203" /></a><br />
<br class="spacer_" /><br />
就这么简单，运行！你的程序就运行在News Stand中了。</p>
<p style="text-align: center;"><a href="http://www.iphone-geek.cn/wp-content/uploads/2011/10/s2.jpg"><img class="aligncenter size-full wp-image-1191" title="s2" src="http://www.iphone-geek.cn/wp-content/uploads/2011/10/s2.jpg" alt="s2" width="651" height="414" /></a></p>
<p><br class="spacer_" /><br />
不过，出现在News Stand中的是一个非常丑陋的白色方框。这一定不是你需要的效果。那么，我们需要第二步。<br />
<br class="spacer_" /></p>
<h2>2. 为你的News Stand程序添加图标</h2>
<p><br class="spacer_" /><br />
应用程序仍需定义标准图标，这些图标用于settings，search，Push等，（而且你的程序有可能运行于iOS 5以前的版本）。Newsstand 图标可以反应应用的内容，可以动态更新，另外还可以加一些修饰，使其看上去就像真正的杂志或者报纸。<br />
<br class="spacer_" /><br />
你可以直接修改Info.plist</p>
<blockquote>
<div class="codecolorer-container xml mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleIcons<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundlePrimaryIcon<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleIconFiles<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Icon.png<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Icon@2x.png<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UINewsstandIcon<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CFBundleIconFiles<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NewsstandIcon1.png<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NewsstandIcon1@2x.png<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
</blockquote>
<p><br class="spacer_" /><br />
或者直接使用Xcode编辑：<br />
<br class="spacer_" /><br />
<a href="http://www.iphone-geek.cn/wp-content/uploads/2011/10/s3.jpg"><img class="aligncenter size-full wp-image-1192" title="s3" src="http://www.iphone-geek.cn/wp-content/uploads/2011/10/s3.jpg" alt="s3" width="517" height="240" /></a><br />
<br class="spacer_" /><br />
关于BindingType和BindingEdge应该很容易理解，我就不知赘述了。另外Newsstand中的图标不一定是正方形，只是不知有没有尺寸上的限制。<br />
<br class="spacer_" /><br />
<a href="http://www.iphone-geek.cn/wp-content/uploads/2011/10/s4.jpg"><img class="aligncenter size-full wp-image-1193" title="s4" src="http://www.iphone-geek.cn/wp-content/uploads/2011/10/s4.jpg" alt="s4" width="398" height="264" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%88%9b%e5%bb%baios-5-news-stand%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b9%8b%e4%b8%80-%e5%a4%96%e8%a7%82/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>强迫UIView以某种方向显示的秘诀</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%bc%ba%e8%bf%abuiview%e4%bb%a5%e6%9f%90%e7%a7%8d%e6%96%b9%e5%90%91%e6%98%be%e7%a4%ba%e7%9a%84%e7%a7%98%e8%af%80</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%bc%ba%e8%bf%abuiview%e4%bb%a5%e6%9f%90%e7%a7%8d%e6%96%b9%e5%90%91%e6%98%be%e7%a4%ba%e7%9a%84%e7%a7%98%e8%af%80#comments</comments>
		<pubDate>Thu, 25 Aug 2011 06:29:53 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[提示与技巧]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1182</guid>
		<description><![CDATA[我有一个项目其中某些UIView必须以特定的方向（Portrait或者Landscape）显示。这个看似简单的问题，困惑了我很久，直到今天我才完全找到解决的方法。

为简单起见，我以一个简单的例子说明一下我的问题。我有一个允许各种方向知道旋转的RootViewController，它包括一个共三行的UITableView，第一行显示“Portrait”，第二行显示“Landscape”，第三行显示“Autorotation”，点击某些行后，使用pushViewController打开一个DetailViewController，这个View Controller控制的view将根据行的内容有所不同。比如，如果按下的是第一行，则在DetailViewController中显示“Portrait”，并只允许UIView以portrait方式显示。

首先，我根据按下的行号，以orientation作为参数，传递给DetailViewController，在此ViewController的shouldAutorotateToInterfaceOrientation方法中根据orentation参数返回。代码如下：

1234567891011&#160;- &#40;BOOL&#41;shouldAutorotateToInterfaceOrientation:&#40;UIInterfaceOrientation&#41;interfaceOrientation
&#123;
&#160; &#160; if &#40; self.orientation == 0 &#41; // allow portrait
&#160; &#160; &#160; &#160; return &#40;interfaceOrientation == UIInterfaceOrientationPortrait&#41; &#124;&#124; &#40;interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown&#41;;
&#160; &#160; else if &#40; self.orientation == 1 &#41; &#160;// allow landscape
&#160; &#160; &#123; &#160; &#160; &#160; &#160;
&#160; &#160; &#160; &#160; return &#40;interfaceOrientation == UIInterfaceOrientationLandscapeLeft&#41; &#124;&#124; &#40;interfaceOrientation == UIInterfaceOrientationLandscapeRight&#41;;
&#160; &#160; &#125;
&#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>我有一个项目其中某些UIView必须以特定的方向（Portrait或者Landscape）显示。这个看似简单的问题，困惑了我很久，直到今天我才完全找到解决的方法。<br />
<span id="more-1182"></span><br />
为简单起见，我以一个简单的例子说明一下我的问题。我有一个允许各种方向知道旋转的RootViewController，它包括一个共三行的UITableView，第一行显示“Portrait”，第二行显示“Landscape”，第三行显示“Autorotation”，点击某些行后，使用pushViewController打开一个DetailViewController，这个View Controller控制的view将根据行的内容有所不同。比如，如果按下的是第一行，则在DetailViewController中显示“Portrait”，并只允许UIView以portrait方式显示。<br />
<br class="spacer_" /><br />
首先，我根据按下的行号，以orientation作为参数，传递给DetailViewController，在此ViewController的shouldAutorotateToInterfaceOrientation方法中根据orentation参数返回。代码如下：<br />
<br class="spacer_" /></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>shouldAutorotateToInterfaceOrientation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIInterfaceOrientation<span style="color: #002200;">&#41;</span>interfaceOrientation<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> self.orientation <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// allow portrait</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>interfaceOrientation <span style="color: #002200;">==</span> UIInterfaceOrientationPortrait<span style="color: #002200;">&#41;</span> || <span style="color: #002200;">&#40;</span>interfaceOrientation <span style="color: #002200;">==</span> UIInterfaceOrientationPortraitUpsideDown<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> self.orientation <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span> &nbsp;<span style="color: #11740a; font-style: italic;">// allow landscape</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>interfaceOrientation <span style="color: #002200;">==</span> UIInterfaceOrientationLandscapeLeft<span style="color: #002200;">&#41;</span> || <span style="color: #002200;">&#40;</span>interfaceOrientation <span style="color: #002200;">==</span> UIInterfaceOrientationLandscapeRight<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// allow autorotation</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p><br class="spacer_" /><br />
以上代码仅仅使得DetailViewController在特定情况下确定是否允许进行自动旋转。但如果初始的方向不正确的话，它却无能为力。比如，我在RootViewController（它允许任何方向的旋转）处于Landscape方向时，点击第一行（row 0），此时的DetailViewController所呈现的UIView仍然处于Landscape，当然这与我程序的本意当然不符。<br />
<br class="spacer_" /><br />
于是我的问题就变成了怎样强迫UIView旋转到特定方向？我在网上搜索到一种方案，即UIDvice的setOrientation: 方法，不过遗憾的是此方法是私有api，当然我不能接受。于是我又试验了UIApplication中setStatusBarOrientation:方法，该方法果然把状态条旋转到了我需要的方向，不过我的UIView还是处于不正确的方向（以我上一段提到的情况为例）。因为在创建DetailViewController时触发的shouldAutorotateToInterfaceOrientation中（发生在setStatusBarOrientation之前），当时的UIInterfaceOrientation还是为landscape，而我的orientation参数为0，所以返回为NO，因此屏幕并没有旋转。有什么方法能够再触发一次shouldAutorotateToInterfaceOrientation吗？<br />
<br class="spacer_" /><br />
答案是肯定的，见我的代码：<br />
<br class="spacer_" /></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// trick to retrigger shouldAutorotateToInterfaceOrientation method</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UIWindow <span style="color: #002200;">*</span>window <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> keyWindow<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; UIView <span style="color: #002200;">*</span>view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>window.subviews objectAtIndex<span style="color: #002200;">:</span>0<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>view removeFromSuperview<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>view<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p><br class="spacer_" /><br />
在setStatusBarOrientation后，调用以上代码，强行触发了shouldAutorotateToInterfaceOrientation了，此时orientation为0，但interfaceOrientation我已经通过setStatusBarOrientation设置成了UIInterfaceOrientationPortrait，所以会返回YES，直接导致UIView的旋转。（以上代码是我在<a href="http://goodliffe.blogspot.com/2009/12/iphone-forcing-uiview-to-reorientate.html">网上</a>看到的方法）。<br />
<br class="spacer_" /><br />
至此，整个解决方案就比较完满了，代码<a href="http://www.iphone-geek.cn/wp-content/uploads/2011/08/Autorotation.zip">下载</a>。不过请注意我的代码实例仅仅针对一种情况进行了处理（即orientation等于1，即仅仅允许UIView处于landscape时），其他情况依此类推。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%bc%ba%e8%bf%abuiview%e4%bb%a5%e6%9f%90%e7%a7%8d%e6%96%b9%e5%90%91%e6%98%be%e7%a4%ba%e7%9a%84%e7%a7%98%e8%af%80/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>presentModalViewController显示半透明UIView</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/presentmodalviewcontroller%e6%98%be%e7%a4%ba%e5%8d%8a%e9%80%8f%e6%98%8euiview</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/presentmodalviewcontroller%e6%98%be%e7%a4%ba%e5%8d%8a%e9%80%8f%e6%98%8euiview#comments</comments>
		<pubDate>Tue, 05 Jul 2011 05:32:32 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1175</guid>
		<description><![CDATA[很多时候我们需要使用presentModalViewController来显示Modal View。如果需要显示半透明的Modal View应该怎么办呢？当然可以自己创建一个半透明的UIView，然后模拟presentModalViewController的动画效果。
不过iOS 4以后的版本再也不需要怎么麻烦了，有一个非常简单的方法，示例如下（这段代码运行于一个View Controller中)：
123456&#160; &#160; UIViewController* transparentView = &#91;&#91;UIViewController alloc&#93; init&#93;;&#160; &#160; &#160; &#160; &#160; &#160; 

&#160; &#160; &#160; &#160;UIViewController* controller = self.view.window.rootViewController;
&#160; &#160; &#160; &#160;transparentView.view.backgroundColor = &#91;UIColor clearColor&#93;;
&#160; &#160; &#160; &#160;controller.modalPresentationStyle = UIModalPresentationCurrentContext; &#160; &#160; &#160; &#160;
&#160; &#160; &#160; &#160;&#91;controller presentModalViewController:transparentView animated:YES&#93;;
其要点就是使用iOS特有的rootViewController来显示Modal View。
]]></description>
			<content:encoded><![CDATA[<p>很多时候我们需要使用presentModalViewController来显示Modal View。如果需要显示半透明的Modal View应该怎么办呢？当然可以自己创建一个半透明的UIView，然后模拟presentModalViewController的动画效果。</p>
<p>不过iOS 4以后的版本再也不需要怎么麻烦了，有一个非常简单的方法，示例如下（这段代码运行于一个View Controller中)：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; UIViewController<span style="color: #002200;">*</span> transparentView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp;UIViewController<span style="color: #002200;">*</span> controller <span style="color: #002200;">=</span> self.view.window.rootViewController;<br />
&nbsp; &nbsp; &nbsp; &nbsp;transparentView.view.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;controller.modalPresentationStyle <span style="color: #002200;">=</span> UIModalPresentationCurrentContext; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #002200;">&#91;</span>controller presentModalViewController<span style="color: #002200;">:</span>transparentView animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>其要点就是使用iOS特有的rootViewController来显示Modal View。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/presentmodalviewcontroller%e6%98%be%e7%a4%ba%e5%8d%8a%e9%80%8f%e6%98%8euiview/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>分享一小段代码</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%88%86%e4%ba%ab%e4%b8%80%e5%b0%8f%e6%ae%b5%e4%bb%a3%e7%a0%81</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%88%86%e4%ba%ab%e4%b8%80%e5%b0%8f%e6%ae%b5%e4%bb%a3%e7%a0%81#comments</comments>
		<pubDate>Fri, 13 May 2011 06:13:58 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[提示与技巧]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1169</guid>
		<description><![CDATA[我们经常会遇到这种情况，某个按钮太小很难按到，又不想把按钮图片做得太大。我把它作成了一个Class Method，放在了我的Utils类中。
123456789101112131415+&#40;void&#41;resizeTouchableAreaForButton:&#40;UIButton* &#41;button withNewSize:&#40;CGSize&#41;size
&#123;
&#160; &#160; // increase margin around button based on width
&#160; &#160; const CGFloat margin_h = 0.5f * &#40; size.width - button.frame.size.width &#41;;
&#160; &#160; const CGFloat margin_v = 0.5f * &#40; size.height - button.frame.size.height &#41;;
&#160; &#160; 
&#160; &#160; // add margin on all four sides of the button
&#160; &#160; CGRect newFrame = button.frame;
&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>我们经常会遇到这种情况，某个按钮太小很难按到，又不想把按钮图片做得太大。我把它作成了一个Class Method，放在了我的Utils类中。</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">+</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>resizeTouchableAreaForButton<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton<span style="color: #002200;">*</span> <span style="color: #002200;">&#41;</span>button withNewSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>size<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// increase margin around button based on width</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">const</span> CGFloat margin_h <span style="color: #002200;">=</span> 0.5f <span style="color: #002200;">*</span> <span style="color: #002200;">&#40;</span> size.width <span style="color: #002200;">-</span> button.frame.size.width <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">const</span> CGFloat margin_v <span style="color: #002200;">=</span> 0.5f <span style="color: #002200;">*</span> <span style="color: #002200;">&#40;</span> size.height <span style="color: #002200;">-</span> button.frame.size.height <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">// add margin on all four sides of the button</span><br />
&nbsp; &nbsp; CGRect newFrame <span style="color: #002200;">=</span> button.frame;<br />
&nbsp; &nbsp; newFrame.origin.x <span style="color: #002200;">-=</span> margin_h;<br />
&nbsp; &nbsp; newFrame.origin.y <span style="color: #002200;">-=</span> margin_v;<br />
&nbsp; &nbsp; newFrame.size.width &nbsp;<span style="color: #002200;">+=</span> 2.0f <span style="color: #002200;">*</span> margin_h;<br />
&nbsp; &nbsp; newFrame.size.height <span style="color: #002200;">+=</span> 2.0f <span style="color: #002200;">*</span> margin_v;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; button.frame <span style="color: #002200;">=</span> newFrame;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p>这段代码把按钮的可按区域扩大到新的尺寸。不过有一个前提是使用UIButton的setImage而不是setBackgroundImage来设置按钮图片。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e5%88%86%e4%ba%ab%e4%b8%80%e5%b0%8f%e6%ae%b5%e4%bb%a3%e7%a0%81/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>检查UIWebView上touch的最简单的方法</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e6%a3%80%e6%9f%a5uiwebview%e4%b8%8atouch%e7%9a%84%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84%e6%96%b9%e6%b3%95</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e6%a3%80%e6%9f%a5uiwebview%e4%b8%8atouch%e7%9a%84%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84%e6%96%b9%e6%b3%95#comments</comments>
		<pubDate>Thu, 10 Mar 2011 02:09:29 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[提示与技巧]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1138</guid>
		<description><![CDATA[我有一个程序需要检测UIWebView是否有touch动作，不幸得很，UIWebView上的touchesBegan等事件无法被检测。在网上查了一下，有许多解决方法，比如在UIWebView上再加一个透明的UIView，重置UIWindow的sendEvent或重置UIWebView的hitest方法等等，要么就是方案不太完美，要么就是太过复杂，经过实验我使用的这种方法最为简单（否则我也没有时间写在这里了），当然我只要求检测UIWebView上有touch动作即可。我的方法是使用UITapGestureRecognizer。关键的地方有两点，请看我的代码（我的代码是在一个UIViewController中):

12345&#160; &#160; UITapGestureRecognizer* singleTap = &#91;&#91;UITapGestureRecognizer alloc&#93; initWithTarget:self action:@selector&#40;handleSingleTap:&#41;&#93;;
&#160; &#160; &#91;self.view addGestureRecognizer:singleTap&#93;;
&#160; &#160; singleTap.delegate = self;
&#160; &#160; singleTap.cancelsTouchesInView = NO;
&#160; &#160; &#91;singleTap release&#93;;
第三行是第一个关键的地方，就是必须设置UITapGuestureRecognizer的delegate（一般我们直接使用initＷithＴarget，无需设置其delegate）。
第二个关键的地方是在下列delegate方法return YES。

1234- &#40;BOOL&#41;gestureRecognizer:&#40;UIGestureRecognizer *&#41;gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:&#40;UIGestureRecognizer *&#41;otherGestureRecognizer
&#123;
&#160; &#160; return YES;
&#125;
做到这两点，你的UIWebView就可以响应touch事件了，你可以使用不同的UIGestureRecognizer来满足你不同的要求等。(handleSingleTap方法请自行补上）。
]]></description>
			<content:encoded><![CDATA[<p>我有一个程序需要检测UIWebView是否有touch动作，不幸得很，UIWebView上的touchesBegan等事件无法被检测。在网上查了一下，有许多解决方法，比如在UIWebView上再加一个透明的UIView，重置UIWindow的sendEvent或重置UIWebView的hitest方法等等，要么就是方案不太完美，要么就是太过复杂，经过实验我使用的这种方法最为简单（否则我也没有时间写在这里了），当然我只要求检测UIWebView上有touch动作即可。我的方法是使用UITapGestureRecognizer。关键的地方有两点，请看我的代码（我的代码是在一个UIViewController中):<br />
<br class="spacer_" /></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; UITapGestureRecognizer<span style="color: #002200;">*</span> singleTap <span style="color: #002200;">=</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>handleSingleTap<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>self.view addGestureRecognizer<span style="color: #002200;">:</span>singleTap<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; singleTap.delegate <span style="color: #002200;">=</span> self;<br />
&nbsp; &nbsp; singleTap.cancelsTouchesInView <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>singleTap release<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p><br class="spacer_" />第三行是第一个关键的地方，就是必须设置UITapGuestureRecognizer的delegate（一般我们直接使用initＷithＴarget，无需设置其delegate）。<br />
<br class="spacer_" />第二个关键的地方是在下列delegate方法return YES。<br />
<br class="spacer_" /></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>gestureRecognizer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIGestureRecognizer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>otherGestureRecognizer<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;<br />
<span style="color: #002200;">&#125;</span></div></td></tr></tbody></table></div>
<p><br class="spacer_" />做到这两点，你的UIWebView就可以响应touch事件了，你可以使用不同的UIGestureRecognizer来满足你不同的要求等。(handleSingleTap方法请自行补上）。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e6%a3%80%e6%9f%a5uiwebview%e4%b8%8atouch%e7%9a%84%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84%e6%96%b9%e6%b3%95/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>怎样使UISearchBar背景透明</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e6%80%8e%e6%a0%b7%e4%bd%bfuisearchbar%e8%83%8c%e6%99%af%e9%80%8f%e6%98%8e</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e6%80%8e%e6%a0%b7%e4%bd%bfuisearchbar%e8%83%8c%e6%99%af%e9%80%8f%e6%98%8e#comments</comments>
		<pubDate>Tue, 14 Sep 2010 10:51:09 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[UISearchBar]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1072</guid>
		<description><![CDATA[在使用UISearchBar时，将背景色设定为clearColor，或者将translucent设为YES，都不能使背景透明，经过一番研究，发现了一种超级简单和实用的方法：
1&#91;&#91;searchbar.subviews objectAtIndex:0&#93;removeFromSuperview&#93;;
背景完全消除了，只剩下搜索框本身了。
]]></description>
			<content:encoded><![CDATA[<p>在使用UISearchBar时，将背景色设定为clearColor，或者将translucent设为YES，都不能使背景透明，经过一番研究，发现了一种超级简单和实用的方法：</p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>searchbar.subviews objectAtIndex<span style="color: #002200;">:</span>0<span style="color: #002200;">&#93;</span>removeFromSuperview<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p>背景完全消除了，只剩下搜索框本身了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e6%80%8e%e6%a0%b7%e4%bd%bfuisearchbar%e8%83%8c%e6%99%af%e9%80%8f%e6%98%8e/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>访问外部网页</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%ae%bf%e9%97%ae%e5%a4%96%e9%83%a8%e7%bd%91%e9%a1%b5</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%ae%bf%e9%97%ae%e5%a4%96%e9%83%a8%e7%bd%91%e9%a1%b5#comments</comments>
		<pubDate>Tue, 10 Aug 2010 01:42:55 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[提示与技巧]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=1045</guid>
		<description><![CDATA[实际上有两种方法，第一种是：

1&#91;&#91;UIApplication sharedApplication&#93; openURL:&#91;NSURL URLWithString:@&#34;http://www.google.com&#34;&#93;&#93;;

这种方法将会退出当前程序，用Safari打开url。我有好几个朋友都问到这个问题，实际上非常简单，可以使用UIWebView在当前程序中打开网页（假定你的UIWebView实例为webView）：

12345678910&#160; &#160; &#160;NSString *urlAddress = @&#34;http://localhost&#34;;
&#160; &#160; &#160; &#160; 
&#160; &#160; //Create a URL object.
&#160; &#160; NSURL *url = &#91;NSURL URLWithString:urlAddress&#93;;
&#160; &#160; &#160; &#160; 
&#160; &#160; //URL Requst Object
&#160; &#160; NSURLRequest *requestObj = &#91;NSURLRequest requestWithURL:url&#93;;
&#160; &#160; &#160; &#160; 
&#160; &#160; //Load the request in the UIWebView.
&#160; &#160; &#91;webView loadRequest:requestObj&#93;;
]]></description>
			<content:encoded><![CDATA[<p>实际上有两种方法，第一种是：<br />
<br class="spacer_" /></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> openURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span style="color: #400080;">NSURL</span></a> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.google.com&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
<p><br class="spacer_" /><br />
这种方法将会退出当前程序，用Safari打开url。我有好几个朋友都问到这个问题，实际上非常简单，可以使用UIWebView在当前程序中打开网页（假定你的UIWebView实例为webView）：<br />
<br class="spacer_" /></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp;<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>urlAddress <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://localhost&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//Create a URL object.</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span style="color: #400080;">NSURL</span></a> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span style="color: #400080;">NSURL</span></a> URLWithString<span style="color: #002200;">:</span>urlAddress<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//URL Requst Object</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/"><span style="color: #400080;">NSURLRequest</span></a> <span style="color: #002200;">*</span>requestObj <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/"><span style="color: #400080;">NSURLRequest</span></a> requestWithURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #11740a; font-style: italic;">//Load the request in the UIWebView.</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>webView loadRequest<span style="color: #002200;">:</span>requestObj<span style="color: #002200;">&#93;</span>;</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e8%ae%bf%e9%97%ae%e5%a4%96%e9%83%a8%e7%bd%91%e9%a1%b5/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>以密码方式显示UITextField文本</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%bb%a5%e5%af%86%e7%a0%81%e6%96%b9%e5%bc%8f%e6%98%be%e7%a4%bauitextfield%e6%96%87%e6%9c%ac</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%bb%a5%e5%af%86%e7%a0%81%e6%96%b9%e5%bc%8f%e6%98%be%e7%a4%bauitextfield%e6%96%87%e6%9c%ac#comments</comments>
		<pubDate>Sun, 25 Jul 2010 13:44:14 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[代码片段]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=988</guid>
		<description><![CDATA[很简单，如下：
1textField.secureTextEntry = YES;


]]></description>
			<content:encoded><![CDATA[<p>很简单，如下：</p>
<p><div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">textField.secureTextEntry <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;</div></td></tr></tbody></table></div>
</p>
<p><br class="spacer_" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%bb%a5%e5%af%86%e7%a0%81%e6%96%b9%e5%bc%8f%e6%98%be%e7%a4%bauitextfield%e6%96%87%e6%9c%ac/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>不规则形状的UIButton</title>
		<link>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%b8%8d%e8%a7%84%e5%88%99%e5%bd%a2%e7%8a%b6%e7%9a%84uibutton</link>
		<comments>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%b8%8d%e8%a7%84%e5%88%99%e5%bd%a2%e7%8a%b6%e7%9a%84uibutton#comments</comments>
		<pubDate>Tue, 04 May 2010 02:48:54 +0000</pubDate>
		<dc:creator>bagusflyer</dc:creator>
				<category><![CDATA[用户界面]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[编程，用户界面]]></category>

		<guid isPermaLink="false">http://www.iphone-geek.cn/?p=936</guid>
		<description><![CDATA[有的时候，我们需要使用非规则形状的按钮。UIButton允许你选择带有alpha通道的图像。比如，我使用下面四个图像：



然后用Interface Builder创建用户定义按钮，你可以透过图像的透明部分看到后面的按钮（假定按钮未定义为opaque）。.然而 UIButton 的点击测试（hit-testing)并未考虑图像的透明性，所以当你将图像重叠放置时，如图所示：



如果你点击此处：




默认的点击测试的结果是绿色菱形按钮被按下，而不是蓝色按钮。当然这可能就是你需要的效果，但大部分情况下并非如你所愿。那么怎样才能让你的程序正常工作？实际上很简单，你只需要一个UIButton的子类并重写点击测试方法。

然而，首先你需要一个方法能确定图像上指定点是透明的。遗憾的是UIImage无法像Cocoa为NSImage提供的NSBitmapRepresentation 那样方便地访问位图数据。但是每个UIImage都具有一个称为CGImage的属性可以访问内部图像数据，Apple发布了一篇技术文章介绍了怎样通过CGImageRef访问内部位图数据。

根据这篇文章的介绍，我们很容易就写出一个方法，它以CGPoint为参数，根据该点是否透明（0）与否返回YES或NO。

UIImage-Alpha.h
12345678#import &#60;UIKit/UIKit.h&#62;

@interface UIImage&#40;Alpha&#41;

- &#40;NSData *&#41;ARGBData;
- &#40;BOOL&#41;isPointTransparent:&#40;CGPoint&#41;point;

@end

UIImage-Alpha.m
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283CGContextRef CreateARGBBitmapContext &#40;CGImageRef inImage&#41;
&#123;
&#160; &#160; CGContextRef &#160; &#160;context = NULL;
&#160; &#160; CGColorSpaceRef colorSpace;
&#160; &#160; void * &#160; &#160; &#160; &#160; &#160;bitmapData;
&#160; &#160; int &#160; &#160; &#160; &#160; &#160; &#160; bitmapByteCount;
&#160; &#160; int &#160; &#160; &#160; &#160; &#160; &#160; bitmapBytesPerRow;

&#160; &#160; size_t pixelsWide = CGImageGetWidth&#40;inImage&#41;;
&#160; &#160; size_t pixelsHigh = CGImageGetHeight&#40;inImage&#41;;
&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>有的时候，我们需要使用非规则形状的按钮。UIButton允许你选择带有alpha通道的图像。比如，我使用下面四个图像：</p>
<p><br class="spacer_" /></p>
<p><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://www.iphone-geek.cn/wp-content/uploads/2010/05/image.png" border="0" alt="image" width="644" height="398" /></p>
<p><br class="spacer_" /></p>
<p>然后用Interface Builder创建用户定义按钮，你可以透过图像的透明部分看到后面的按钮（假定按钮未定义为opaque）。.然而 UIButton 的点击测试（hit-testing)并未考虑图像的透明性，所以当你将图像重叠放置时，如图所示：</p>
<p><br class="spacer_" /></p>
<p><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" src="http://www.iphone-geek.cn/wp-content/uploads/2010/05/image1.png" border="0" alt="image" width="240" height="130" /></p>
<p><br class="spacer_" /></p>
<p>如果你点击此处：</p>
<p><br class="spacer_" /></p>
<p><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" src="http://www.iphone-geek.cn/wp-content/uploads/2010/05/image2.png" border="0" alt="image" width="240" height="130" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>默认的点击测试的结果是绿色菱形按钮被按下，而不是蓝色按钮。当然这可能就是你需要的效果，但大部分情况下并非如你所愿。那么怎样才能让你的程序正常工作？实际上很简单，你只需要一个UIButton的子类并重写点击测试方法。</p>
<p><span id="more-936"></span></p>
<p>然而，首先你需要一个方法能确定图像上指定点是透明的。遗憾的是UIImage无法像Cocoa为NSImage提供的NSBitmapRepresentation 那样方便地访问位图数据。但是每个UIImage都具有一个称为CGImage的属性可以访问内部图像数据，Apple发布了一篇技术文章介绍了<a href="http://developer.apple.com/mac/library/qa/qa2007/qa1509.html">怎样通过CGImageRef访问内部位图数据</a>。</p>
<p><br class="spacer_" /></p>
<p>根据这篇文章的介绍，我们很容易就写出一个方法，它以CGPoint为参数，根据该点是否透明（0）与否返回YES或NO。</p>
<p><br class="spacer_" /></p>
<p><strong><em>UIImage-Alpha.h</em></strong></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span><br />
<br />
<span style="color: #a61390;">@interface</span> UIImage<span style="color: #002200;">&#40;</span>Alpha<span style="color: #002200;">&#41;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span style="color: #400080;">NSData</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ARGBData;<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>isPointTransparent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point;<br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p><br class="spacer_" /></p>
<p><em><strong>UIImage-Alpha.m</strong></em></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CGContextRef CreateARGBBitmapContext <span style="color: #002200;">&#40;</span>CGImageRef inImage<span style="color: #002200;">&#41;</span><br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; CGContextRef &nbsp; &nbsp;context <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;<br />
&nbsp; &nbsp; CGColorSpaceRef colorSpace;<br />
&nbsp; &nbsp; <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bitmapData;<br />
&nbsp; &nbsp; <span style="color: #a61390;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitmapByteCount;<br />
&nbsp; &nbsp; <span style="color: #a61390;">int</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitmapBytesPerRow;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">size_t</span> pixelsWide <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>inImage<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">size_t</span> pixelsHigh <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>inImage<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; bitmapBytesPerRow &nbsp; <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>pixelsWide <span style="color: #002200;">*</span> 4<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; bitmapByteCount &nbsp; &nbsp; <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>bitmapBytesPerRow <span style="color: #002200;">*</span> pixelsHigh<span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; colorSpace <span style="color: #002200;">=</span> CGColorSpaceCreateDeviceRGB<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>colorSpace <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;<br />
<br />
&nbsp; &nbsp; bitmapData <span style="color: #002200;">=</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/malloc.html"><span style="color: #a61390;">malloc</span></a><span style="color: #002200;">&#40;</span> bitmapByteCount <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>bitmapData <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;  CGColorSpaceRelease<span style="color: #002200;">&#40;</span> colorSpace <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;  <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<br />
&nbsp; &nbsp; context <span style="color: #002200;">=</span> CGBitmapContextCreate <span style="color: #002200;">&#40;</span>bitmapData,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pixelsWide,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  pixelsHigh,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  8,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  bitmapBytesPerRow,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  colorSpace,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  kCGImageAlphaPremultipliedFirst<span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>context <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;  <a href="http://www.opengroup.org/onlinepubs/009695399/functions/free.html"><span style="color: #a61390;">free</span></a> <span style="color: #002200;">&#40;</span>bitmapData<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;  <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fprintf.html"><span style="color: #a61390;">fprintf</span></a> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">stderr</span>, <span style="color: #bf1d1a;">&quot;Context not created!&quot;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<br />
&nbsp; &nbsp; CGColorSpaceRelease<span style="color: #002200;">&#40;</span> colorSpace <span style="color: #002200;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> context;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@implementation</span> UIImage<span style="color: #002200;">&#40;</span>Alpha<span style="color: #002200;">&#41;</span><br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span style="color: #400080;">NSData</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ARGBData<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; CGContextRef cgctx <span style="color: #002200;">=</span> CreateARGBBitmapContext<span style="color: #002200;">&#40;</span>self.CGImage<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cgctx <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">size_t</span> w <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>self.CGImage<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">size_t</span> h <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>self.CGImage<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGRect rect <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span><span style="color: #002200;">&#123;</span>0,0<span style="color: #002200;">&#125;</span>,<span style="color: #002200;">&#123;</span>w,h<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#125;</span>;<br />
&nbsp; &nbsp; CGContextDrawImage<span style="color: #002200;">&#40;</span>cgctx, rect, self.CGImage<span style="color: #002200;">&#41;</span>; <br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> CGBitmapContextGetData <span style="color: #002200;">&#40;</span>cgctx<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; CGContextRelease<span style="color: #002200;">&#40;</span>cgctx<span style="color: #002200;">&#41;</span>; &nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>data<span style="color: #002200;">&#41;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">size_t</span> dataSize <span style="color: #002200;">=</span> 4 <span style="color: #002200;">*</span> w <span style="color: #002200;">*</span> h; <span style="color: #11740a; font-style: italic;">// ARGB = 4 8-bit components</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span style="color: #400080;">NSData</span></a> dataWithBytes<span style="color: #002200;">:</span>data length<span style="color: #002200;">:</span>dataSize<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span> &nbsp; &nbsp;<br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>isPointTransparent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span style="color: #400080;">NSData</span></a> <span style="color: #002200;">*</span>rawData <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self ARGBData<span style="color: #002200;">&#93;</span>; &nbsp;<span style="color: #11740a; font-style: italic;">// See about caching this</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>rawData <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;  <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">size_t</span> bpp <span style="color: #002200;">=</span> <span style="color: #2400d9;">4</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">size_t</span> bpr <span style="color: #002200;">=</span> self.size.width <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span>;<br />
<br />
&nbsp; &nbsp; NSUInteger index <span style="color: #002200;">=</span> point.x <span style="color: #002200;">*</span> bpp <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>point.y <span style="color: #002200;">*</span> bpr<span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>rawDataBytes <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">char</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>rawData bytes<span style="color: #002200;">&#93;</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> rawDataBytes<span style="color: #002200;">&#91;</span>index<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span>;<br />
<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p>一旦我们有能力确定图像中的某点是否透明，我们就可以编写UIButton的子类，重写hitTest:withEvent: 方法。它将返回一个UIView的实例。如果该点在此视图或其子视图中未被点击，那么将返回nil。如果点击在其子视图，那么将返回点击中的子视图，如果点击中视图，那么返回视图本身。</p>
<p><br class="spacer_" /></p>
<p>然而，我们可以进行一些简化，这是因为尽管UIButton继承了UIView，技术上可能具有子视图，但这非常的少见，而且Interface Builder并不支持这样做。所以在本文的实现中并不考虑子视图。</p>
<p><br class="spacer_" /></p>
<p><em><strong>IrregularShapedButton.h</strong></em></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span><br />
<br />
<span style="color: #a61390;">@interface</span> IrregularShapedButton <span style="color: #002200;">:</span> UIButton <span style="color: #002200;">&#123;</span><br />
<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<p><em><strong>IrregularShapedButton.m</strong></em></p>
<div class="codecolorer-container objc mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6e371a;">#import &quot;IrregularShapedButton.h&quot;</span><br />
<span style="color: #6e371a;">#import &quot;UIImage-Alpha.h&quot;</span><br />
<br />
<span style="color: #a61390;">@implementation</span> IrregularShapedButton<br />
<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>hitTest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point 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<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;<br />
&nbsp; &nbsp; <span style="color: #a61390;">else</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UIImage <span style="color: #002200;">*</span>displayedImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self imageForState<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self state<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>displayedImage <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// No image found, try for background image</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; displayedImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self backgroundImageForState<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self state<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>displayedImage <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">// No image could be found, fall back to</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> self; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">BOOL</span> isTransparent <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>displayedImage isPointTransparent<span style="color: #002200;">:</span>point<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>isTransparent<span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #a61390;">return</span> self;<br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #a61390;">@end</span></div></td></tr></tbody></table></div>
<pre>将Interface Builder中的四个图像按钮改为IrregularShapedButton，它们将正常工作了。
</pre>
<h5>原文见：<a title="http://iphonedevelopment.blogspot.com/2010/03/irregularly-shaped-uibuttons.html" href="http://iphonedevelopment.blogspot.com/2010/03/irregularly-shaped-uibuttons.html">Irregularly Shaped UIButton</a></h5>
]]></content:encoded>
			<wfw:commentRss>http://www.iphone-geek.cn/%e7%bc%96%e7%a8%8b/%e4%b8%8d%e8%a7%84%e5%88%99%e5%bd%a2%e7%8a%b6%e7%9a%84uibutton/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

