ios开发中的一些小技巧
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、在一个程序中打开另一个程序的方法。
http://www.cocoachina.com/iphonedev/sdk/2010/0322/768.html
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);
}
ios开发中的一些小技巧的更多相关文章
- iOS开发中的那些小技巧
前言:今天在写代码的过程中遇到一个需要修改系统navigationBar的背景色,我起初用的是barTintColor去修改但是防不住系统点击按钮的时候会有一个渲染高亮的效果,调了好久没有达到自己想要 ...
- RS开发中的一些小技巧[不定期更新]
从9月份一直忙到了现在,项目整体的改版工作也完成了十有八九了,有些事情只有你自己真正的做了,你才能明白:哦,原来还可以这个样子,这样做真的好了很多呢,接下来我就分享一些最近遇到的RS开发的一些小技巧, ...
- 日常开发中的shell小技巧
工具推荐 命令行中很方便的代码统计工具---cloc 强大的分屏工具---tmux 最舒服的markdown书写工具---typora markdown图床推荐--七牛云 模拟生成熵(避免暴力手搓键盘 ...
- Java开发中的一些小技巧
原文:http://www.cnblogs.com/xdp-gacl/p/3490276.html 一. Java获取URL地址中传递的参数 /** * 获取URL中的参数名和参数值的Map集合 * ...
- ios 开发 收起键盘的小技巧
在UIViewController中收起键盘,除了调用相应控件的resignFirstResponder方法外,还有另外三种方法: 1.重载UIViewController中的touchesBegin ...
- iOS开发 字符串的转化 小技巧
/字典或者数组转化为nsstring NSArray *arr = [NSArray arrayWithObject:@"1"]; NSString *str = [arr JSO ...
- JS开发中常用的小技巧
1.获取指定范围内的随机数 1 2 3 function getRadomNum(min,max){ return Math.floor(Math.random() * (max - min ...
- JS开发中的一些小技巧和方法
生成指定范围内的随机数 当我们需要获取指定范围(min,max)内的整数的时候,下面的代码非常适合:这段代码用的还挺多的. function setRadomNum(min,max){ return ...
- Cocos2d-JS开发中的一些小技巧
1.获取URL中的请求参数的值----此方法接收参数名 function getQueryString(name) { var reg = new RegExp("(^|&)&quo ...
随机推荐
- python之模块poplib之常见用法
# -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块poplib之常见用法 ''' 所以,收取邮件分两步: 第一步:用poplib把邮件的原始 ...
- Android自定义控件进阶-打造Android自定义的下拉列表框控件
技术:Android+java 概述 Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求, 比如有时候我们需要类似windows 或者web网页中常见的 ...
- 【转】 mysql反引号的使用(防冲突)
转载地址:http://blog.itechol.com/space.php?uid=33&do=blog&id=6681 1.mysql语句中 反引号 [`]作用: 避免表明.字段名 ...
- Windows下的包管理器Chocolatey的使用
1.配置安装路径(后续需要管理员权限执行命令) Create a machine level (user level will also work) environment variable name ...
- 【RS】Local Low-Rank Matrix Approximation - LLORMA :局部低秩矩阵近似
[论文标题]Local Low-Rank Matrix Approximation (icml_2013 ) [论文作者]Joonseok Lee,Seungyeon Kim,Guy Lebanon ...
- 【DeepLearning】Exercise:Sparse Autoencoder
Exercise:Sparse Autoencoder 习题的链接:Exercise:Sparse Autoencoder 注意点: 1.训练样本像素值需要归一化. 因为输出层的激活函数是logist ...
- Js里面IF(var)表示什么意思?js中if的写法、含义
Q:if(一个变量),没有> < !=这些半短逻辑, 什么时候为真 什么时候为假? A:是判断逻辑,javascript是弱变量类型语言,通俗地就是没有数据类型.任意值都有逻辑值(真或假) ...
- iphone 恢复出厂设置方法
1.下载安装并打开itunes. 2.让手机进入恢复模式: 一.先长按住电源键,出现关机选项时,请滑动关机: 二.随后再按电源键开机,屏幕会出现苹果标志,不要松开电源键: 三.接着再按住主屏 Home ...
- Win7 U盘安装Ubuntu16.04 双系统详细教程(方法一)
主要分为以下几步: 一. 下载Ubuntu 16.04镜像软件: 二. 制作U盘启动盘使用ultraISO: 三. 安装Ubuntu系统: 四. 用EasyBCD 创建启动系统启动引导: (根据个人情 ...
- ef SQL Server 版本不支持数据类型“datetime2”
我遇到这个问题是在用数据库模型的时候.当时我电脑上是sql2008 通过vs2010建立了一个ADO.net数据库模型,之后在项目上线的时候,临时把数据库换成了sql2005,在添加新闻的时候出现了“ ...