ios常用小技巧

1、打印view所有子视图


1、打开终端输入一下三条命令:

touch ~/.lldbinit

echo display @import UIKit >> ~/.lldbinit

echo target stop-hook add -o \"target stop-hook disable\" >> ~/.lldbinit

输入完没有任何显示就表示成功了,可以打上断点并在控制台输入相关命令查看层级关系,如:

po self.view.frame(打印当前视图的frame)

po [[self view]recursiveDescription](打印所有视图的层级关系)

2、layoutSubviews调用时机


当视图第一次显示的时候会被调用

当这个视图显示在屏幕上了,点击按钮

添加子视图也会调用这个方法

当本视图的大小发生改变时,也会调用这个方法

当子视图的frame发生改变时,也会调用这个方法

当删除子视图时会调用方法

3、NSString过滤特殊字符


//定义一个特殊字符的集合

NSCharaterSet *set = [NSCharaterSet charaterSetWithCharaterInString:

@"@/:;(}{}][//\\@#$%$#%$%@#!@#"];

//过滤字符串的特殊字符

NSString *newString = [trimString stringByTrimmingCharacterInSet:set];

4、TranForm属性


//平移按钮

CGAffineTransform transForm = self.buttonView.transform;

self.buttonView.transform = CGAffineTransformRotate(transForm, 10, 0 );

//旋转按钮

self.buttonView.transform = CGAffineTransformRotate(transForm, M_PI_4 );

//缩放按钮

self.buttonView.transform = CGAffineTransformScale(transForm, 1.2, 1.2);

//初始化复位

self.buttonView.transform = CGAffineTransformIdentity;

5、去掉分割线多余15像素

//首先在viewDidLoad方法加入以下代码

if ([self.tableView respondsToSelector:@selector(setSparatorInset:)]) {

         [self.tableView setSeparatorInset:UIEdgeInsetZero];

}

if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

         [self.tableView setLayoutMargins:UIEdgesetsZero];

}

然后在重写willDisplaycell方法

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath {

        if ([cell reaspondsToSelector:@selector(setSeparatorInset:)]) {

                   [cell setSeparatorInset:UIEdgeInsetsZero];

        }

        if ([cell respondsToselector:@selector(setLayoutMargins:)]) { 

                   [cell setLayoutMargrgins:UIEdgeInsetsZero];

        }

}

6、计算方法耗时时间间隔

//获取时间间隔

#define TICK CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();

#define TOCK NSLog(@“Time: %f , CFAbsoluteTimeGetCurrent()-start”)

7、Color颜色宏定义

//随机颜色

#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 RGBCOLOR(r , g , b)  [UIColor colorWithRed:(r) / 255.0f  green:(g) / 255.0f    blue:(b) / 255.0f    alpha:1]

//利用这种方法设置颜色和透明度,可不影响子视图背景色

#define RGBACOLOR(r , g , b , a)  [UIColor colorWithRed:(r)/255.0f   green:(g)/255.0f  blue:(b)/255.0f   alpha:(a)];

8、Alert提示宏定义

#define Alert(_S_, ...) [[[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:(_S_), ##__VA_ARGS__] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show]

9、让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);

       }];

}

10、NSArray快速求总和 最大值 最小值和平均值

NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];

CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];

CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];

CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];

CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];

NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);

11、修改Label中不同文字颜色


- (void)touchesEnded:(NSSet<UITouch>*)touches withEvent:(UIEvent*)event{

     [self edotStringColor:Self.label.text editStr:@"好" color:[UIColor blueColor]];

}

- (void) edotStringColor:(NSString *)string editStr:(NSString*)edotStr color:(UIColor*)color{

//string为整体字符串,editStr为需要修改的字符串

    NSRange rande = [string rangeOfString:editStr];

    NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc]             initWithString:striking];

    [attribute addAttributes:@{NSForegroundColorAttributeName:color} rande:range];

    self.label.attributedText = attribute;

}

12、播放声音


#import<AVFoundation>

//1.获取音效资源的路径

NSString *path = [[NSBundle mainBundle]pathForResource:@"pour_milk" ofType:@"wav"];

//2.将路径转化为url

NSURL *tempUrl = [NSURL fileURLWithPath:path];

//3.用转化为的url创建一个播放器

NSError *error = nil;

AVAudioPlayer *play = [[AVAudioPlayer alloc] initWithContentsOfURL:tempUrl error:&error];

self.player = play;

//4.播放

[play play];

