`
473687880
  • 浏览: 485055 次
文章分类
社区版块
存档分类
最新评论

iphone开发中的一些小技巧

 
阅读更多
1、如果在程序中想对某张图片进行处理的话(得到某张图片的一部分)可一用以下代码:

UIImage *image = [UIImageimageNamed:filename];

CGImageRef imageRef = image.CGImage;

CGRect rect =CGRectMake(origin.x, origin.y,size.width, size.height);

CGImageRef imageRefRect =CGImageCreateWithImageIn<wbr>Rect</wbr>(imageRef, rect);

UIImage *imageRect = [[UIImagealloc]initWithCGImage:imageRefRect];


2、判断设备是iphone还是iphone4的代码:


#define isRetina ([UIScreen instancesRespondToSelect<wbr>or:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(<span style="word-wrap:normal; word-break:normal; color:rgb(65,0,224)">640</span>,<span style="word-wrap:normal; word-break:normal; color:rgb(65,0,224)">960</span>), [[UIScreen mainScreen] currentMode].size) : NO)</wbr>


3、判断邮箱输入的是否正确:


- (BOOL) validateEmail: (NSString*) candidate {

NSString*emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";<wbr></wbr>

NSPredicate*emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", emailRegex];<wbr></wbr>

<wbr></wbr>

return[emailTestevaluateWithObject:candidate];

}


4、如何把当前的视图作为照片保存到相册中去:

<wbr></wbr>

#import<QuartzCore/QuartzCore.h>

UIGraphicsBeginImageCont<wbr>ext(currentView.<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(123,28,175)">bounds</span>.<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(123,28,175)">size</span>); <wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,140,0)">//currentView</span><span style="word-wrap:normal; word-break:normal; font-family:'Heiti sC Light'; color:rgb(0,140,0)">当前的</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,140,0)">view</span></wbr></wbr></wbr>

[currentView.layerrenderInContext:UIGraphicsGetCurrentCont<wbr>ext<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">()];</span></wbr>

UIImage *viewImage =UIGraphicsGetImageFromCu<wbr>rrentImageContext<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">();</span></wbr>

UIGraphicsEndImageContex<wbr>t();</wbr>

UIImageWriteToSavedPhoto<wbr>sAlbum(viewImage,<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(200,0,167)">nil</span>,<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(200,0,167)">nil</span>,<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(200,0,167)">nil</span>);</wbr>


5、本地通知(类似于push通知)按home键到后台 十秒后触发:

UILocalNotification *notification=[[UILocalNotificationalloc]init];<wbr></wbr>

if(notification!=nil) {<wbr></wbr>

NSLog(@">> support local notification");<wbr></wbr>

NSDate*now=[NSDatenew];<wbr></wbr>

notification.fireDate=[nowaddTimeInterval:10];<wbr></wbr>

notification.timeZone=[NSTimeZonedefaultTimeZone];<wbr></wbr>

notification.alertBody=@"该去吃晚饭了!";<wbr></wbr>

[[UIApplicationsharedApplication].scheduleLocalNotificatio<wbr>n<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">:notification];</span></wbr>

}


6、捕获iphone通话事件:

CTCallCenter *center = [[CTCallCenteralloc]init];

center.callEventHandler = ^(CTCall *call)<wbr></wbr>

{

NSLog(@"call:%@",call.callState);

}


7、iOS 4引入了多任务支持,所以用户按下“Home”键以后程序可能并没有退出而是转入了后台运行。如果您想让应用直接退出,最简单的方法是:在infoplist里面找到Application does not run in background一项,勾选即可。


8、使UIimageView的图像旋转:


floatrotateAngle =M_PI;

CGAffineTransform transform =CGAffineTransformMakeRot<wbr>ation</wbr>(rotateAngle);

imageView.transform = transform;


9、设置旋转的原点:

<wbr></wbr>

#import<wbr></wbr><QuartzCore/QuartzCore.h>

UIImageView *imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"bg.png"]];

imageView.layer.anchorPoint =CGPointMake(0.5,1.0);


10、实现自定义的状态栏(遮盖状态栏):

CGRect frame = {{0,0}, {320,20}};

UIWindow* wd = [[UIWindowalloc]initWithFrame:frame];

[wdsetBackgroundColor:[UIColorclearColor]];

[wdsetWindowLevel:UIWindowLevelStatusBar];

frame =CGRectMake(100,0,30,20);

UIImageView* img = [[UIImageViewalloc]initWithFrame:frame];

[imgsetContentMode:UIViewContentModeCenter];

[imgsetImage:[UIImageimageNamed:@"00_0103.png"]];

[wdaddSubview:img];

[wdmakeKeyAndVisible];


[UIViewbeginAnimations:nilcontext:nil];

[UIViewsetAnimationDuration:2];

frame.origin.x +=150;

[imgsetFrame:frame];

[UIViewcommitAnimations];


11、在程序中实现电话的拨打:


//添加电话图标按钮<wbr></wbr>

UIButton *btnPhone = [[UIButtonbuttonWithType:UIButtonTypeCustom]retain];<wbr></wbr>

btnPhone.frame =CGRectMake(280,10,30,30);<wbr></wbr>

UIImage *image = [UIImageimageNamed:@"phone.png"];<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>

[btnPhonesetBackgroundImage:imageforState:UIControlStateNormal];<wbr></wbr>


//点击拨号按钮直接拨号<wbr></wbr>

[btnPhoneaddTarget:selfaction:@selector(callAction:event:)forControlEvents:UIControlEventTouchUpIns<wbr>ide<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">];<wbr></wbr></span></wbr>


[cell.contentViewaddSubview:btnPhone];<wbr><wbr></wbr></wbr>//cell是一个UITableViewCell<wbr></wbr>


//定义点击拨号按钮时的操作<wbr></wbr>

- (void)callAction:(id)sender event:(id)event{<wbr></wbr>

NSSet*touches = [eventallTouches];<wbr></wbr>

UITouch*touch = [touchesanyObject];<wbr></wbr>

CGPointcurrentTouchPosition = [touchlocationInView:self.listTable];<wbr></wbr>

NSIndexPath*indexPath = [self.listTableindexPathForRowAtPoint: currentTouchPosition];<wbr></wbr>

if(indexPath ==nil) {<wbr></wbr>

return;<wbr></wbr>

}<wbr></wbr>

NSIntegersection = [indexPathsection];<wbr></wbr>

NSUIntegerrow = [indexPathrow];<wbr></wbr>

NSDictionary*rowData = [datasobjectAtIndex:row];<wbr></wbr>

<wbr></wbr>

NSString*num = [[NSStringalloc]initWithFormat:@"tel://%@",number];//number为号码字符串<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:num]];//拨号<wbr></wbr>

}


12、更改iphone的键盘颜色:


1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPadUIKeyboardTypePhonePad

2.keyboardAppearanceUIKeyboardAppearanceAler<wbr>t<wbr></wbr></wbr>

- (void)textViewDidBeginEditing:(UITextView*)textView{

NSArray*ws = [[UIApplicationsharedApplication]windows];

for(UIView*winws){

NSArray*vs = [wsubviews];

for(UIView*vinvs)

{

if([[NSStringstringWithUTF8String:object_getClassName(v)]isEqualToString:@"UIKeyboard"])

{

v.backgroundColor= [UIColorredColor];

}

}

}


13、设置时区


<wbr></wbr>

NSTimeZone*defaultTimeZone = [NSTimeZonedefaultTimeZone];

NSTimeZone*tzGMT = [NSTimeZonetimeZoneWithName:@"GMT"];

[NSTimeZonesetDefaultTimeZone:tzGMT];

上面两个时区任意用一个。


14、Ipad隐藏键盘的同时触发方法。

<wbr></wbr>

<wbr></wbr>

[[NSNotificationCenterdefaultCenter]addObserver:self

selector:@selector(keyboardWillHide:)

name:UIKeyboardWillHideNotifi<wbr>cation</wbr>

<wbr><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(71,0,134)">object</span>:<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(200,0,167)">nil</span>];</wbr>


- (IBAction)keyboardWillHide:(NSNotification*)note


14、在一个程序中打开另一个程序的方法。

<wbr></wbr>

http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html

15、计算字符串的字数

-(int)calculateTextNumber:(NSString*)text

{

floatnumber =0.0;

intindex =0;

for(index; index < [textlength]; index++)

{

NSString*protoText = [textsubstringToIndex:[textlength] - index];

NSString*toChangetext = [textsubstringToIndex:[textlength] -1-index];

NSString*charater;

if([toChangetextlength]==0)

{

charater = protoText;

}

else<wbr></wbr>

{

NSRangerange = [textrangeOfString:toChangetext];

charater = [protoTextstringByReplacingCharact<wbr>ersInRange<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">:range</span>withString<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">:</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(204,45,36)">@""</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">];</span></wbr>

<wbr></wbr>

}

NSLog(charater);

if([charaterlengthOfBytesUsingEncodi<wbr>ng<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">:</span>NSUTF8StringEncoding<span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">] ==</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(47,48,211)">3</span><span style="word-wrap:normal; word-break:normal; line-height:16px; color:rgb(0,0,0)">)</span></wbr>

{

number++;

}

else<wbr></wbr>

{

number = number+0.5;

}

}

returnceil(number);

}

分享到:
评论

相关推荐

    iphone初学者开发小技巧

    iphone初学者开发的小技巧,自己搜集整理,简洁实用

    iphone开发小技巧

    iPhone 将图片缩小成指定大小.txt iPhone 设置导航栏标题背景图片.txt iPhone关于调用摄像头代码.txt iPhone开发不得不知的几件事.txt UIKit 控件背景贴图方法.txt

    《iPhone开发实战》.(Christopher Allen).pdf

     本书适合所有 iphone开发人员学习参考。... 目录 第一部分 iphone编程简介. 第1章 iphone简介2 1.1 iphone核心规范3 1.1.1 iphone的输入及输出规范3 1.1.2 iphone网络规范4 1.1.3 iphone浏览器规范5...

    一些Android的初级技巧说明

    对于初学者的一些开发的小技巧,来自于网上的资料。 ImageView的加边框的源码 屏幕的横屏纵屏的控制 TextView的滚动技巧 ListView的详解 希望对大家有所帮助。 最近在研究定制ImageView,实现iPhone的惯性滑动...

    android开发资料大全

    Android 小項目之---Iphone拖动图片特效 (附源码) 一个完整的新浪微博客户端android版OAuth认证示例 超爽的android抽屉效果 65个Android实例教程汇总 基本控件及基本动画效果dem 2011android面试题目及其答案大全...

    iOS开发之topLayoutGuide和bottomLayoutGuide的使用小技巧分享

    前言 LayoutGuide这个概念在本人从事iOS开发过程中一直是比较陌生...在IOS开发的过程中我们经常会遇到一些紧贴tabbar有工具条之类的页面,比如说购买、支付等页面,往往这些页面有时候在栈底显示(页面有tabbar),有时

    iOS 6应用开发实战 开发用图

    本书有两大特点:第一,技术新颖,基于最新iOS 6技术撰写,系统讲解开发iPhone和iPad应用所需掌握的基础技术和高级技巧,以及其流程和方法;第二,易于阅读,从认知学角度进行内容规划,一个案例贯穿全书,不仅能从...

    安卓软件开发--毕业设计开题报告安卓软件文献翻译.docx

    通过培训能够具有Android智能手机软件开发的设计思想,形成规范的代码编写习惯,掌握Android开发中各种功能点的实现方法和开发技巧。全面认识和了解Android系统的框架,掌握Android上进行应用程序开发的能力,熟悉...

    用Eclipse开发iPhoneWeb应用程序

    了解如何开发iPhone的Javadoc查看器,用户界面设计的技巧,以及iPhone应用程序开发的前景。Apple的iPhone平台为开发人员提供了一个有趣的机会。借助其小型的交互式触摸屏,iPhone和iPodTouch在很短的时间内迅速吸引...

    iOS 6应用开发实战 代码2

    本书有两大特点:第一,技术新颖,基于最新iOS 6技术撰写,系统讲解开发iPhone和iPad应用所需掌握的基础技术和高级技巧,以及其流程和方法;第二,易于阅读,从认知学角度进行内容规划,一个案例贯穿全书,不仅能从...

    iOS6 应用开发实战代码 1

    本书有两大特点:第一,技术新颖,基于最新iOS 6技术撰写,系统讲解开发iPhone和iPad应用所需掌握的基础技术和高级技巧,以及其流程和方法;第二,易于阅读,从认知学角度进行内容规划,一个案例贯穿全书,不仅能从...

    iOS图片拉伸小技巧

    华丽外观的背后,少不了美工的辛苦设计,但如果开发人员不懂得怎么合理展示这些设计好的图片,将会糟蹋了这些设计,功亏一篑。 比如下面张图片,本来是设计来做按钮背景的: button.png,尺寸为:24×60 现在我们...

    Google Android SDK开发范例大全(完整版)

    余志龙,来自于手机制造业、电视媒体业、网络、电信产业、软件开发等领域,擅长嵌入式系统软件设计、J2ME游戏开发、Android开发,以及J2EE、JSP、Servlet、JavaBeans、PHP、C#等程序语言,熟悉面向对象技术与Eclipse...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    4.10 心爱小宝贝相片集——Gallery与衍生BaseAdapter容器 4.11 快速的搜索手机文件引擎——JavaI/O的应用 4.12 按钮也能随点击变换——ImageButton选择特效 4.13 具自动提示功能的菜单——AutoCompleteTextView与...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    4.10 心爱小宝贝相片集——Gallery与衍生BaseAdapter容器 4.11 快速的搜索手机文件引擎——JavaI/O的应用 4.12 按钮也能随点击变换——ImageButton选择特效 4.13 具自动提示功能的菜单——AutoCompleteTextView与...

    30条android项目开发技巧与经验总结

    如果是展示型文本(例如按钮中的文本),其大小受到限制的,textSize可以使用dp。 2、使用json用作网络数据传输时,应该使用String字段取代int字段。 3、按照现在正常密度比(系统的densityDPI根据分辨率和屏幕尺寸...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    4.10 心爱小宝贝相片集——Gallery与衍生BaseAdapter容器 4.11 快速的搜索手机文件引擎——JavaI/O的应用 4.12 按钮也能随点击变换——ImageButton选择特效 4.13 具自动提示功能的菜单——AutoCompleteTextView与...

Global site tag (gtag.js) - Google Analytics