<转>iphone开发中的一些小技巧
1、如果在程序中想对某张图片进行处理的话(得到某张图片的一部分)可一用以下代码:
1 UIImage *image = [UIImage imageNamed:filename]; 2 CGImageRef imageRef = 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:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
3、判断邮箱输入的是否正确:
1 - (BOOL) validateEmail: (NSString *) candidate { 2 3 NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}"; 4 5 NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 6 7 8 9 return [emailTest evaluateWithObject:candidate]; 10 11 }
4、如何把当前的视图作为照片保存到相册中去:
1 #import <QuartzCore/QuartzCore.h> 2 3 UIGraphicsBeginImageContext(currentView.bounds.size); //currentView 当前的view 4 5 [currentView.layer renderInContext:UIGraphicsGetCurrentContext()]; 6 7 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 8 9 UIGraphicsEndImageContext(); 10 11 UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
5、本地通知(类似于push通知)按home键到后台 十秒后触发:
1 UILocalNotification *notification=[[UILocalNotification alloc] init]; 2 3 if (notification!=nil) { 4 5 NSLog(@">> support local notification"); 6 7 NSDate *now=[NSDate new]; 8 9 notification.fireDate=[now addTimeInterval:10]; 10 11 notification.timeZone=[NSTimeZone defaultTimeZone]; 12 13 notification.alertBody=@"该去吃晚饭了!"; 14 15 [[UIApplication sharedApplication].scheduleLocalNotification:notification]; 16 17 }
6、捕获iphone通话事件:
1 CTCallCenter *center = [[CTCallCenter alloc] init]; 2 3 center.callEventHandler = ^(CTCall *call) 4 5 { 6 7 NSLog(@"call:%@", call.callState); 8 9 }
7、iOS 4 引入了多任务支持,所以用户按下 “Home” 键以后程序可能并没有退出而是转入了后台运行。如果您想让应用直接退出,最简单的方法是:在 info-plist 里面找到 Application does not run in background 一项,勾选即可。
8、使UIimageView的图像旋转:
1 float rotateAngle = M_PI; 2 3 CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle); 4 5 imageView.transform = transform;
9、设置旋转的原点:
1 #import <QuartzCore/QuartzCore.h> 2 3 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]]; 4 5 imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);
10、实现自定义的状态栏(遮盖状态栏):
1 CGRect frame = {{0, 0}, {320, 20}}; 2 3 UIWindow* wd = [[UIWindow alloc] initWithFrame:frame]; 4 5 [wd setBackgroundColor:[UIColor clearColor]]; 6 7 [wd setWindowLevel:UIWindowLevelStatusBar]; 8 9 frame = CGRectMake(100, 0, 30, 20); 10 11 UIImageView* img = [[UIImageView alloc] initWithFrame:frame]; 12 13 [img setContentMode:UIViewContentModeCenter]; 14 15 [img setImage:[UIImage imageNamed:@"00_0103.png"]]; 16 17 [wd addSubview:img]; 18 19 [wd makeKeyAndVisible]; 20 21 22 23 [UIView beginAnimations:nil context:nil]; 24 25 [UIView setAnimationDuration:2]; 26 27 frame.origin.x += 150; 28 29 [img setFrame:frame]; 30 31 [UIView commitAnimations];
11、在程序中实现电话的拨打:
//添加电话图标按钮
1 UIButton *btnPhone = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 2 3 btnPhone.frame = CGRectMake(280,10,30,30); 4 5 UIImage *image = [UIImage imageNamed:@"phone.png"]; 6 7 [btnPhone setBackgroundImage:image forState:UIControlStateNormal];
//点击拨号按钮直接拨号
1 [btnPhone addTarget:self action:@selector(callAction:event:)forControlEvents:UIControlEventTouchUpInside]; 2 3 4 5 [cell.contentView addSubview:btnPhone]; //cell是一个UITableViewCell
//定义点击拨号按钮时的操作
1 - (void)callAction:(id)sender event:(id)event{ 2 3 NSSet *touches = [event allTouches]; 4 5 UITouch *touch = [touches anyObject]; 6 7 CGPoint currentTouchPosition = [touch locationInView:self.listTable]; 8 9 NSIndexPath *indexPath = [self.listTable indexPathForRowAtPoint: currentTouchPosition]; 10 11 if (indexPath == nil) { 12 13 return; 14 15 } 16 17 NSInteger section = [indexPath section]; 18 19 NSUInteger row = [indexPath row]; 20 21 NSDictionary *rowData = [datas objectAtIndex:row]; 22 23 24 25 NSString *num = [[NSString alloc] initWithFormat:@"tel://%@",number]; //number为号码字符串 26 27 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号 28 29 }
12、更改iphone的键盘颜色:
1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPad,UIKeyboardTypePhonePad
2. keyboardAppearance = UIKeyboardAppearanceAlert
1 - (void)textViewDidBeginEditing:(UITextView *)textView{ 2 3 NSArray *ws = [[UIApplication sharedApplication] windows]; 4 5 for(UIView *w in ws){ 6 7 NSArray *vs = [w subviews]; 8 9 for(UIView *v in vs) 10 11 { 12 13 if([[NSString stringWithUTF8String:object_getClassName(v)] isEqualToString:@"UIKeyboard"]) 14 15 { 16 17 v.backgroundColor = [UIColor redColor]; 18 19 } 20 21 } 22 23 }
13、设置时区
1 NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone]; 2 3 NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"GMT"]; 4 5 [NSTimeZone setDefaultTimeZone:tzGMT];
上面两个时区任意用一个。
14、Ipad隐藏键盘的同时触发方法。
1 [[NSNotificationCenter defaultCenter] addObserver:self 2 3 selector:@selector(keyboardWillHide:) 4 5 name:UIKeyboardWillHideNotification 6 7 object:nil]; 8 9 10 11 - (IBAction)keyboardWillHide:(NSNotification *)note
14、在一个程序中打开另一个程序的方法。
http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html
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 8 NSString *protoText = [text substringToIndex:[text length] - index]; 9 10 NSString *toChangetext = [text substringToIndex:[text length] -1 -index]; 11 12 NSString *charater; 13 14 if ([toChangetext length]==0) 15 { 16 charater = protoText; 17 } 18 else 19 { 20 NSRange range = [text rangeOfString:toChangetext]; 21 charater = [protoText stringByReplacingCharactersInRange:range withString:@""]; 22 } 23 24 NSLog(charater); 25 26 if ([charater lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3) 27 { 28 number++; 29 } 30 else 31 { 32 number = number+0.5; 33 } 34 35 } 36 37 return ceil(number); 38 39 }

浙公网安备 33010602011771号