13、检测是否IPad Pro

- (BOOL)ispadPro {

UIScreen *Screen = [UIScreen mainScreen];

CGFloat width = Screen.nativeBounds.size.width/Screen.nativeScale;

CGFloat height = Screen.nativeBouns.size.height/Screen.nativeScale;

BOOL islpad  = [[UIDevice currentDevice] userlnterfaceldiom] == UIUserInterfaceIdiomPad;

BOOL hasIPadProWidth  = fabs(width  - 1024.f) < DBL xss = removed xss=removed xss = removeed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed>> ~/.lldbinit

echo target stop-hook add -o \"target stop-hook disable\" >> ~/.lldbinit

}

14、Label行间距

- (void) test { 

NSMutableAttributedString *attributedString = [[NSMutableAttributtedString alloc] initWithString:self.contentLabel.text];

NSMutableParagraghStyle*paragraphStyle = [[NSMutableParagraphStyle alloc] init];

[garagraphStyle setLineSpacing:3];

//调整行间距

[attributedString addAttribute:NSParagraphStyleAttributeName  value:paragraphStyle  range:NSMakeRange(0,[self.contentLabel.text length])];

self.contentLabel.attributedText = attributedString;

}

15、UIIMageView填充模式

@"UIViewContentModeScaleTofill", // 拉伸自适应填满整个视图

@"UIViewContentModeScaleAspetFit" // 自适应比例大小显示

@"UIViewContentModeScaleAspectFill" // 原始大小显示

@"UIViewContentModeRedraw", // 尺寸改变时重绘

@"UIViewContentModeCenter",// 中间

@"UIViewContentModeTop",//顶部

@"UIViewContentModeBottom",//底部

@"UIViewContentModeLeft",//中间贴左

@"UIViewContentModeRight",//中间贴右

@"UIViewContentModeTopLeft",          // 贴左上

@"UIViewContentModeTopRight",        // 贴右上

@"UIViewContentModeBottomLeft",      // 贴左下

@"UIViewContentModeBottomRight",      // 贴右下

16、Debug栏自动把Unicode编码转化为汉字

DXXcodeConsoleUnicodePlugin 插件

17、设置状态栏文字样式颜色

[UIAppliction sharedApplication] setStatusBarHidden :NO];

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

18、字符串的相关操作


//去除所有的空格

[str stringByReplacingOccurrencesOfString:@" " withString:@""];

//去掉首尾的空格

[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

- (NSString *)uppercaseString;//全部字符转化为大写字母

- (NSString *)lowercaseString;//全部字符转化为小写字母

19、解决tableview的分割线短一截

-(void)viewDidLayoutSubviews{

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)])

{

{[self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];

}

if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])

{

[self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];

}

}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

if ([cell respondsToSelector:@selector(setSeparatorInset:)])

{

[cell setSeparatorInset:UIEdgeInsetsZero];

}

if ([cell respondsToSelector:@selector(setLayoutMargins:)])

{

[cell setLayoutMargins:UIEdgeInsetsZero];

}

}

20、动态隐藏NavigationBar

//当我们的手离开屏幕时候隐藏

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

if(velocity.y > 0)

}

[self.navigationController setNavigationBarHidden:YES animated:YES];

} else {

[self.navigationController setNavigationBarHidden:NO animated:YES];

}

}

velocity.y这个量,在上滑和下滑时,变化极小(小数),但是因为方向不同,有正负之分,这就很好处理了。

//在滑动过程中隐藏

self.navigationController.hidesBarsOnSwipe = YES;

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

CGFloat offsetY = scrollView.contentOffset.y + __tableView.contentInset.top;

CGFloat panTranslationY = [scrollView.panGestureRecognizer translationInView:self.tableView].y;

if (offsetY > 64) {

if (panTranslationY > 0)

{

//下滑趋势,显示

[self.navigationController setNavigationBarHidden:NO animated:YES];

} else {

//上滑趋势,隐藏

[self.navigationController setNavigationBarHidden:YES animated:YES];

}

} else {

[self.navigationController setNavigationBarHidden:NO animated:YES];

}

}

这里的offsetY > 64只是为了在视图滑过navigationBar的高度之后才开始处理,防止影响展示效果。panTranslationY是scrollView的pan手势的手指位置的y值,可能不是太好,因为panTranslationY这个值在较小幅度上下滑动时,可能都为正或都为负,这就使得这一方式不太灵敏.

参照地址:http://blog.cocoachina.com/article/28221

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容