iphone开发常用代码段
iphone开发常用代码段
1、如果在程序中想对某张图片迚行处理的话(得到某张图片的一部分)可一用以下代码:
UIImage *image = [UIImage imageNamed:filename]; CGImageRef imageRef = image.CGImage;
CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);
CGImageRef imageRefRect
= CGImageCreateWithImageInRect(imageRef, rect); UIImage *imageRect =
[[UIImage alloc] 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 =
[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:candidate];
}
4、如何把当前的视图作为照片保存到相册中去:
iphone开发常用代码段
#import <QuartzCore/QuartzCore.h> UIGraphicsBeginImageContext(currentView.bounds.size); // currentView 当前的view
[yer renderInContext:UIGraphicsGetCurrentCont ext()];
UIImage *viewImage
= UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
5、本地通知(类似于push通知)按home键到后台十秒后触发:
UILocalNotification
*notification=[[UILocalNotification alloc] init];
if (notification!=nil) {
NSLog(@">> support local notification");
NSDate *now=[NSDate new];
notification.fireDate=[now addTimeInterval:10]; notification.timeZone=[NSTimeZone defaultTimeZone]; notification.alertBody=@"该去吃晚饭了!";
[[UIApplication sharedApplication].scheduleLocalNotification:no tification];
}
6、捕获iphone通话事件:
CTCallCenter *center = [[CTCallCenter alloc] init];
center.callEventHandler = ^(CTCall *call)
{
NSLog(@"call:%@", call.callState);
}
iphone开发常用代码段
7、iOS 4 引入了多任务支持,所以用户按下“Home”键以后程序可能并没有退出而是转入了后台运行。如果您想让应用直接退出,最简单的方法是:在 info-plist 里面找到 Application does not run in background 一项,勾选即可。
8、使UIimageView的图像旋转:
float rotateAngle = M_PI;
CGAffineTransform transform
=CGAffineTransformMakeRotation(rotateAngle); imageView.transform = transform;
9、设置旋转的原点:
#import <QuartzCore/QuartzCore.h> UIImageView *imageView =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@ "bg.png"]];
yer.anchorPoint = CGPointMake(0.5, 1.0);
10、实现自定义的状态栏(遮盖状态栏):
CGRect frame = {{0, 0}, {320, 20}};
UIWindow* wd = [[UIWindow alloc] initWithFrame:frame]; [wd setBackgroundColor:[UIColor clearColor]];
[wd setWindowLevel:UIWindowLevelStatusBar];
frame = CGRectMake(100, 0, 30, 20);
UIImageView* img =
[[UIImageView alloc] initWithFrame:frame];
[img setContentMode:UIViewContentModeCenter];
[img setImage:[UIImage imageNamed:@"00_0103.png"]]; [wd addSubview:img];
iphone开发常用代码段
[wd makeKeyAndVisible];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
frame.origin.x += 150;
[img setFrame:frame];
[UIView commitAnimations];
11、在程序中实现电话的拨打:
//添加电话图标按钮
UIButton *btnPhone =
[[UIButton buttonWithType:UIButtonTypeCustom] retain]; btnPhone.frame = CGRectMake(280,10,30,30);
UIImage *image = [UIImage imageNamed:@"phone.png"]; [btnPhone setBackgroundImage:image forState:UIControlState Normal];
//点击拨号按钮直接拨号
[btnPhone addTarget:self action:@selector(callAction:event:)fo rControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnPhone]; //cell是一个UITableViewCell
//定义点击拨号按钮时的操作
- (void)callAction:(id)sender event:(id)event{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition =
[touch locationInView:self.listTable];
NSIndexPath *indexPath =
iphone开发常用代码段
[self.listTable indexPathForRowAtPoint: currentTouchPosition]; if (indexPath == nil) {
return;
}
NSInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSDictionary *rowData = [datas objectAtIndex:row]; NSString *num =
[[NSString alloc] initWithFormat:@"tel://%@",number]; //num ber为号码字符串
[[UIApplication sharedApplication] openURL:[NSURL URLWithSt ring:num]]; //拨号
}
12、更改iphone的键盘颜色:
1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPad,UIKeyboardTypePhonePad
2. keyboardAppearance = UIKeyboardAppearanceAlert
- (void)textViewDidBeginEditing:(UITextView *)textView{ NSArray *ws = [[UIApplication sharedApplication] windows]; for(UIView *w in ws){
NSArray *vs = [w subviews];
for(UIView *v in vs)
{
if([[NSString stringWithUTF8String:object_getClassName(v)] is EqualToString:@"UIKeyboard"])
{
v.backgroundColor = [UIColor redColor];
}
}
iphone开发常用代码段
}
13、设置时区
NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone];
NSTimeZone *tzGMT =
[NSTimeZone timeZoneWithName:@"GMT"]; [NSTimeZone setDefaultTimeZone:tzGMT];
上面两个时区任意用一个。
14、Ipad隐藏键盘的同时触发方法。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
- (IBAction)keyboardWillHide:( …… 此处隐藏:4444字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [高等教育]一年级家长课程教案
- [高等教育]封丘县人民医院深入推进纠正医药购销领
- [高等教育]2017年6月大学英语四级真题试卷及答案(
- [高等教育]2017年北京第二外国语学院文学院824中
- [高等教育]7 高中历史第7单元1861年俄国农奴制改
- [高等教育]【K12学习】4、实际测量-苏教版六年级
- [高等教育]药具培训试卷题库及部分参考答案
- [高等教育]本土电子元器件目录分销商如何赢得生意
- [高等教育]七年级岭南版美术教案
- [高等教育]书作文之书法活动通讯稿
- [高等教育]Endnote X 软件使用入门和用法总结(LS)
- [高等教育]嵌入式系统的现状及发展状况
- [高等教育]2012抗菌药物专项整治活动方案解读
- [高等教育]人教版新课本一年级数学下册期末试卷
- [高等教育]爱课程民法学观后感
- [高等教育]930机组使用说明书1
- [高等教育]煤气设备设施点检标准
- [高等教育]常见室内观叶植物图解
- [高等教育]312党员群众路线心得体会
- [高等教育]小学信息(苗版)第一册全册教案
- 在市---局2010党建大会上的讲话
- 《科哲》提纲及补充阅读材料(2010.7)
- 苏州高博软件技术职业学院论文开题报告
- 兼职导游管理的困境及对策探讨
- 基于通用设计理念的现代厨房产品语义研
- 康乐一中2010年至2011年度鼓号队、花束
- 第10章_数据收集整理与描述_期末复习课
- 2008年黑龙江林甸商贸购物中心营销策划
- 水硬度的测定实验报告
- 五分钟教你拍摄夜景光绘照
- 2014年临床妇产科三基三严试题及答案
- 0第二课 纾解压力第一站了解压力
- 解析建筑工程电气设备安装施工技术要点
- 地方性应用型本科高校“双师型”师资队
- 高考语文专题复习课件:小说阅读指导
- 装饰工程投标书2
- 大学生就业难问题探讨及对策
- English and Its History
- 青岛市城市房屋修缮工程质量监督管理办
- 初中英语形容词和副词的用法和练习题




