iOS app开发中常用技巧
博客专区 > IamOkay 的博客 > 博客详情
iOS app开发中常用技巧
IamOkay 发表于1年前
iOS app开发中常用技巧
  • 发表于 1年前
  • 阅读 91
  • 收藏 2
  • 点赞 2
  • 评论 0

腾讯云 学生专属云服务套餐 10元起购>>>   

摘要: iOS app开发中常用技巧

1.退出App

- (void)exitApplication {
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
    
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
    
}

必要时,我们还需要设置Plist属性

在plist设置屬性:Application does not run in background = YES

2.直接回到主界面

[self.navigationController popToRootViewControllerAnimated:YES]; 

当然,你也可以使用 [UIApplication shareApplication].keyWindow.rootViewController.viewControllers进行遍历回退

3.禁止UIScrollView及其子类边界弹簧效果

mWebView..scrollView.bounces = NO

4.修改状态栏颜色

第一种方法:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];

if (is_ios_7_Later) {

   self.view.window.frame = CGRectMake(0, 20, self.view.window.frame.size.width, self.view.window.frame.size.height - 20);
  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
}

第二种方法(这方法比较简单):

UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
statusBarView.backgroundColor=[UIColor blackColor];
[self.view addSubview:statusBarView];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];

[UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor colorWithHexString:@"#D3331f"];

    if([[UIColor getWhiteBlackByHexString:@"#D3331f"] isEqual:[UIColor blackColor]])
    {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }
    else
    {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }

5.解决UIImage的imageWithContentsOfFile获取图片非自动适配的问题

+(NSString *) pathForResource:(NSString *) picName type:(NSString *) type
{
    
    if([NSStringisEmptyOrNil:picName])
    {
        returnnil;
    }
    if(![NSStringisEmptyOrNil:type] && [NSStringtrim:type].length>0)
    {
       
        
        return [selfgetBundlePath:picName type:type];
        
    }
    else
    {
        return [self pathForResource:picName];
    }

}

+(NSString *) getBundlePath:(NSString *) picName type:(NSString *) type
{
    
    NSString* suggestScaleSuffix  = [UIImagegetImageScaleSuffix];
    
    NSString * fitForPicName = [picName stringByAppendingString:suggestScaleSuffix];
    NSString * path = [[NSBundlemainBundle] pathForResource:fitForPicName ofType:type inDirectory:@"myios.bundle"];
    
    if([NSStringisEmptyOrNil:path])
    {
        
        if([@"@3x"isEqualToString:suggestScaleSuffix])
        {
            NSString * fitForPicName = [picName stringByAppendingString:@"@2x"];
            path = [[NSBundlemainBundle] pathForResource:fitForPicName ofType:type inDirectory:@"myios.bundle"];
            
        }
        
        if([NSStringisEmptyOrNil:path] && [@"@2x"isEqualToString:suggestScaleSuffix])
        {
            path = [[NSBundlemainBundle] pathForResource:picName ofType:type inDirectory:@"myios.bundle"];
            
        }
        
    }
    return path;
}

+(NSString *) pathForResource:(NSString *) imageFullName
{
    
    if(imageFullName==nil)
    {
        return  nil;
    }
    
     NSString *pathExtension = [imageFullName pathExtension];
     NSString *subExts = @".png|.PNG|.9.png|.9.PNG|.jpeg|.JPEG|.jpg|.JEG|.bmp|.BMP|.gif|.GIF|.ico|.ICO|.tif|.TIF|.cur|.CUR|.xbm|.XBM";
    
    if([NSStringisEmptyOrNil:pathExtension])
    {
        NSString * path = nil;
        NSArray * imageSuffixes = [subExts componentsSeparatedByString:@"|"];
        
        for (NSString * imageSuffix in imageSuffixes)
        {
            path = [selfgetBundlePath:imageFullName type:imageSuffix];
            
            if(![NSStringisEmptyOrNil:path])
            {
                return path;
            }
        }
    
    }
    elseif([subExts containsString:[@"."stringByAppendingString:pathExtension]])
    {
        
        imageFullName = [imageFullName substringWithRange:NSMakeRange(0, imageFullName.length-pathExtension.length-1)];
        NSString * path = [selfgetBundlePath:imageFullName type:pathExtension];
    
        return path;
    }
    
    returnnil;
}


+(NSString *)getImageScaleSuffix
{
    if ( [[[UIDevicecurrentDevice] systemVersion] intValue] >= 4 && [[UIScreenmainScreen] scale] == 2.0 )
    {
        
        return@"@2x";
    }elseif ( [[[UIDevicecurrentDevice] systemVersion] intValue] >= 4 && [[UIScreenmainScreen] scale] == 3.0 )
    {
        
        return@"@3x";
    
    }else{
        return@"";
    }

}

6.解决SQlite数据库批量操作问题

批量插入:

INSERT INTO MYTABLE VALUES(.....),(.....),(.....).....(.....);

批量更新插入

REPLACE INTO MYTABLE VALUES(.....),(.....),(.....).....(.....);

查询受影响的函数

在sqlite中可以直接调用

sqlite_changes(sqlite * db);

当然你也可以使用如下语句

SELECT CHANGES();

7.iOS UITableView默认行高问题

UITableView默认secctionheader与sectionFooter高度是10,当然代理返回值是0的话依然是10,为了不影响使用0高度,将代理的返回值0.0001

关于UITableView缓存identifier 

​​​​​​​不要写成局部静态变量,建议写成全局变量,并且在初始化时使其变成和以往不重复的值

8.时间戳计算

  NSDateFormatter * datefmt = [[NSDateFormatteralloc] init];
        [datefmt setTimeStyle:NSDateFormatterNoStyle];
        [datefmt setDateStyle:NSDateFormatterNoStyle];
        NSTimeZone * timezone = [NSTimeZonetimeZoneWithName:@"Asia/Chongqing"];
        [datefmt setTimeZone: timezone];
        
        [datefmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSDate * sDate = [datefmt dateFromString:@"2014-12-25 12:24:35"];
        
        NSDate * tDate = [NSDatedate];
        
        NSTimeInterval startTime = [sDate timeIntervalSince1970];
        NSTimeInterval toTime =[tDate timeIntervalSince1970] ;
        NSTimeInterval interval = round(toTime-startTime);
        
        NSLog(@"interval=%f",interval);
        NSLog(@"interval=%@",[datefmt stringFromDate:[NSDatedateWithTimeIntervalSince1970:interval ]]);

9.ARC下获取对象的引用计数

NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)myObject));

10.拦截Ajax请求,实现native与web同步与异步通信

重点是实现NSURLProtocol和跨域通信问题

11.截屏

- (UIImage *)screenshot:(UIViewController *) controller
{
    CGRect rect = controller.view.bounds;
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (context == NULL) return nil;
    CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);
    
    [self layoutIfNeeded];
    [[controller.view layer] renderInContext:context];
    
    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenshotImage;
}

 

共有 人打赏支持
粉丝 165
博文 405
码字总数 301129
×
IamOkay
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: