`
isiqi
  • 浏览: 16362806 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

iphone开发中的一些小技巧

阅读更多

经过半年多的iphone开发,我发现在开发过程中最难的就是一些嘈杂的细节,而了解一些小技巧就会达到事半功倍的效果,下面我就总结一下在iphone开发中的一些小技巧。

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

UIImage *image = [UIImageimageNamed:filename];

CGImageRef imageRef = image.CGImage;

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

CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);

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

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


#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640,960), [[UIScreen mainScreen] currentMode].size) : NO)


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


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

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

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

return[emailTestevaluateWithObject:candidate];

}

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

#import<QuartzCore/QuartzCore.h>

UIGraphicsBeginImageContext(currentView.bounds.size); //currentView当前的view

[currentView.layerrenderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage,nil,nil,nil);

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

UILocalNotification *notification=[[UILocalNotificationalloc]init];

if(notification!=nil) {

NSLog(@">> support local notification");

NSDate*now=[NSDatenew];

notification.fireDate=[nowaddTimeInterval:10];

notification.timeZone=[NSTimeZonedefaultTimeZone];

notification.alertBody=@"该去吃晚饭了!";

[[UIApplicationsharedApplication].scheduleLocalNotification:notification];

}

6、捕获iphone通话事件:

CTCallCenter *center = [[CTCallCenteralloc]init];

center.callEventHandler = ^(CTCall *call)

{

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

}

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


8、使UIimageView的图像旋转:


floatrotateAngle =M_PI;

CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);

imageView.transform = transform;

9、设置旋转的原点:

#import<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、在程序中实现电话的拨打:


//添加电话图标按钮

UIButton *btnPhone = [[UIButtonbuttonWithType:UIButtonTypeCustom]retain];

btnPhone.frame =CGRectMake(280,10,30,30);

UIImage *image = [UIImageimageNamed:@"phone.png"];

[btnPhonesetBackgroundImage:imageforState:UIControlStateNormal];

//点击拨号按钮直接拨号

[btnPhoneaddTarget:selfaction:@selector(callAction:event:)forControlEvents:UIControlEventTouchUpInside];

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

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

- (void)callAction:(id)sender event:(id)event{

NSSet*touches = [eventallTouches];

UITouch*touch = [touchesanyObject];

CGPointcurrentTouchPosition = [touchlocationInView:self.listTable];

NSIndexPath*indexPath = [self.listTableindexPathForRowAtPoint: currentTouchPosition];

if(indexPath ==nil) {

return;

}

NSIntegersection = [indexPathsection];

NSUIntegerrow = [indexPathrow];

NSDictionary*rowData = [datasobjectAtIndex:row];

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

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:num]];//拨号

}


12、更改iphone的键盘颜色:


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

2.keyboardAppearanceUIKeyboardAppearanceAlert

- (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、设置时区


NSTimeZone*defaultTimeZone = [NSTimeZonedefaultTimeZone];

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

[NSTimeZonesetDefaultTimeZone:tzGMT];

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


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

[[NSNotificationCenterdefaultCenter]addObserver:self

selector:@selector(keyboardWillHide:)

name:UIKeyboardWillHideNotification

object:nil];

- (IBAction)keyboardWillHide:(NSNotification*)note

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

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

{

NSRangerange = [textrangeOfString:toChangetext];

charater = [protoTextstringByReplacingCharactersInRange:rangewithString:@""];

}

NSLog(charater);

if([charaterlengthOfBytesUsingEncoding:NSUTF8StringEncoding] ==3)

{

number++;

}

else

{

number = number+0.5;

}

}

returnceil(number);

}

分享到:
评论

相关推荐

    iphone初学者开发小技巧

    【iPhone初学者开发小技巧】 在iOS开发领域,特别是对于初学者来说,掌握一些实用的技巧可以大大提高开发效率,减少不必要的麻烦。以下是一些在iPhone应用开发中可能会遇到的常见问题及其解决办法: 1. **截取...

    iphone开发小技巧

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

    iphone iOS开发中一些实用的Demo.zip

    "iPhone iOS开发中一些实用的Demo.zip"这个压缩包文件显然包含了多个示例代码,旨在帮助开发者理解并应用到实际项目中。这里我们将深入探讨其中可能涵盖的一些关键知识点。 1. **Swift编程语言**:作为苹果官方推荐...

    iphone开发技巧UI篇之自定义TabBar

    本篇文章将深入探讨如何在iPhone开发中实现自定义TabBar,以提升应用的用户体验和视觉效果。 首先,我们要了解TabBar的基本概念。TabBar是iOS SDK中的UITabBarController类的一部分,它提供了一个底部栏,通常包含...

    Cocos2D-iPhone开发教程

    以下是对Cocos2D-iPhone开发的一些关键知识点的详细说明: 1. **Cocos2D基础**: - **场景(Scenes)**: Cocos2D中的场景是游戏或应用的基本构造单元,可以看作是游戏的不同阶段或屏幕。 - **层(Layers)**: 层...

    精编Iphone开发教程

    【精编iPhone开发教程】 本教程全面覆盖了iPhone应用开发的核心知识,旨在帮助开发者从零开始掌握iOS平台的开发技能。教程深入浅出,既包含了基础的编程概念,也涉及了高级的技术应用,确保读者能够逐步成长为一名...

    iPhone开发入门

    【iPhone开发入门】这篇文章主要介绍了如何入门iPhone应用程序的开发,从不同角度逐步深入,适合对iOS开发感兴趣的初学者。以下是对各个章节内容的详细解读: 1. **程序员眼中的iPhone**:这部分可能介绍iPhone作为...

    iPhone开发秘籍 Coverflow

    《iPhone开发秘籍:Coverflow》是一本专为iOS开发者准备的指南,主要聚焦于Coverflow这一独特的用户界面效果的实现。Coverflow是苹果公司在其iOS操作系统中引入的一种视觉效果,它模仿了翻阅专辑封面或图片的效果,...

    iphone开发 星星 评分控件

    在本篇文章中,我们将深入探讨如何在iPhone应用中实现星星评分控件,以及它在开发中的应用。 首先,我们要了解iOS开发的主要语言Objective-C或Swift。虽然标题没有明确指出是使用哪种语言,但这里我们将会涵盖两种...

    iphone开发基础教程源代码1

    在本“iPhone开发基础教程源代码1”中,我们涵盖了多个关键知识点,这些知识点对于初学者和有经验的iOS开发者来说都是至关重要的。这个压缩包包含了一系列与iPhone应用开发相关的项目,通过这些项目的源代码,我们...

    iPhone开发基础教程_(美)Dave_Mark_中文高清版

    ### iPhone开发基础教程知识点概述 #### 一、书籍基本信息 - **书名**:《iPhone开发基础教程》 - **作者**:Dave Mark & Jeff LaMarche - **出版社**:人民邮电出版社 - **版本**:中文高清版 - **文件大小**:...

    iPhone开发书籍大全].Beginning.iPhone.SDK.Programming.with.Objective-C

    - 初学者或有一定编程基础但对iPhone开发不熟悉的程序员。 - 希望通过系统学习掌握iPhone应用程序开发技能的开发者。 #### 二、主要内容概述 ##### 第一部分:入门 - **第1章**:了解iPhone编程的基础知识 - ...

    iphone开发基础教程(含源码)

    综上所述,《iPhone开发基础教程》不仅覆盖了iPhone应用开发的基础知识,还深入探讨了一些高级话题,对于希望从事iOS开发的初学者来说是一份非常宝贵的学习资源。通过系统学习这些知识点,可以帮助开发者快速掌握iOS...

    iphone游戏开发实例

    在本主题中,我们将深入探讨“iPhone游戏开发实例”,包括几个关键项目,如测试反应小游戏、计数器、计算器、篝火动画以及打转块游戏的实现。这些实例是iOS应用开发的重要组成部分,特别是对于那些希望进入或已经在...

    iPhone开发秘籍第七章Media

    通过学习"iPhone开发秘籍第七章Media"的内容,开发者不仅可以掌握媒体处理的基本技巧,还能深入理解如何在实际项目中灵活运用这些知识,打造高质量的iOS应用程序。不论是音乐应用、社交媒体应用还是教育应用,对...

Global site tag (gtag.js) - Google Analytics