博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【iOS】iOS开发中常用的几个功能代码
阅读量:5913 次
发布时间:2019-06-19

本文共 5449 字,大约阅读时间需要 18 分钟。

hot3.png

博客分类: 
 

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、如何把当前的视图作为照片保存到相册中去:
#import <QuartzCore/QuartzCore.h>
UIGraphicsBeginImageContext(currentView.bounds.size);     //currentView 当前的view
[currentView.layer renderInContext:UIGraphicsGetCurrentContext()];
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:notification];
}
6、捕获iphone通话事件:
CTCallCenter *center = [[CTCallCenter alloc] init];
center.callEventHandler = ^(CTCall *call) 
{
NSLog(@"call:%@", call.callState);
}
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"]];
imageView.layer.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];
[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:UIControlStateNormal]; 
//点击拨号按钮直接拨号 
[btnPhone addTarget:self action:@selector(callAction:event:)forControlEvents: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 = [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]; //number为号码字符串     
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: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)] isEqualToString:@"UIKeyboard"])
{
v.backgroundColor = [UIColor redColor];
}
}
}
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:(NSNotification *)note
14、在一个程序中打开另一个程序的方法。
15、计算字符串的字数
-(int)calculateTextNumber:(NSString *)text
{
float number = 0.0;
int index = 0;
for (index; index < [text length]; index++)
{
NSString *protoText = [text substringToIndex:[text length] - index];
NSString *toChangetext = [text substringToIndex:[text length] -1 -index];
NSString *charater;
if ([toChangetext length]==0)
{
charater = protoText;
}
else 
{
NSRange range = [text rangeOfString:toChangetext];
charater = [protoText stringByReplacingCharactersInRange:range withString:@""];
}
NSLog(charater);
if ([charater lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3)
{
number++;
}
else 
{
number = number+0.5;
}
}
return ceil(number);
}

转载于:https://my.oschina.net/meilidashijie/blog/99408

你可能感兴趣的文章
NBU备份DB2
查看>>
java final
查看>>
Java基础学习第四天
查看>>
Linux 系统目录结构
查看>>
备份MySQL数据库的命令
查看>>
Mycat 学习笔记
查看>>
关于Qt中的对话框无缘无故弹出两次的问题
查看>>
Transform组件C#游戏开发快速入门
查看>>
网站建设中关于并发连接数的解释
查看>>
破解加密PDF文件pdfcrack
查看>>
Windows 2008 服务器核心(Server Core)
查看>>
多线程的深入学习:单生产单消费,单生产多消费,多生产多消费,守护线程,线程优先级,join和yield,线程内部匿名类...
查看>>
在64位CentOS6.2安装64位oracle11g步骤
查看>>
Servlet过滤器实现网站访问计数器功能
查看>>
两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同?(异或的用法)...
查看>>
Python学习日记---字典
查看>>
工资低的.Net程序员,活该你工资低
查看>>
FastDFS专题】fastdfs使用实战(概念篇)
查看>>
连接MongoDB时报错:由于目标计算机的积极拒绝,无法连接
查看>>
Linux下安装PHP
查看>>