干貨! iOS開(kāi)發(fā)中的實(shí)用小技巧(上)

tip 1 : 給UIImage添加毛玻璃效果

func blurImage(value:NSNumber) -> UIImage {

let context = CIContext(options:[KCIContextUseSoftwareRenderer:true])

let ciImage = CoreImage.CIImage(image:self)

let blurFilter = CIFilter(name:"CIGassianBlur")

blurFilter?.setValue(ciImage, forKey:KCIInputImageKey)

blurFilter?.setValue(value, forKey:"inputRadius")

let imageRef = context.createCGImage((blurFilter?.outputImage)!, fromRect:(ciImage?.extent)!)

let newImage = UIImage(CGImage:imageRef)

return newImage

說(shuō)到這里推薦下我自己建的iOS開(kāi)發(fā)學(xué)習(xí)群:680565220,群里都是學(xué)ios開(kāi)發(fā)的,如果你正在學(xué)習(xí)ios ,小編歡迎你加入,今天分享的這個(gè)案例已經(jīng)上傳到群文件,大家都是軟件開(kāi)發(fā)黨,不定期分享干貨(只有iOS軟件開(kāi)發(fā)相關(guān)的),包括我自己整理的一份2017最新的iOS進(jìn)階資料和高級(jí)開(kāi)發(fā)教程,歡迎進(jìn)階中和進(jìn)想深入iOS的小伙伴。

}

value : value代表毛玻璃效果的程度

核心內(nèi)容:let blurFilter = CIFilter(name:"CIGassianBlur") 使用濾鏡工具

tip 2 : 圖片壓縮

func imageCompress(targetWidth:CGFloat) -> UIIMage {

let targetHeight = targetWidth/width*height

UIGraphicsBeginImageContext(CGSizeMake(targetWidth,targetHeight))

self.drawInRect(CGRectMake(0,0,targetWidth,targetHeight))

let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()

UIGrapicsEndImageContext()

return newImage

}

這里是按原UIImage比例做壓縮所以:let targetHeight = targetWidth/width*height

tip 3 : SVN & Git 用法總結(jié)

一:SVN

A. 項(xiàng)目經(jīng)理:1.創(chuàng)建項(xiàng)目—CheckIn

2.設(shè)置項(xiàng)目人員

B.工作人員:1.CheckOut 獲取項(xiàng)目的完整副本,此工作只需要做"一次"

2. 工作寫代碼....

3.階段性工作完成后,Commit(提交) 將自己修改的文件,上傳到服務(wù)器

每天下班前一定要做一次能夠編譯的提交!

4.定期Update項(xiàng)目,從服務(wù)器將最新的內(nèi)容更新到本地,每天上班第一件事情一定要做的事情!

二. Git

A.項(xiàng)目經(jīng)理:1.創(chuàng)建項(xiàng)目push

2.設(shè)置項(xiàng)目人員

B. 工作人員:1.Pull從服務(wù)器下拉最新的本版

2.Commit是將修改過(guò)的代碼提交至本地的代碼庫(kù)

3.每天下班前Push自己當(dāng)前的代碼到服務(wù)器

4.每天上班前從服務(wù)器Pull最新的內(nèi)容

三. M / A 文件更新

對(duì)于文件夾svn支持并不好,需要在工具下選定文件夾commit

對(duì)應(yīng)文件選定文件commits

由于過(guò)去在公司多半時(shí)間是做獨(dú)立開(kāi)發(fā),最多人的時(shí)候也是兩個(gè)人做開(kāi)發(fā),所以協(xié)作工具用的少,但是不斷的關(guān)注著兩種代碼倉(cāng)庫(kù)管理工具,總結(jié)了一些tip,還有待實(shí)踐驗(yàn)證,吐槽吧......

tip 4: UINavigationController下的坐標(biāo)系

iOS 9 前:

navigationBar 如果設(shè)置了背景圖片,那么可視化坐標(biāo)會(huì)從navgationbar下面開(kāi)始

self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"nav"), forBarMetrics:UIBarMetrics.Default)

iOS 9 后 : 坐標(biāo)從狀態(tài)欄下面開(kāi)始

