iphone开发小技巧汇总

100 篇文章 0 订阅

1.iphone之xx_Prefix.pch的作用和用法

       Prefix.pch:扩展名.pch表示"pre-compiled-header",这是一个你工程要用到的 来自于外部框架的头文件列表。xcode将编译这些头到文件,这将减少你在选择Build或Build and Go时编译项目的时间。通常用到的头文件已经自动包含了

   pch,系统编译每个.m文件前,都会先import这个文件。这样既节省了你手动添加import的时间,也有助于加速编译.

   还有就是可以在这里面放入宏,在整个工程中都可以用。节省了时间

2.iphone之发布版本的时候移除NSLog输出的方法

只需要将下列代码加入到pch文件中即可, __OPTIMIZE__这个编译选项是发布版本才有的,所以在编译调试版本的时候可以看到Log,而发布版本则没有Log。

#ifndef __OPTIMIZE__

   #define NSLog(...) NSLog(__VA_ARGS__)

#else

   #define NSLog(...) {}

#endif

3.iphone之取消icon的高光状态

系统会默认会在图标上 自动加上半透明的高光半圆,如果我们不想要这个效果或者图标本身已经包含了这个高光效果,我们可以在项目配置里把系统的高光功能取消掉:

xcode3.2x建的项目:

在info plist里加一个配置项,key为“Icon already includes gloss and bevel effects”, 类型为bool,然后打上钩就,这样系统就不会自动加高光;
 

xcode4建的项目:

在项目target的summary标签页下找到App Icons项,在“Prerendered”打上钩
 

 
此时在info.plist里会多出一个配置项" Icon already includes gloss effects":

 
再找到 “Icon files (iOS 5)”项目(如果有的话),展开, 把里面的“Icon already includes gloss effects”也设置成“YES”

这样程序中的高光效果就取消了。

4.iphone之unichar和初始化

在iphone/mac开发中,unichar是两字节长的char,代表unicode的一个字符。但在xcode中,初始化unichar是个问题。如果像下面这样声明,会有warning"Multi-character character constant"。

unichar a = '国';

这是因为C语言中两个单引号只能用于char。可以采用直接写文字编码的方式来初始化。

unichar a = 0x0100;

如果有很多个unichar怎么办?一个个去查表太麻烦了。可以采取变通的方法:

unichar a[10];

NSString *aString = @"一二三四五六七八九十";

for (int i = 0; i < 10; i++)

a[i] = [aString characterAtIndex:i];

5.iphone之转向appstore中商品

NSString *str =@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware? id=473898949";

[[UIApplication sharedApplication] openURL:[NSURLURLWithString:str]];

把"id=3410863403"换成id=xxxx(自己程序的ID),即可。

6.iphone之iphone控件尺寸



ElementSize (in points)
Window (including status bar)320 x 480 pts
Status Bar
(How to hide the status bar)
20 pts
View inside window (visible status bar)320 x 460
Navigation Bar44 pts
Nav Bar Image /Toolbar Imageup to 20 x 20 pts (transparent PNG)
Tab Bar49 pts
Tab Bar Iconup to 30 x 30 pts (transparent PNGs)
Text Field31 pts
Height of a view inside a navigation bar416 pts
Height of a view inside a tab bar411 pts
Height of a view inside a navbar and a tab bar367 pts
Portrait Keyboard height216 pts
Landscape Keyboard height140 pts

Points vs. Pixels

The iPhone 4 introduced a high resolution display with twice the pixels of previous iPhones. However you don't have to modify your  code to support high-res displays; the coordinate system goes by points rather than pixels, and the dimensions in points of the screen and all UI elements remain the same.
iOS 4 supports high resolution displays (like the iPhone 4 display) via the  scale property on UIScreen, UIView, UIImage, and CALayer classes. If the object is displaying high-res content, its scale property is set to 2.0. Otherwise it defaults to 1.0.
All you need to do to support high-res displays is to provide @2x versions of the images in your project. See the  checklist for updating to iOS4 or  Apple documentation for  Supporting High Resolution Screens for more info.

Adjusting Sizes

Click here to see how to adjust  View Frames and Bounds.

Additional References

7.iPhone之发送附件邮件代码

[cpp]  view plain copy
  1. MFMailComposeViewController *picker = [[MFMailComposeViewControlleralloc] init];  
  2. picker.mailComposeDelegate = self;  
  3.   
  4. [pickersetSubject:@"I have a pencil for you"];  
  5. NSString*databasePathFromApp = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"10.pdf"];  
  6. NSData*fileData = [NSDatadataWithContentsOfFile:databasePathFromApp];  
  7. [pickeraddAttachmentData:fileData mimeType:@"application/pdf"fileName:@"15.pdf"];  
  8. [pickeraddAttachmentData:fileData mimeType:@"application/pdf"fileName:@"16.pdf"];  
  9. UIImage*roboPic = [UIImage imageNamed:@"1.png"];  
  10. NSData*imageData = UIImageJPEGRepresentation(roboPic, 1);  
  11. [pickeraddAttachmentData:imageData mimeType:@"image/jpg"fileName:@"1.png"];  
  12.   
  13. NSString*emailBody = @"This is a cool image of a robot Ifound.  Check it out!";  
  14. [pickersetMessageBody:emailBody isHTML:YES];  
  15.   
  16. [selfpresentModalViewController:picker animated:YES];  
  17. [pickerrelease];  

8.iphone之自动休眠定时器

Phone OS试图省电的一个方法是使用自动休眠定时器。

如果在一定的时间内没有检测到触摸事件,系统最初会使屏幕变暗,并最终完全关闭屏幕。大多数开发者都应该让这个定时器打开,但是,游戏和不使用触摸输入的应用程序开发者可以禁用这个定时器,使屏幕在应用程序运行时不会变暗。

将共享的 UIApplication对象的 idleTimerDisabled属性设置为 YES,就可以禁用自动休眠定时器。

9.iphone之广告转向

[cpp]  view plain copy
  1. NSString *iTunesLink =@"http://itunes.apple.com/us/app/id(产口id号)?mt=8";  
  2.   
  3. [[UIApplication sharedApplication] openURL:[NSURLURLWithString:iTunesLink]];  

10.iphone之navigationItem 添加标题视图的方法

但是如果题目太长,后半部分就变成省略号了,那要实现自定义字体。代码如下:
[cpp]  view plain copy
  1. UILabel *titleText =[[UILabel alloc] initWithFrame: CGRectMake(0, 0,200, 20)];  
  2. titleText.backgroundColor =[UIColor clearColor];  
  3. [titleText setFont:[UIFontsystemFontOfSize:15.0]];  
  4. [titleText setText:@"设置navigationItem标题的字体大小"];  
  5. self.navigationItem.titleView=titleText;//titleView  
  6. [titleText release];  

11.iphone之开源类库工具

几个常用的开源类库及下载地址:
1. jsonjson编码解码
2. GTMBase64base64编码解码
3. TouchXMLxml解析
4. SFHFKeychainUtils安全保存用户密码到keychain中
5. MBProgressHUD很棒的一个加载等待特效框架
6. ASIHTTPRequesthttp等相关协议封装
7. EGORefreshTableHeaderView下拉刷新代码
8. AsyncImageView异步加载图片并缓存代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值