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這個值在較小幅度上下滑動時烁挟,可能都為正或都為負婴洼,這就使得這一方式不太靈敏.