iOS 中常用到的实用小功能总结

1、 多个异步任务进行处理,用GCD的dispatch_group_t,还有其他的方法,不过用这个就好了

// 1.创建dispatch_group_t
dispatch_group_t group = dispatch_group_create();
for (int i=0; i<10; i++) {
     // 2、将当前的下载操作添加到组中
      dispatch_group_enter(group);
        {
              // 数据请求成功离开当前组
              dispatch_group_leave(group);
        }
}
// 3.当所有图片都下载完毕再通过闭包通知调用者
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
         // 能够来到这个地方, 一定是所有任务都已经完成
});

2、给App进行评分

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、 跳转进入app的设置界面

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

4、label设置删除线

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、 设置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设置圆角时产生UI不流畅的解决

//        只需要加上这段代码就可以去除锯齿
imageView.layer.shouldRasterize = YES;
当shouldRasterize设成true时,layer被渲染成一个bitmap,并缓存起来,等下次使用时不会再重新去渲染了。实现圆角本身就是在做颜色混合(blending),如果每次页面出来时都blending,消耗太大,这时shouldRasterize = yes,下次就只是简单的从渲染引擎的cache里读取那张bitmap,节约系统资源。

额外收获:如果在滚动tableView时,每次都执行圆角设置,肯定会阻塞UI,设置这个将会使滑动更加流畅。
原文链接:http://blog.csdn.net/zhuangyou123/article/details/8737367

8、 打电话

//1、这种发放,拨打完电话回不到原来的应用,会停留在通讯录里面,而且是直接拨打,不弹出提示
      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个的时候要小心,因为apple的文档里边没出现过telprompt这个。      之前是有过被reject的案例。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 1,426评论 0 3
  • 转自:http://www.code4app.com/blog-866962-1317.html1、设置UILab...
    MMOTE阅读 1,687评论 1 1
  • 1、设置UILabel行间距 NSMutableAttributedString*attrString=[[NSM...
    iOS祎阅读 2,222评论 0 0
  • 睡不着,要疯了,真的要疯了,这么下去该怎么办,突然发现离不开它,可是我被他逼的发疯了,调整好自己的心态,一定要保持...
    阿飞飞阅读 252评论 0 0
  • 一、概述 超人发现自己身处凡人世界。他们很特别,与我们不同,因此不受到尊敬。这也是这种题材的影片里既有成功,也有牺...
    禁止放映阅读 3,530评论 0 50