ios常用小技巧

1懂讯、打印view所有子視圖


1瘟忱、打開終端輸入一下三條命令:

touch ~/.lldbinit

echo display @import UIKit >> ~/.lldbinit

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

輸入完沒有任何顯示就表示成功了载庭,可以打上斷點并在控制臺輸入相關(guān)命令查看層級關(guān)系,如:

po self.view.frame(打印當(dāng)前視圖的frame)

po [[self view]recursiveDescription](打印所有視圖的層級關(guān)系)

2、layoutSubviews調(diào)用時機


當(dāng)視圖第一次顯示的時候會被調(diào)用

當(dāng)這個視圖顯示在屏幕上了,點擊按鈕

添加子視圖也會調(diào)用這個方法

當(dāng)本視圖的大小發(fā)生改變時螃征,也會調(diào)用這個方法

當(dāng)子視圖的frame發(fā)生改變時,也會調(diào)用這個方法

當(dāng)刪除子視圖時會調(diào)用方法

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 );

//旋轉(zhuǎn)按鈕

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

//縮放按鈕

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

//初始化復(fù)位

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]

//利用這種方法設(shè)置顏色和透明度背率,可不影響子視圖背景色

#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應(yīng)用直接退出

- (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.將路徑轉(zhuǎn)化為url

NSURL *tempUrl = [NSURL fileURLWithPath:path];

//3.用轉(zhuǎn)化為的url創(chuàng)建一個播放器

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];

//調(diào)整行間距

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

self.contentLabel.attributedText = attributedString;

}

15都许、UIIMageView填充模式

@"UIViewContentModeScaleTofill", // 拉伸自適應(yīng)填滿整個視圖

@"UIViewContentModeScaleAspetFit" // 自適應(yīng)比例大小顯示

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

@"UIViewContentModeRedraw", // 尺寸改變時重繪

@"UIViewContentModeCenter",// 中間

@"UIViewContentModeTop",//頂部

@"UIViewContentModeBottom",//底部

@"UIViewContentModeLeft",//中間貼左

@"UIViewContentModeRight",//中間貼右

@"UIViewContentModeTopLeft",? ? ? ? ? // 貼左上

@"UIViewContentModeTopRight",? ? ? ? // 貼右上

@"UIViewContentModeBottomLeft",? ? ? // 貼左下

@"UIViewContentModeBottomRight",? ? ? // 貼右下

16稻薇、Debug欄自動把Unicode編碼轉(zhuǎn)化為漢字

DXXcodeConsoleUnicodePlugin 插件

17、設(shè)置狀態(tài)欄文字樣式顏色

[UIAppliction sharedApplication] setStatusBarHidden :NO];

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

18胶征、字符串的相關(guān)操作


//去除所有的空格

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

//去掉首尾的空格

[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

- (NSString *)uppercaseString;//全部字符轉(zhuǎn)化為大寫字母

- (NSString *)lowercaseString;//全部字符轉(zhuǎn)化為小寫字母

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、動態(tài)隱藏NavigationBar

//當(dāng)我們的手離開屏幕時候隱藏

- (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這個量睛低,在上滑和下滑時案狠,變化極蟹(小數(shù)),但是因為方向不同骂铁,有正負之分吹零,這就很好處理了。

//在滑動過程中隱藏

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市撼嗓,隨后出現(xiàn)的幾起案子窃蹋,更是在濱河造成了極大的恐慌,老刑警劉巖静稻,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件警没,死亡現(xiàn)場離奇詭異,居然都是意外死亡振湾,警方通過查閱死者的電腦和手機杀迹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來押搪,“玉大人树酪,你說我怎么就攤上這事〈笾荩” “怎么了续语?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長厦画。 經(jīng)常有香客問我疮茄,道長,這世上最難降的妖魔是什么根暑? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任力试,我火速辦了婚禮,結(jié)果婚禮上排嫌,老公的妹妹穿的比我還像新娘畸裳。我一直安慰自己,他們只是感情好淳地,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布怖糊。 她就那樣靜靜地躺著帅容,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伍伤。 梳的紋絲不亂的頭發(fā)上丰嘉,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天,我揣著相機與錄音嚷缭,去河邊找鬼饮亏。 笑死,一個胖子當(dāng)著我的面吹牛阅爽,可吹牛的內(nèi)容都是我干的路幸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼付翁,長吁一口氣:“原來是場噩夢啊……” “哼简肴!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起百侧,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤砰识,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后佣渴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體辫狼,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年辛润,在試婚紗的時候發(fā)現(xiàn)自己被綠了膨处。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡砂竖,死狀恐怖真椿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情乎澄,我是刑警寧澤突硝,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站置济,受9級特大地震影響解恰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜舟肉,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一修噪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧路媚,春花似錦、人聲如沸樊销。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至裤园,卻和暖如春撤师,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拧揽。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工剃盾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人淤袜。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓痒谴,卻偏偏與公主長得像,于是被迫代替她去往敵國和親铡羡。 傳聞我的和親對象是個殘疾皇子积蔚,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 打印View所有子視圖 layoutSubviews調(diào)用的調(diào)用時機 當(dāng)視圖第一次顯示的時候會被調(diào)用當(dāng)這個視圖顯示到...
    hyeeyh閱讀 513評論 0 3
  • 1. 打印View所有子視圖 po [[self view]recursiveDescription] 2. la...
    Hurricane_4283閱讀 967評論 0 2
  • 一、去掉tableviewcell的左邊多余的15像素 1.可以改變tableviewcell的樣式為self.t...
    Mr_Bob_閱讀 181評論 0 1
  • 2017年9月17日 晴 星期日(17) 從孩子入學(xué)那天就開始寫日記了漱贱,已經(jīng)堅持寫了十幾天了,之前都是用手寫在日記...
    大野芳菲閱讀 303評論 0 0