教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 高等教育 >

iphone开发常用代码段

来源:网络收集 时间:2026-01-24
导读: iphone开发常用代码段 1、如果在程序中想对某张图片迚行处理的话(得到某张图片的一部分)可一用以下代码: UIImage *image = [UIImage imageNamed:filename]; CGImageRef imageRef = image.CGImage; CGRect rect = CGRectMake(origin.x, origin.y ,size.wid

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字,全部文档内容请下载后查看。喜欢就下载吧 ……

iphone开发常用代码段.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/127652.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)