iOS开发小技巧及小知识点

1、Category(类别)

什么是Category

1、Category可以在不获悉不改变原代码的情况下向已有的类中添加方法,从而达到扩展已有类的目的,但是只能添加方法,不建议删除和修改(会导致bug)。

2、无法向Category中添加实例变量,Category通常作为一种组织框架代码的工具来使用。

3、如果Category和原始类中的方法名称冲突,则Category将覆盖原始类的方法,因为Category具有更高的优先级。

Category的作用:

1、将类的实现分散到多个不同文件或不同框架中。

2、创建对私有方法的前向引用。

3、向对象添加非正式协议。

2、关于日期(NSDate)的几个常用方法

NSDateFormatter *FM = [[NSDateFormatter alloc] init];

FM.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSDate *date1 = [FM dateFromString:@"2016-12-20 00:00:00"];

NSDate *date2 = [FM dateFromString:@"2016-12-21 00:00:00"];

//date1与当前时间的差(单位:秒)

NSTimeInterval second1 = [date1 timeIntervalSinceNow];

//date1与1970-1-1 08:00:00时间的差(单位:秒)

NSTimeInterval second2 = [date1 timeIntervalSince1970];

//date1与2001-1-1 08:00:00时间的差(单位:秒)

NSTimeInterval second3 = [date1 timeIntervalSinceReferenceDate];

//date1与date2的差(单位:秒)

NSTimeInterval second4 = [date1 timeIntervalSinceDate:date2];

//返回比较早的那个时间

NSDate *earlyDate = [date1 earlierDate:date2];

//返回比较晚的那个时间

NSDate *laterDate = [date1 laterDate:date2];

//判断两个时间是否相等

BOOL isEqual = [date1 isEqualToDate:date2];

//返回当前时间10秒后的时间

NSDate *date01 = [NSDate dateWithTimeIntervalSinceNow:10];

//返回1970-1-1 08:00:00时间10秒后的时间

NSDate *date02 = [NSDate dateWithTimeIntervalSince1970:10];

//返回2001-1-1 08:00:00时间10秒后的时间

NSDate *date03 = [NSDate dateWithTimeIntervalSinceReferenceDate:10];

//返回date2时间10秒后的时间

NSDate *date04 = [NSDate dateWithTimeInterval:10sinceDate:date2];

//随机返回一个比较遥远的未来时间

NSDate *date05 = [NSDate distantFuture];

//随机返回一个比较遥远的过去时间

NSDate *date06 = [NSDate distantPast];

3、图片拉伸

//先对图片设置拉伸程度

UIImage *image = [UIImage imageNamed:@"pic.png"];

image = [image stretchableImageWithLeftCapWidth:10topCapHeight:5];

//将图片显示出来

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,100,100,50)];

imageView.image = image;

[self.view addSubview:imageView];

1- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是左边不拉伸区域的宽度,第二个是上面不拉伸的高度。

注意:可拉伸的范围都是距离leftCapWidth后的1竖排像素,和距离topCapHeight后的1横排像素,而图像后面的剩余像素也不会被拉伸。

比如参数指定10,5。那么,图片左边10个像素,上边5个像素。不会被拉伸,x坐标为11和一个像素会被横向复制,y坐标为6的一个像素会被纵向复制。

4、APP跳转/跳转至系统APP

跳转前设置:

1、在将要跳转到的APP中TARGETS--info--URL Types中添加URL Schemes;

2、在本APP的info.plist中添加一个LSApplicationQueriesSchemes数组字段,把对方的APP的URL Schemes添加进去

//跳转到系统app

url = @"tel://1234567"//拨打电话

url = @"sms://1234567"//发短信

url = @"http://www.baidu.com"//Safari

url = @"mailto://admin@abt.com"//邮箱

url = @"maps://"//地图

url = @"facetime://1234567"//FaceTime

//跳转到系统设置

url = UIApplicationOpenSettingsURLString//适用于iOS >= 8

url = @"Prefs:root=General"//适用于iOS < 10

//跳转第三方APP

url = @"要跳转的app的URL Schemes"

//开始跳转

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) {

if(IOS_10) {

[[UIApplication sharedApplication] openURL:URL options:@{} completionHandler:nil];

}else{

[[UIApplication sharedApplication] openURL:URL];

}

}

5、UIView中的坐标转换

// 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;

// 将像素point从view中转换到当前视图中,返回在当前视图中的像素值

- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;

// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect

- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;

