轉(zhuǎn)載至iOS開發(fā)小技巧及小知識點(diǎn)
1杜秸、Category(類別)
什么是Category
1例隆、Category可以在不獲悉不改變原代碼的情況下向已有的類中添加方法憔维,從而達(dá)到擴(kuò)展已有類的目的微姊,但是只能添加方法航攒,不建議刪除和修改(會導(dǎo)致bug)呕屎。
2让簿、無法向Category中添加實(shí)例變量,Category通常作為一種組織框架代碼的工具來使用秀睛。
3拜英、如果Category和原始類中的方法名稱沖突,則Category將覆蓋原始類的方法琅催,因?yàn)镃ategory具有更高的優(yōu)先級居凶。
Category的作用:
1、將類的實(shí)現(xiàn)分散到多個不同文件或不同框架中藤抡。
2侠碧、創(chuàng)建對私有方法的前向引用。
3缠黍、向?qū)ο筇砑臃钦絽f(xié)議弄兜。
2、關(guān)于日期(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與當(dāng)前時間的差(單位:秒)
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];
//返回當(dāng)前時間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:10 sinceDate:date2];
//隨機(jī)返回一個比較遙遠(yuǎn)的未來時間
NSDate *date05 = [NSDate distantFuture];
//隨機(jī)返回一個比較遙遠(yuǎn)的過去時間
NSDate *date06 = [NSDate distantPast];
3瓷式、圖片拉伸
//先對圖片設(shè)置拉伸程度
UIImage *image = [UIImage imageNamed:@"pic.png"];
image = [image stretchableImageWithLeftCapWidth:10 topCapHeight: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
這個函數(shù)是UIImage的一個實(shí)例函數(shù)替饿,它的功能是創(chuàng)建一個內(nèi)容可拉伸,而邊角不拉伸的圖片贸典,需要兩個參數(shù)视卢,第一個是左邊不拉伸區(qū)域的寬度,第二個是上面不拉伸的高度廊驼。
注意:可拉伸的范圍都是距離leftCapWidth后的1豎排像素据过,和距離topCapHeight后的1橫排像素惋砂,而圖像后面的剩余像素也不會被拉伸。
比如參數(shù)指定10绳锅,5西饵。那么,圖片左邊10個像素鳞芙,上邊5個像素眷柔。不會被拉伸,x坐標(biāo)為11和一個像素會被橫向復(fù)制原朝,y坐標(biāo)為6的一個像素會被縱向復(fù)制驯嘱。
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中添加一個LSApplicationQueriesSchemes數(shù)組字段丙笋,把對方的APP的URL Schemes添加進(jìn)去
//跳轉(zhuǎn)到系統(tǒng)app
url = @``"[tel://1234567](tel://1234567)" //撥打電話`
`url = @``"[sms://1234567](sms://1234567)"` `//發(fā)短信`
`url = @``"[http://www.baidu.com](http://www.baidu.com/)"` `//Safari`
`url = @``"[mailto://admin@abt.com](mailto://admin@abt.com)"` `//郵箱`
`url = @``"[maps://](maps://)"` `//地圖`
`url = @``"[facetime://1234567](facetime://1234567)"` `//FaceTime`
`//跳轉(zhuǎn)到系統(tǒng)設(shè)置`
`url = UIApplicationOpenSettingsURLString ``//適用于iOS >= 8`
`url = @``"Prefs:root=General"` `//適用于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 中有一個UITableView, UITableView里有多行UITableVieCell怠肋,cell上放有一個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時:
CGRect rc = [btn.superview convertRect:btn.frame toView:self.view];
或
CGRect rc = [self.view convertRect:btn.frame fromView:btn.superview];
6、修改Xcode代碼自動生成版權(quán)信息Copyright ? 2017年 XXX. All rights reserved
修改方法:打開.xcodeproj工程文件淹朋,顯示包含內(nèi)容笙各,會看到一個project.pbxproj文件,打開此文件修改 ORGANIZATIONNAME = "xxx";
7础芍、iOS讓button的文字局左或局右對齊
我們首先想到的方法是這個
button.titleLabel.textAlignment = NSTextAlignmentLeft;
突然發(fā)現(xiàn)這是無效的杈抢,下面正確的設(shè)置方式:
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//局左
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;//局右
8、去除searchBar的灰色背景
近日仑性,在做搜索框UISearchBar的時候惶楼,把searchBar放在導(dǎo)航欄的titleView上,當(dāng)進(jìn)入下一頁然后再返回本頁的時候searchBar的灰色背景會閃一下诊杆,那么怎么去除這個灰色背景呢歼捐?
調(diào)用如下方法即可去除灰色背景,哈哈晨汹。豹储。。
- (void)removeSearchBarGrayBackColor
{
for (int i = 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 (int j = 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ù)顏色生成一個圖片
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)建一個UIImage的類
@interface UIImage (Scale)
//壓縮圖片
- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage;
@end
@implementation UIImage (Scale)
- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage
{
CGFloat maxWidth = 640;
if (sourceImage.size.width < maxWidth) {
return sourceImage;
}
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;
} else if (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();
return newImage;
}
@end
知行辦公颂翼,專業(yè)移動辦公平臺https://zx.naton.cn/
【總監(jiān)】十二春秋之晃洒,3483099@qq.com;
【Master】zelo朦乏,616701261@qq.com球及;
【運(yùn)營】運(yùn)維艄公,897221533@qq.com呻疹;****
【產(chǎn)品設(shè)計】流浪貓吃引,364994559@qq.com;
【體驗(yàn)設(shè)計】兜兜刽锤,2435632247@qq.com镊尺;
【iOS】淘碼小工,492395860@qq.com并思;iMcG33K庐氮,imcg33k@gmail.com;
【Android】人猿居士宋彼,1059604515@qq.com弄砍;思路的頓悟,1217022114@qq.com输涕;
【java】首席工程師MR_W音婶,feixue300@qq.com;
【測試】土鏡問道莱坎,847071279@qq.com衣式;
【數(shù)據(jù)】fox009521,42151960@qq.com檐什;