美文网首页
多年iOS开发经验总结(整理后转)

多年iOS开发经验总结(整理后转)

作者: 心亦逸风 | 来源:发表于2017-11-30 08:57 被阅读7次

    今天看到一篇总结开发经验的文章,看了几条发现一些很有用,准备记录一下把认为比较通用需要的进行再总结(原地址):
    1.view截图

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    

    2.view设置圆角

    #define ViewBorderRadius(View, Radius, Width, Color)\
    \
    [View.layer setCornerRadius:(Radius)];\
    [View.layer setMasksToBounds:YES];\
    [View.layer setBorderWidth:(Width)];\
    [View.layer setBorderColor:[Color CGColor]] // view圆角
    

    3.各种系统文件目录获取

    //获取temp(存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除)
    #define PathTemp NSTemporaryDirectory()
    //获取Document(应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录)
    #define PathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
    //获取Cache(存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除)
    #define PathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
    

    4.导入自定义字体库

    ---
    1、找到你想用的字体的 ttf 格式,拖入工程
    2、在工程的plist中增加一行数组,“Fonts provided by application”
    3、为这个key添加一个item,value为你刚才导入的ttf文件名
    4、直接使用即可:label.font = [UIFont fontWithName:@"你刚才导入的ttf文件名" size:20.0];
    ---
    

    5.在非ViewController的地方弹出UIAlertController对话框

    //  最好抽成一个分类
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
    //...
    id rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    if([rootViewController isKindOfClass:[UINavigationController class]])
    {
        rootViewController = ((UINavigationController *)rootViewController).viewControllers.firstObject;
    }
    if([rootViewController isKindOfClass:[UITabBarController class]])
    {
        rootViewController = ((UITabBarController *)rootViewController).selectedViewController;
    }
    [rootViewController presentViewController:alertController animated:YES completion:nil];
    

    6.在状态栏增加网络请求的菊花,类似safari加载网页的时候状态栏菊花
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    7.关于tableview删除一行
    他给的方法是

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // 如果是你需要隐藏的那一行,返回高度为0
        if(indexPath.row == YouWantToHideRow)
            return 0; 
        return 44;
    }
    
    // 然后再你需要隐藏cell的时候调用
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    

    但是我觉得一般应用场景删除是要把tableview的数组中的数据删除,我之前在做带关联的问卷中有用到tableview的cell的添加和删除,是这样写的

    //删除一行动画
    - (void)tableViewDeleteCellWithIndexPath:(NSIndexPath *)indexPath
    {
        [_tableView beginUpdates];
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [_tableView endUpdates];
    }
    //添加一行动画
    - (void)tableViewInsertCellWithIndexPath:(NSIndexPath *)indexPath
    {
        [_tableView beginUpdates];
        [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [_tableView endUpdates];
    }
    

    相关文章

      网友评论

          本文标题:多年iOS开发经验总结(整理后转)

          本文链接:https://www.haomeiwen.com/subject/mgqubxtx.html