// 将rect从view中转换到当前视图中,返回在当前视图中的rect

- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;

例把UITableViewCell中的subview(btn)的frame转换到 controllerA中

// controllerA 中有一个UITableView, UITableView里有多行UITableVieCell,cell上放有一个button

// 在controllerA中实现:

CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];

CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];

// 此rc为btn在controllerA中的rect

或当已知btn时:

CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];

CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];

6、修改Xcode代码自动生成版权信息Copyright ? 2017年 XXX. All rights reserved

如图,就是修改这个

​修改方法:打开.xcodeproj工程文件,显示包含内容,会看到一个project.pbxproj文件,打开此文件修改 ORGANIZATIONNAME = "xxx";

7、iOS让button的文字局左或局右对齐

我们首先想到的方法是这个

1button.titleLabel.textAlignment = NSTextAlignmentLeft;

突然发现这是无效的,下面正确的设置方式:

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//局左

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;//局右

8、去除searchBar的灰色背景

近日,在做搜索框UISearchBar的时候,把searchBar放在导航栏的titleView上,当进入下一页然后再返回本页的时候searchBar的灰色背景会闪一下,那么怎么去除这个灰色背景呢?

调用如下方法即可去除灰色背景,哈哈。。。

- (void)removeSearchBarGrayBackColor

{

for(inti =0; i < self.searchBar.subviews.count; i++) {

UIView *backView = self.searchBar.subviews[i];

if([backView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {

[backView removeFromSuperview];

[self.searchBar setBackgroundColor:[UIColor clearColor]];

break;

}else{

NSArray * arr = self.searchBar.subviews[i].subviews;

for(intj =0; j < arr.count; j++) {

UIView *barView = arr[i];

if([barView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {

[barView removeFromSuperview];

[self.searchBar setBackgroundColor:[UIColor clearColor]];

break;

}

}

}

}

}

9、修改UITabbar顶部分割线颜色

//根据颜色生成一个图片

CGRect rect = CGRectMake(0,0, SCREEN_WIDTH,0.3);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, LineColor.CGColor);

CGContextFillRect(context, rect);

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//开始修改UITabbar顶部分割线颜色

[self.tabBar setShadowImage:img];

[self.tabBar setBackgroundImage:[Tools imageWithColor:WHITECOLOR]];

10、压缩图片

创建一个UIImage的类

@interfaceUIImage (Scale)

//压缩图片

- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage;

@end

@implementation UIImage (Scale)

- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage

{

CGFloat maxWidth =640;

if(sourceImage.size.width < maxWidth) {

returnsourceImage;

}

CGFloat btWidth =0.0f;

CGFloat btHeight =0.0f;

if(sourceImage.size.width > sourceImage.size.height) {

btHeight = maxWidth;

btWidth = sourceImage.size.width * (maxWidth / sourceImage.size.height);

}else{

btWidth = maxWidth;

btHeight = sourceImage.size.height * (maxWidth / sourceImage.size.width);

}

CGSize targetSize = CGSizeMake(btWidth, btHeight);

return[self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];

}

- (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize

{

UIImage *newImage = nil;

CGSize imageSize = sourceImage.size;

CGFloat width = imageSize.width;

CGFloat height = imageSize.height;

CGFloat targetWidth = targetSize.width;

CGFloat targetHeight = targetSize.height;

CGFloat scaleFactor =0.0;

CGFloat scaledWidth = targetWidth;

CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if(CGSizeEqualToSize(imageSize, targetSize) == NO) {

CGFloat widthFactor = targetWidth / width;

CGFloat heightFactor = targetHeight / height;

if(widthFactor > heightFactor)

scaleFactor = widthFactor;// scale to fit height

else

scaleFactor = heightFactor;// scale to fit width

scaledWidth  = width * scaleFactor;

scaledHeight = height * scaleFactor;

// center the image

if(widthFactor > heightFactor) {

thumbnailPoint.y = (targetHeight - scaledHeight) *0.5;

}elseif(widthFactor < heightFactor) {

thumbnailPoint.x = (targetWidth - scaledWidth) *0.5;

}

}

UIGraphicsBeginImageContext(targetSize);

CGRect thumbnailRect = CGRectZero;

thumbnailRect.origin = thumbnailPoint;

thumbnailRect.size.width  = scaledWidth;

thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();

if(newImage == nil) NSLog(@"could not scale image");

UIGraphicsEndImageContext();

returnnewImage;

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容