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

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

1. UIImage *image = [UIImage imageNamed:filename];

2. CGImageRef imageimageRef = image.CGImage;

3.

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

5.

6. CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);

7.

8. UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];

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

1. #define isRetina ([UIScreen instancesRespondToSelector:

2. @selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960),

3. [[UIScreen mainScreen] currentMode].size) : NO)

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

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

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

3. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

4. return [emailTest evaluateWithObject:candidate];

5. }

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

1. #import <QuartzCore/QuartzCore.h>

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

3. [currentView.layer renderInContext:UIGraphicsGetCurrentContext()];

4. UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

5. UIGraphicsEndImageContext();

6. UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

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

1. UILocalNotification *notification=[[UILocalNotification alloc] init];

2. if (notification!=nil) {

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

4. NSDate *now=[NSDate new];

5. notification.fireDate=[now addTimeInterval:10];

6. notification.timeZone=[NSTimeZone defaultTimeZone];

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

8. [[UIApplication sharedApplication].scheduleLocalNotification:notification];

9. }

6、捕获iphone通话事件:

1. CTCallCenter *center = [[CTCallCenter alloc] init];

2. center.callEventHandler = ^(CTCall *call)

3. {

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

5. }

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

8、使UIimageView的图像旋转:

1. float rotateAngle = M_PI;

2. CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);

3. imageView.transform = transform;

9、设置旋转的原点:

1. #import <QuartzCore/QuartzCore.h>

2. UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];

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

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

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

2. UIWindow* wd = [[UIWindow alloc] initWithFrame:frame];

3. [wd setBackgroundColor:[UIColor clearColor]];

4. [wd setWindowLevel:UIWindowLevelStatusBar];

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

6. UIImageView* img = [[UIImageView alloc] initWithFrame:frame];

7. [img setContentMode:UIViewContentModeCenter];

8. [img setImage:[UIImage imageNamed:@"00_0103.png"]];

9. [wd addSubview:img];

10.[wd makeKeyAndVisible];

11.[UIView beginAnimations:nil context:nil];

12.[UIView setAnimationDuration:2];

13.frame.origin.x += 150;

14.[img setFrame:frame];

15.[UIView commitAnimations];

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

1. //添加电话图标按钮

2. UIButton *btnPhone = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

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

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

5. [btnPhone setBackgroundImage:image forState:UIControlStateNormal];

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

7. [btnPhone addTarget:self action:@selector(callAction:event:) forControlEvents:UIControlEventTouchUpInside];

8. [cell.contentView addSubview:btnPhone];  //cell是一个UITableViewCell

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

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

11.NSSet *touches = [event allTouches];

12.UITouch *touch = [touches anyObject];

13.CGPoint currentTouchPosition = [touch locationInView:self.listTable];

14.NSIndexPath *indexPath = [self.listTable indexPathForRowAtPoint: currentTouchPosition];

15.if (indexPath == nil) {

16.return;

17.}

18.NSInteger section = [indexPath section];

19.NSUInteger row = [indexPath row];

20.NSDictionary *rowData = [datas objectAtIndex:row];

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

22.[[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号

23.}

12、更改iphone的键盘颜色:

1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPad,UIKeyboardTypePhonePad

2. keyboardAppearance =UIKeyboardAppearanceAlert

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

2. NSArray *ws = [[UIApplication sharedApplication] windows];

3. for(UIView *w in ws){

4. NSArray *vs = [w subviews];

5. for(UIView *v in vs)

6. {

7. if([[NSString stringWithUTF8String:object_getClassName(v)] isEqualToString:@"UIKeyboard"])

8. {

9. v.backgroundColor = [UIColor redColor];

10.}

11.}

12.}

13、设置时区

1. NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone];

2. NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"GMT"];

3. [NSTimeZone setDefaultTimeZone:tzGMT];

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

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

1. [[NSNotificationCenter defaultCenter] addObserver:self

2. selector:@selector(keyboardWillHide:)

3. name:UIKeyboardWillHideNotification

4.   object:nil];

5. - (IBAction)keyboardWillHide:(NSNotification *)note

15、计算字符串的字数

1. -(int)calculateTextNumber:(NSString *)text