tip 5 : C字符串轉(zhuǎn)成NSString & NSString轉(zhuǎn)成C字符串

const char *cString = "CString";

C字符串轉(zhuǎn)成NSString : NSString *str = [NSString stringWithUTF8String:cString];

NSString * str = @"NSString";

NSString轉(zhuǎn)成C字符串 : const char *cString = [str UTF8String];

tip 6 : OC & Swift 代碼塊(語(yǔ)法糖)

Objective-C :

UILabel *label1 = ({

UILabel *label = [UILabelnew];

[self.view addSubview:label];

label.frame=CGRectMake(100,100,100,45);

label.backgroundColor= [UIColor redColor];

label.text=@"大家好1";

label;

});

UILabel*label2 = ({

UILabel*label = [UILabel new];

[self.view addSubview:label];

label.frame=CGRectMake(100,160,100,45);

label.backgroundColor= [UIColor redColor];

label.text=@"大家好2";

label;

});

UILabel*label3 = ({

UILabel*label = [UILabel new];

label.frame=CGRectMake(100,250,100,45);

label.backgroundColor= [UIColor redColor];

label.text=@"大家好3";

label;

});

[self.viewaddSubview:label3];

({

UILabel *label = [UILabel new];

[self.view addSubview:label];

label.frame=CGRectMake(100,310,100,45);

label.backgroundColor= [UIColor redColor];

label.text=@"大家好4";

});

Swift:

letlabel1:UILabel= {

let label =UILabel()

self.view.addSubview(label)

label.frame=CGRectMake(100,100,100,45)

label.backgroundColor=UIColor.redColor()

label.text="大家好"

return label

}()

let label2:UILabel= {

let label =UILabel()

label.frame=CGRectMake(100,200,100,45)

label.backgroundColor=UIColor.redColor()

label.text="大家好"

return label

}()

self.view.addSubview(label2)

使用語(yǔ)法糖的好處就是拷貝代碼時(shí)只需做少許的修改就可以達(dá)到目的,如上面的栗子,想要?jiǎng)?chuàng)建多個(gè)label,只要賦值粘貼,改一處,也就是對(duì)象名稱就可以輕松完成!

tip 7 : 數(shù)據(jù)持久化方式歸納總結(jié)

數(shù)據(jù)緩存方式選擇:

1: fmdata數(shù)據(jù)庫(kù)(增刪改查) --數(shù)據(jù)需要:增刪改查

2: coreData --數(shù)據(jù)需要:增刪改查

3:序列化(NSUserDefault) --狀態(tài)、偏好設(shè)置、默認(rèn)選項(xiàng)

4:單獨(dú).plist --列表數(shù)據(jù),城市信息等

5:單獨(dú).json文件 --頁(yè)面列表數(shù)據(jù)

6: realm框架(增刪改查) --數(shù)據(jù)需要:增刪改查

7: FastCoder 某“強(qiáng)哥”推薦,哈哈哈!

tip 8 : 清理Profiles證書文件

~/Library/MobileDevice/Provisioning Profiles

由于平時(shí)會(huì)負(fù)責(zé)多個(gè)項(xiàng)目的上線管理或是開(kāi)發(fā)工作,因此MAC中有很多簽名文件,有時(shí)候都分不清東西南北,一不做,二不休,前往這個(gè)目錄下,將文件刪個(gè)精光,調(diào)試的時(shí)候用到證書再update一下當(dāng)前項(xiàng)目的證書即可

tip 9 : 拿到當(dāng)前屏幕所看到的viewController

Objective-c版本:

- (UIViewController *)getAppRootViewController

{

UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;

UIViewController *topVC = appRootVC;

while (topVC.presentedViewController) {

topVC = topVC.presentedViewController;

}

return topVC;

Swift版本:

func getAppRootViewController()->UIViewController?{

vartopVC=UIApplication.sharedApplication().keyWindow?.rootViewController

while topVC?.presentedViewController!=nil{

topVC=topVC?.presentedViewController

}

return topVC?

}

tip 10 : 制作pch文件

步驟:

1、新建iOS->Other->PCH File

2、targets->BuildSetting->Prefix Header->設(shè)置$(SRCROOT)/文件在工程中的路徑

3、pch能像以前一樣正常使用

如:$(SRCROOT)/FirstProject/PrefixHeader.pch

FirstProject : pch路徑中的最后一個(gè)拓展名

PrefixHeader.pch: 是pch文件名

簡(jiǎn)介:/Users/ly/Desktop/FirstProject/FirstProject

tip 11 : 設(shè)置UINavigationController title

當(dāng) UIViewController作為UINavigationController的根視圖控制器的時(shí)候,將這個(gè)Nav(root)賦給 tabBarController時(shí),

這樣寫:

root.title = @“title”;

那么 :self.naviagtionItem.title 會(huì)顯示 title

同時(shí) :nav.tabBarItem.title 也會(huì)顯示 title

tip 12 : 判斷UIScrollView是橫向滾動(dòng)還是豎向滾動(dòng)

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer {

if([gestureRecognizerisKindOfClass:[UIPanGestureRecognizerclass]]) {

CGPointvelocity = [(UIPanGestureRecognizer*)gestureRecognizervelocityInView:gestureRecognizer.view];

BOOLisHorizontalPanning =fabsf(velocity.x) >fabsf(velocity.y);

return isHorizontalPanning;

}

returnYES;

}

tip 13 : 監(jiān)聽(tīng) backBarButtonItem 返回事件

github上搜: UIViewController+BackButtonHandler 開(kāi)源項(xiàng)目

tip 14 : 讓一個(gè)view透明度失效

self.view.backgroundColor=UIColor(colorLiteralRed:255.0/255, green:255.0/255, blue:255.0/255, alpha:0.3)

let subView = UIView()

subView.tintColor=UIColor.whiteColor()//不透明了

self.view.addSubview(subView)

tip 15 : 從ipod庫(kù)中讀出音樂(lè)文件

// 從ipod庫(kù)中讀出音樂(lè)文件

MPMediaQuery*everything=[[MPMediaQueryalloc]init];

//讀取條件

MPMediaPropertyPredicate*albumNamePredicate=[MPMediaPropertyPredicatepredicateWithValue:[NSNumbernumberWithInt:MPMediaTypeMusic]forProperty:MPMediaItemPropertyMediaType];

[everythingaddFilterPredicate:albumNamePredicate];

NSLog(@"Loggingitemsfromagenericquery...");

NSArray*itemsFromGenericQuery=[everythingitems];

for(MPMediaItem*songinitemsFromGenericQuery){

NSString*songTitle=[songvalueForProperty:MPMediaItemPropertyTitle];

NSLog(@"%@",songTitle);

}

tip 16 : 廣告標(biāo)示符(adId) & adfv標(biāo)示符的那些事

1.如何識(shí)別一個(gè)應(yīng)用安裝在同一個(gè)設(shè)備上呢?

2.如何識(shí)別一個(gè)企業(yè)的應(yīng)用安裝在同一個(gè)設(shè)備上呢?

蘋果給我們提供了advertisingIdentifier 來(lái)解決問(wèn)題1;

只要是同一臺(tái)設(shè)備,那么advertisingIdentifier就是一樣的

但是如果在設(shè)置-隱私-廣告那里關(guān)掉這個(gè)權(quán)限或是還原設(shè)備的話,就沒(méi)辦法了哭死去吧

蘋果給我們提供了identifierForVendor 來(lái)作為一個(gè)企業(yè)的app標(biāo)示符

比如: com.game.yoyo

com.game.xoxo

只要在同一臺(tái)設(shè)備上,那么identifierForVendor 是一樣的

如果:com.game.yoyo

com.buyer.yoyo

不管是不是同一個(gè)應(yīng)用identifierForVendor 都是不一樣的

上代碼:

廣告id:

#import

//每個(gè)設(shè)備有唯一一個(gè),如果重置廣告或設(shè)置-隱私-關(guān)閉廣告就會(huì)關(guān)閉更換

NSString*adId = [[[ASIdentifierManagersharedManager]advertisingIdentifier]UUIDString];

企業(yè)id:

NSString*idfv = [[[UIDevicecurrentDevice]identifierForVendor]UUIDString];

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容