经过半年多的iphone开发,我发现在开发过程中最难的就是一些嘈杂的细节,而了解一些小技巧就会达到事半功倍的效果,下面我就总结一下在iphone开发中的一些小技巧。
CGImageRef imageRef = image.CGImage;
CGRect rect =CGRectMake(origin.x, origin.y,size.width, size.height);
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *imageRect = [[UIImagealloc]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 = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", emailRegex];
return[emailTestevaluateWithObject:candidate];
}
4、如何把当前的视图作为照片保存到相册中去:
#import<QuartzCore/QuartzCore.h>
UIGraphicsBeginImageContext(currentView.bounds.size); //currentView当前的view
[currentView.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage,nil,nil,nil);
5、本地通知(类似于push通知)按home键到后台 十秒后触发:
UILocalNotification *notification=[[UILocalNotificationalloc]init];
if(notification!=nil) {
NSLog(@">> support local notification");
NSDate*now=[NSDatenew];
notification.fireDate=[nowaddTimeInterval:10];
notification.timeZone=[NSTimeZonedefaultTimeZone];
notification.alertBody=@"该去吃晚饭了!";
[[UIApplicationsharedApplication].scheduleLocalNotification:notification];
}
6、捕获iphone通话事件:
CTCallCenter *center = [[CTCallCenteralloc]init];
center.callEventHandler = ^(CTCall *call)
{
NSLog(@"call:%@",call.callState);
}
7、iOS 4引入了多任务支持,所以用户按下“Home”键以后程序可能并没有退出而是转入了后台运行。如果您想让应用直接退出,最简单的方法是:在info-plist里面找到Application does not run in background一项,勾选即可。
8、使UIimageView的图像旋转:
floatrotateAngle =M_PI;
CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);
imageView.transform = transform;
9、设置旋转的原点:
#import<QuartzCore/QuartzCore.h>
UIImageView *imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"bg.png"]];
imageView.layer.anchorPoint =CGPointMake(0.5,1.0);
10、实现自定义的状态栏(遮盖状态栏):
CGRect frame = {{0,0}, {320,20}};
UIWindow* wd = [[UIWindowalloc]initWithFrame:frame];
[wdsetBackgroundColor:[UIColorclearColor]];
[wdsetWindowLevel:UIWindowLevelStatusBar];
frame =CGRectMake(100,0,30,20);
UIImageView* img = [[UIImageViewalloc]initWithFrame:frame];
[imgsetContentMode:UIViewContentModeCenter];
[imgsetImage:[UIImageimageNamed:@"00_0103.png"]];
[wdaddSubview:img];
[wdmakeKeyAndVisible];
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:2];
frame.origin.x +=150;
[imgsetFrame:frame];
[UIViewcommitAnimations];
11、在程序中实现电话的拨打:
//添加电话图标按钮
UIButton *btnPhone = [[UIButtonbuttonWithType:UIButtonTypeCustom]retain];
btnPhone.frame =CGRectMake(280,10,30,30);
UIImage *image = [UIImageimageNamed:@"phone.png"];
[btnPhonesetBackgroundImage:imageforState:UIControlStateNormal];
//点击拨号按钮直接拨号
[btnPhoneaddTarget:selfaction:@selector(callAction:event:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentViewaddSubview:btnPhone];//cell是一个UITableViewCell
//定义点击拨号按钮时的操作
- (void)callAction:(id)sender event:(id)event{
NSSet*touches = [eventallTouches];
UITouch*touch = [touchesanyObject];
CGPointcurrentTouchPosition = [touchlocationInView:self.listTable];
NSIndexPath*indexPath = [self.listTableindexPathForRowAtPoint: currentTouchPosition];
if(indexPath ==nil) {
return;
}
NSIntegersection = [indexPathsection];
NSUIntegerrow = [indexPathrow];
NSDictionary*rowData = [datasobjectAtIndex:row];
NSString*num = [[NSStringalloc]initWithFormat:@"tel://%@",number];//number为号码字符串
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:num]];//拨号
}
12、更改iphone的键盘颜色:
1.只有这2种数字键盘才有效果。UIKeyboardTypeNumberPad,UIKeyboardTypePhonePad
2.keyboardAppearance=UIKeyboardAppearanceAlert
- (void)textViewDidBeginEditing:(UITextView*)textView{
NSArray*ws = [[UIApplicationsharedApplication]windows];
for(UIView*winws){
NSArray*vs = [wsubviews];
for(UIView*vinvs)
{
if([[NSStringstringWithUTF8String:object_getClassName(v)]isEqualToString:@"UIKeyboard"])
{
v.backgroundColor= [UIColorredColor];
}
}
}
13、设置时区
NSTimeZone*defaultTimeZone = [NSTimeZonedefaultTimeZone];
NSTimeZone*tzGMT = [NSTimeZonetimeZoneWithName:@"GMT"];
[NSTimeZonesetDefaultTimeZone:tzGMT];
上面两个时区任意用一个。
14、Ipad隐藏键盘的同时触发方法。
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
- (IBAction)keyboardWillHide:(NSNotification*)note
14、在一个程序中打开另一个程序的方法。
http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html
15、计算字符串的字数
-(int)calculateTextNumber:(NSString*)text
{
floatnumber =0.0;
intindex =0;
for(index; index < [textlength]; index++)
{
NSString*protoText = [textsubstringToIndex:[textlength] - index];
NSString*toChangetext = [textsubstringToIndex:[textlength] -1-index];
NSString*charater;
if([toChangetextlength]==0)
{
charater = protoText;
}
else
{
NSRangerange = [textrangeOfString:toChangetext];
charater = [protoTextstringByReplacingCharactersInRange:rangewithString:@""];
}
NSLog(charater);
if([charaterlengthOfBytesUsingEncoding:NSUTF8StringEncoding] ==3)
{
number++;
}
else
{
number = number+0.5;
}
}
returnceil(number);
}
本文分享了在iPhone开发过程中的一些实用技巧,包括图片处理、设备类型判断、邮箱验证、视图截图保存、本地通知设置等,旨在帮助开发者提高开发效率。
821

被折叠的 条评论
为什么被折叠?



