1伞矩、Category(類別)
什么是Category
1拄衰、Category可以在不獲悉不改變原代碼的情況下向已有的類中添加方法络拌,從而達(dá)到擴(kuò)展已有類的目的,但是只能添加方法擂送,不建議刪除和修改(會(huì)導(dǎo)致bug)悦荒。
2、無法向Category中添加實(shí)例變量嘹吨,Category通常作為一種組織框架代碼的工具來使用搬味。
3、如果Category和原始類中的方法名稱沖突,則Category將覆蓋原始類的方法碰纬,因?yàn)镃ategory具有更高的優(yōu)先級(jí)萍聊。
找資料可以加下ios開發(fā)學(xué)習(xí) :626433463
Category的作用:
1、將類的實(shí)現(xiàn)分散到多個(gè)不同文件或不同框架中悦析。
2寿桨、創(chuàng)建對(duì)私有方法的前向引用。
3强戴、向?qū)ο筇砑臃钦絽f(xié)議亭螟。
2、關(guān)于日期(NSDate)的幾個(gè)常用方法
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與當(dāng)前時(shí)間的差(單位:秒)
NSTimeInterval second1 = [date1 timeIntervalSinceNow];
//date1與1970-1-1 08:00:00時(shí)間的差(單位:秒)
NSTimeInterval second2 = [date1 timeIntervalSince1970];
//date1與2001-1-1 08:00:00時(shí)間的差(單位:秒)
NSTimeInterval second3 = [date1 timeIntervalSinceReferenceDate];
//date1與date2的差(單位:秒)
NSTimeInterval second4 = [date1 timeIntervalSinceDate:date2];
//返回比較早的那個(gè)時(shí)間
NSDate *earlyDate = [date1 earlierDate:date2];
//返回比較晚的那個(gè)時(shí)間
NSDate *laterDate = [date1 laterDate:date2];
//判斷兩個(gè)時(shí)間是否相等
BOOL isEqual = [date1 isEqualToDate:date2];
//返回當(dāng)前時(shí)間10秒后的時(shí)間
NSDate *date01 = [NSDate dateWithTimeIntervalSinceNow:10];
//返回1970-1-1 08:00:00時(shí)間10秒后的時(shí)間
NSDate *date02 = [NSDate dateWithTimeIntervalSince1970:10];
//返回2001-1-1 08:00:00時(shí)間10秒后的時(shí)間
NSDate *date03 = [NSDate dateWithTimeIntervalSinceReferenceDate:10];
//返回date2時(shí)間10秒后的時(shí)間
NSDate *date04 = [NSDate dateWithTimeInterval:10sinceDate:date2];
//隨機(jī)返回一個(gè)比較遙遠(yuǎn)的未來時(shí)間
NSDate *date05 = [NSDate distantFuture];
//隨機(jī)返回一個(gè)比較遙遠(yuǎn)的過去時(shí)間
NSDate *date06 = [NSDate distantPast];
3骑歹、圖片拉伸
//先對(duì)圖片設(shè)置拉伸程度
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];
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
這個(gè)函數(shù)是UIImage的一個(gè)實(shí)例函數(shù)预烙,它的功能是創(chuàng)建一個(gè)內(nèi)容可拉伸,而邊角不拉伸的圖片陵刹,需要兩個(gè)參數(shù)默伍,第一個(gè)是左邊不拉伸區(qū)域的寬度,第二個(gè)是上面不拉伸的高度衰琐。
注意:可拉伸的范圍都是距離leftCapWidth后的1豎排像素也糊,和距離topCapHeight后的1橫排像素,而圖像后面的剩余像素也不會(huì)被拉伸羡宙。
比如參數(shù)指定10狸剃,5。那么狗热,圖片左邊10個(gè)像素钞馁,上邊5個(gè)像素。不會(huì)被拉伸匿刮,x坐標(biāo)為11和一個(gè)像素會(huì)被橫向復(fù)制僧凰,y坐標(biāo)為6的一個(gè)像素會(huì)被縱向復(fù)制。找資料可以加下ios開發(fā)學(xué)習(xí) :626433463
4熟丸、APP跳轉(zhuǎn)/跳轉(zhuǎn)至系統(tǒng)APP
跳轉(zhuǎn)前設(shè)置:
1训措、在將要跳轉(zhuǎn)到的APP中TARGETS--info--URL Types中添加URL Schemes;
2光羞、在本APP的info.plist中添加一個(gè)LSApplicationQueriesSchemes數(shù)組字段绩鸣,把對(duì)方的APP的URL Schemes添加進(jìn)去
//跳轉(zhuǎn)到系統(tǒng)app
url = @"tel://1234567"http://撥打電話
url = @"sms://1234567"http://發(fā)短信
url = @"http://www.baidu.com"http://Safari
url = @"mailto://admin@abt.com"http://郵箱
url = @"maps://"http://地圖
url = @"facetime://1234567"http://FaceTime
//跳轉(zhuǎn)到系統(tǒng)設(shè)置
url = UIApplicationOpenSettingsURLString//適用于iOS >= 8
url = @"Prefs:root=General"http://適用于iOS < 10
//跳轉(zhuǎn)第三方APP
url = @"要跳轉(zhuǎn)的app的URL Schemes"
//開始跳轉(zhuǎn)
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) {
if(IOS_10) {
[[UIApplication sharedApplication] openURL:URL options:@{} completionHandler:nil];
}else{
[[UIApplication sharedApplication] openURL:URL];
}
}
5、UIView中的坐標(biāo)轉(zhuǎn)換
// 將像素point由point所在視圖轉(zhuǎn)換到目標(biāo)視圖view中纱兑,返回在目標(biāo)視圖view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
// 將像素point從view中轉(zhuǎn)換到當(dāng)前視圖中呀闻,返回在當(dāng)前視圖中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
// 將rect由rect所在視圖轉(zhuǎn)換到目標(biāo)視圖view中,返回在目標(biāo)視圖view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// 將rect從view中轉(zhuǎn)換到當(dāng)前視圖中潜慎,返回在當(dāng)前視圖中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
例把UITableViewCell中的subview(btn)的frame轉(zhuǎn)換到 controllerA中
// controllerA 中有一個(gè)UITableView, UITableView里有多行UITableVieCell捡多,cell上放有一個(gè)button
// 在controllerA中實(shí)現(xiàn):
CGRect rc = [cell convertRect:cell.btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:cell.btn.frame fromView:cell];
// 此rc為btn在controllerA中的rect
或當(dāng)已知btn時(shí):
CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
6蓖康、修改Xcode代碼自動(dòng)生成版權(quán)信息Copyright ? 2017年 XXX. All rights reserved
如圖,就是修改這個(gè)
修改方法:打開.xcodeproj工程文件局服,顯示包含內(nèi)容钓瞭,會(huì)看到一個(gè)project.pbxproj文件,打開此文件修改 ORGANIZATIONNAME = "xxx";
7淫奔、iOS讓button的文字局左或局右對(duì)齊
我們首先想到的方法是這個(gè)
button.titleLabel.textAlignment = NSTextAlignmentLeft;
突然發(fā)現(xiàn)這是無效的山涡,下面正確的設(shè)置方式:
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//局左
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;//局右
8、去除searchBar的灰色背景
近日唆迁,在做搜索框UISearchBar的時(shí)候鸭丛,把searchBar放在導(dǎo)航欄的titleView上,當(dāng)進(jìn)入下一頁然后再返回本頁的時(shí)候searchBar的灰色背景會(huì)閃一下唐责,那么怎么去除這個(gè)灰色背景呢鳞溉?
調(diào)用如下方法即可去除灰色背景,哈哈鼠哥。熟菲。。
- (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頂部分割線顏色
//根據(jù)顏色生成一個(gè)圖片
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抄罕、壓縮圖片
創(chuàng)建一個(gè)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