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調用時機


當視圖第一次顯示的時候會被調用

當這個視圖顯示在屏幕上了,點擊按鈕

添加子視圖也會調用這個方法

當本視圖的大小發(fā)生改變時,也會調用這個方法

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

當刪除子視圖時會調用方法

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

//調整行間距

[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、設置狀態(tài)欄文字樣式顏色

[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、動態(tài)隱藏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這個量,在上滑和下滑時,變化極?。ㄐ?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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容