iOS 代码可能遗忘的小技巧

1.计算程序运行时间

#define STARTTIME NSDate *startTime = [NSDate date]
#define STOPTIME NSLog(@"Time: %f", -[startTime timeIntervalSinceNow])

2.根据模拟器或真机执行不同代码

#if defined (__i386__) || defined (__x86_64__)  
    //模拟器下执行  
#else  
    //真机下执行  
#endif

3.颜色的宏定义

// 随机颜色
#define RANDOM_COLOR [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]
// 颜色(RGB)
#define RGB_COLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
// 利用这种方法设置颜色和透明值,可不影响子视图背景色
#define RGBA_COLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

4.iOS应用程序直接退出

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

5.设置状态栏文字样式颜色

[[UIApplication sharedApplication] setStatusBarHidden:NO];
// 黑色(默认)
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
// 白色
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
// 动画的改变statusBar的前景色
 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];

6.获取iOS路径方法

//获取家目录路径的函数
NSString *homeDir = NSHomeDirectory();
//获取Documents目录路径的方法
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
//获取Documents目录路径的方法
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
//获取tmp目录路径的方法:
NSString *tmpDir = NSTemporaryDirectory();

7.将网络数据转换成字符串

-(NSString *)getDataByURL:(NSString *)url {
    return [[NSString alloc] initWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]] encoding:NSUTF8StringEncoding];
}

8.iPhone/iPad强制横屏

[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

9.键盘监听事件

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];

10.UITableViewCell 隐藏多余的分割线


-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

11.在项目中打开appStore及打开对软件的评价

//以下是通过id打开此软件的评价  
        int m_appleID = 836500024;  
        NSString *str = [NSString stringWithFormat:@"itms-apps//ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d",  m_appleID ];  
        //以下是通过appstore链接到自己应用在商店的位置 eg:打开微信
        NSString *url = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/wechat/id836500024?mt=12"];  
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];  

12.项目拨打电话,调用safari,点用mail,调用SMS,跳转设置界面

//1、调用 电话phone
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4008008288"]];
//2、调用自带 浏览器 safari
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.abt.com"]];
//3、调用 自带mail
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://[email protected]"]];
//4、调用 SMS
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];
//5,跳转到系统设置相关界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];

13.获取设备版本号

float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 

14.查看设备所支持的字体

for (NSString *family in [UIFont familyNames]) { 
// 查看字体簇
    NSLog(@"%@", family); 
    for (NSString *font in [UIFont fontNamesForFamilyName:family]) { 
        NSLog(@"\t%@", font); 
    } 
} 

15.页面切换效果

// 模态推出界面效果
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
// 模态推出界面
[self presentModalViewController:controller animated:YES]; 

//可供使用的效果:
UIModalTransitionStyleCoverVertical  //新视图从下向上出现 
UIModalTransitionStyleFlipHorizontal //以设备的长轴为中心翻转出现 
UIModalTransitionStyleCrossDissolve  //渐渐显示 
UIModalTransitionStylePartialCurl    //原视图向上卷起 

//恢复之前的页面:
[self dismissModalViewControllerAnimated:YES]; 

16.获取截屏

- (UIImage *)getScreenShot { 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 

17.UIScrollView 相关设置

// 隐藏竖向滚动条
scrollView.showsVerticalScrollIndicator = FALSE;
// 隐藏横向滚动条
scrollView.showsHorizontalScrollIndicator = FALSE;
// 设置滑动范围不超过本身即不显示边界缓冲
[scrollView setBounces:NO];
  • 结束是另一种开始
  • 本文持续更新...

你可能感兴趣的:(iOS 代码可能遗忘的小技巧)