iOS 中常用到的實(shí)用小功能總結(jié)

1、 多個(gè)異步任務(wù)進(jìn)行處理,用GCD的dispatch_group_t,還有其他的方法,不過用這個(gè)就好了

// 1.創(chuàng)建dispatch_group_t
dispatch_group_t group = dispatch_group_create();
for (int i=0; i<10; i++) {
     // 2、將當(dāng)前的下載操作添加到組中
      dispatch_group_enter(group);
        {
              // 數(shù)據(jù)請求成功離開當(dāng)前組
              dispatch_group_leave(group);
        }
}
// 3.當(dāng)所有圖片都下載完畢再通過閉包通知調(diào)用者
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
         // 能夠來到這個(gè)地方, 一定是所有任務(wù)都已經(jīng)完成
});

2、給App進(jìn)行評分

NSString *str = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=xxxxxx" ];
if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)){
           str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/idxxxxxxx"];
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

3、 跳轉(zhuǎn)進(jìn)入app的設(shè)置界面

NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url]) {
          NSURL*url =[NSURL           
           URLWithString:UIApplicationOpenSettingsURLString];
           [[UIApplication sharedApplication] openURL:url];
}

4、label設(shè)置刪除線

UILabel *originPriceLabel = [UILabel new];
NSString *originPriceString = [NSString stringWithFormat:@"¥ %@0", @"100"];
NSMutableAttributedString *originPriceAttrsStr = [[NSMutableAttributedString alloc] initWithString:originPriceString];
[originPriceAttrsStr addAttribute:NSStrikethroughStyleAttributeName value:@(1)
range:NSMakeRange(0, originPriceString.length)];
originPriceLabel.attributedText = originPriceAttrsStr;

5、 設(shè)置textView或者label的行間距方法

UILabel *label = [UILabel new];
label.numberOfLines = 0;
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 4;
NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle};
label.attributedText = [[NSAttributedString alloc]initWithString:label.text attributes:attributes];

6、 NSArray倒序

NSArray *arr = [NSArray array];
NSArray *tmparr = [[arr reverseObjectEnumerator] allObjects];
NSLog(@"%@",tmparr);

7、UIImageView設(shè)置圓角時(shí)產(chǎn)生UI不流暢的解決

//        只需要加上這段代碼就可以去除鋸齒
imageView.layer.shouldRasterize = YES;
當(dāng)shouldRasterize設(shè)成true時(shí),layer被渲染成一個(gè)bitmap,并緩存起來,等下次使用時(shí)不會再重新去渲染了。實(shí)現(xiàn)圓角本身就是在做顏色混合(blending),如果每次頁面出來時(shí)都blending,消耗太大,這時(shí)shouldRasterize = yes,下次就只是簡單的從渲染引擎的cache里讀取那張bitmap,節(jié)約系統(tǒng)資源。

額外收獲:如果在滾動tableView時(shí),每次都執(zhí)行圓角設(shè)置,肯定會阻塞UI,設(shè)置這個(gè)將會使滑動更加流暢。
原文鏈接:http://blog.csdn.net/zhuangyou123/article/details/8737367

8、 打電話

//1、這種發(fā)放,撥打完電話回不到原來的應(yīng)用,會停留在通訊錄里面,而且是直接撥打,不彈出提示
      NSMutableString * str1=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str1]];
//2,這種方法,打完電話后還會回到原來的程序,也會彈出提示,推薦這種
         NSMutableString * str2=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str2]]];
        [self.view  addSubview:callWebview];
// 3,這種方法也會回去到原來的程序里(注意這里的telprompt),也會彈出提示
     NSMutableString * str3=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"186xxxx6979"];
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str3]];
//用第3個(gè)的時(shí)候要小心,因?yàn)閍pple的文檔里邊沒出現(xiàn)過telprompt這個(gè)。      之前是有過被reject的案例。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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