2. {

3. float number = 0.0;

4. int index = 0;

5. for (index; index < [text length]; index++)

6. {

7. NSString *protoText = [text substringToIndex:[text length] - index];

8. NSString *toChangetext = [text substringToIndex:[text length] -1 -index];

9. NSString *charater;

10.if ([toChangetext length]==0)

11.{

12.charater = protoText;

13.}

14.else

15.{

16.NSRange range = [text rangeOfString:toChangetext];

17.charater = [protoText stringByReplacingCharactersInRange:range withString:@""];

18.}

19.NSLog(charater);

20.if ([charater lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3)

21.{

22.number++;

23.}

24.else

25.{

26.numbernumber = number+0.5;

27.}

28.}

29.return ceil(number);

30.}

iPhone开发中一些使用小技巧相关推荐

  1. iPhone开发中的一些小技巧

    NavBar+TarBar iphone开发 NavBar+TarBar 1  改变NavBar颜色:选中Navigation Bar 的Tint属性.选中颜色. 2  隐藏"back&qu ...

  2. iphone开发中的一些小技

    在iPhone开发过程中,一些小技巧对开发工作有很大的帮助作用,所以以下内容收集了一些开发过程中的小技巧. 1.如果在程序中想对某张图片进行处理的话(得到某张图片的一部分)可一用以下代码:   UII ...

  3. 我开发中总结的小技巧

    经过一段时间的react开发,自己总结了一些小习惯,自动构建webpack 1. 组件命名文件夹 一个组件一个文件夹,可读性高,结构清晰 文件夹:Danamic(组件首字母大写)index.jsx s ...

  4. [as部落首发]网页游戏开发中的一些小技巧

    分享一:swf文件的自动化版本管理 如何在图片更新后,玩家能立即看到最新的图片呢?按照技术中心的规定,需要在文件地址后面加入版本号,格式为"__ver=版本号",这个版本号如何进行 ...

  5. JS开发中常用的小技巧

    1.获取指定范围内的随机数 1 2 3 function getRadomNum(min,max){     return  Math.floor(Math.random() * (max - min ...

  6. Cocos2d-JS开发中的一些小技巧(持续更新)

    1.获取URL中的请求参数的值----此方法接收参数名 1 2 3 4 5 6 function getQueryString(name) {     var reg = new RegExp(&qu ...

  7. iPhone开发中的技巧整理(四)

    iphone开发笔记 退回输入键盘 - (BOOL) textFieldShouldReturn:(id)textField{ [textField  resignFirstResponder]; } ...

  8. web开发常用js功能性小技巧(转)

    web开发常用js功能性小技巧 -------------------------------------------------------------------------------- < ...

  9. 学习iPhone开发中 sqlite3的使用

    由于我主要负责我们小组项目数据库模块的部分所以这几天都一直在研究在iphone中最为常用的一个简单数据库sqlite,自己也搜集很多资料,因此在这里总结一下这几天的学习成果: 1.Sqlite操作简明 ...

最新文章

  1. php实现人工神经网络算法,BP人工神经网络实现
  2. 集群节点Elasticsearch升级
  3. 使用jsp+swfupload做上传时报Upload Error: 405错误
  4. pycharm更改为系统python3_今天配置好python3 装好了pycharm 就新建一个django一直改不好...
  5. .NET Core 2.1 容器镜像将从 Docker Hub 中删除
  6. Redis 混合存储最佳实践指南
  7. nginx静态文件服务器_Linux分享文件?快速创建静态文件服务器
  8. Silverlight 游戏开发小技巧:实现街霸4的选人界面
  9. pycharm运行pytorch版pix2pix学习笔记
  10. 【雷达通信】《现代雷达系统分析与设计》大作业【含Matlab源码 285期】
  11. java hsqldb数据库,HSQLDB数据库的使用
  12. Java基础知识笔记
  13. 【安卓】Android Studio 3.5的引用包错误
  14. esp32 鸿蒙,ESP8266最佳开发板--ESP-LAUNCHER开发板评测
  15. Millionaire Dream
  16. 简单理解2台主机数据通信过程
  17. [转]sed命令详解
  18. 手机kakao聊天能自动翻译 WhatsApp翻译 实时翻译
  19. 求一组数的组合的和为一个值的所有情况
  20. opencv 脸部识别_实时面部识别如何与OpenCV一起使用?

热门文章

  1. 数据结构——中国邮路问题
  2. 吴忠军 - 项羽30万大军挖不动秦始皇陵!秦陵难解之谜
  3. K均值聚类和DBSCAN介绍
  4. 【密码学】破解维吉尼亚密码(C++代码实现)
  5. 怎么查看访问网站服务器记录,如何快速分析网站服务器日志
  6. K8S在centos7虚拟机上部署集群
  7. c语言猴子摘桃思路,递归函数--猴子摘桃问题的解题思路
  8. opencv SVM
  9. Maven私有仓库上传及使用
  10. 如何对待工作中的失误?我的一点心得