iphone 开发技巧总结
1、如果在程序中想对某张图片进行处理的话(得到某张图片的一部分)可一用以下代码:
CGImageRef imageRef =image.CGImage;
CGRect rect=
CGImageRef imageRefRect=
UIImage *imageRect = [[UIImagealloc]
2、判断设备是iphone还是iphone4的代码:
#define isRetina([UIScreen instancesRespondToSelect
3、判断邮箱输入的是否正确:
- (BOOL)validateEmail: (NSString
NSString*emailRegex=
NSPredicate
return
}
4、如何把当前的视图作为照片保存到相册中去:
#import
UIGraphicsBeginImageCont
[currentView.layer
UIImage *viewImage=
UIGraphicsEndImageContex
UIImageWriteToSavedPhoto
5、本地通知(类似于push通知)按home键到后台十秒后触发:
UILocalNotification*notification=[[UILocalNotificationalloc]
if
NSLog(@">>support local notification");
NSDate
notification.fireDate=[now
notification.timeZone=[NSTimeZonedefaultTimeZone];
notification.alertBody=@"该去吃晚饭了!";
[[UIApplication
}
6、捕获iphone通话事件:
CTCallCenter *center =[[CTCallCenter
center.callEventHandler = ^(CTCall*call)
{
NSLog(@"call:%@",
}
7、iOS4
8、使UIimageView的图像旋转:
float
CGAffineTransform transform=CGAffineTransformMakeRot
imageView.transform =transform;
9、设置旋转的原点:
#import
UIImageView *imageView =[[UIImageViewalloc]
imageView.layer.anchorPoint=
10、实现自定义的状态栏(遮盖状态栏):
CGRect frame = {{0,
UIWindow* wd = [[UIWindowalloc]
[wd
[wdsetWindowLevel:UIWindowLevelStatusBar];
frame=
UIImageView* img = [[UIImageViewalloc]
[img
[img
[wd
[wd
[UIView
[UIView
frame.origin.x+=
[img
[UIView
11、在程序中实现电话的拨打:
//添加电话图标按钮
UIButton *btnPhone =[[UIButton
btnPhone.frame=
UIImage *image = [UIImageimageNamed:@"phone.png"];
[btnPhone
//点击拨号按钮直接拨号
[btnPhone
[cell.contentViewaddSubview:btnPhone];
//定义点击拨号按钮时的操作
- (void)callAction:(id)senderevent:(id)event{
NSSet
UITouch
CGPoint
NSIndexPath
if
return;
}
NSInteger
NSUInteger
NSDictionary
NSString
[[UIApplication
}
12、更改iphone的键盘颜色:
1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPad,UIKeyboardTypePhonePad
2.
- (void)textViewDidBeginEditing:(UITextView
NSArray*ws= [[UIApplication
for(UIView
NSArray
for(UIView
{
if([[NSString
{
v.backgroundColor
}
}
}
13、设置时区
NSTimeZone
NSTimeZone
[NSTimeZone
上面两个时区任意用一个。
14、Ipad隐藏键盘的同时触发方法。
[[NSNotificationCenter
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotifi
object:nil];
- (IBAction)keyboardWillHide:(NSNotification
14、在一个程序中打开另一个程序的方法。
http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html
15、计算字符串的字数
-(int)calculateTextNumber:(NSString
{
float
int
for
{
NSString
NSString
NSString
if
{
charater = protoText;
}
else
{
NSRange
charater =[protoText
}
NSLog(charater);
if([charater
{
number++;
}
else
{
number = number+0.5;
}
}
returnceil(number);
}