注意手勢會冒泡上拋,一個view沒有實現的手勢,若是父類view有實現,父視圖就處理,若是不想讓父視圖處理,就把本視圖添加到底層window上html
setMasksToBounds:YESgit
imageView.contentMode=UIViewContentModeScaleAspectFit;web
imageview.animationImages=圖片數組數組
將某個子視圖移到最上層網絡
[self.window bringSubviewToFront:redView];app
將某個子視圖移到最底層ide
[self.window sendSubviewToBack:blueView];佈局
是否裁剪子視圖動畫
View.clipsToBounds=YES;url
當父視圖的尺寸發生變化時,子視圖以怎樣的方式發生變化,子視圖的寬度和高度靈活的隨着父視圖變化
topView.autoresizingMask=UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleHeight;
UIImageView *imgview; 圖片動畫,區別於UIview動畫(屬性動畫和翻轉動畫)
[imgview startAnimating];
連續縮放
imageView.transform=CGAffineTransformScale(imageView.transform, 1.2, 1.2);
方法能夠設置動畫的延遲時間、動畫效果、動畫結束後執行的代碼
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear
animations:^{
} completion:^(BOOL finished) {
}];
nil 和null 的區別
UInavigationController.view.appearance
UIView翻轉 AnimationTransition
1.
設置翻轉動畫的樣式,對象
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];
設置翻轉動畫,第一個參數:操做的視圖對象,持續時間,翻轉樣式選項,動畫代碼(桌面翻轉)
[UIView transitionWithView:self.window duration:2 options:UIViewAnimationOptionTransitionFlipFromTop
animations:^{
[self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:2];
} completion:nil];
CALayer動畫
實例化一個動畫對象,指定動畫內容
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"position"];
設置動畫的起始位置
animation.fromValue=[NSValue valueWithCGPoint:imageView.center];
設置動畫的結束位置
animation.toValue=[NSValue valueWithCGPoint:CGPointMake(300, 600)];
動畫的持續時間
animation.duration=2;
設置是否將動畫還原爲初始狀態
animation.autoreverses=YES;
動畫重複次數
animation.repeatCount=1;
動畫是添加在layer上的,動畫結束後會恢復原來的樣式,若是不想還原,須要設置layer的相應屬性
imageView.layer.position=CGPointMake(300, 600);
給layer添加動畫
[imageView.layer addAnimation:animation forKey:@"position"];
UITextField
leftView設置textField內左側的視圖 leftViewMode設置左視圖一直顯示(UITextFieldViewModeAlways)
clearButtonMode設置清除文本的按鈕的顯示模式
clearsOnBeginEditing設置編輯一開始清空原始文本內容
adjustsFontSizeToFitWidth是否自動調整字號
//能夠設置驗證
設置是否容許結束編輯,默認是YES,能夠設置最少輸入的字數
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
設置用戶輸入字符個數必須大於6,不然鍵盤不會隱藏
if (textField.text.length<6) {
return NO;
}
return YES;
}
是否容許響應return鍵,默認YES,並在單擊return後實現隱藏鍵盤,須要實現UITextFieldDelegate協議方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];//不能少
return YES;
}
UIViewController
數據共享,在代理類中添加共享的成員變量
AppDelegate *delegate=[UIApplication sharedApplication].delegate;
單例類:從[Application sharedApplication] 獲取應用程序對象,是一個單例類
獲取應用程序的代理對象,(應用程序對象只有一個,代理對象也只有一個,用來作全局變量,共享數據)
動畫切換
sub.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:sub animated:YES completion:nil];
是否返回前一個視圖控制器對象root
[self dismissViewControllerAnimated:YES completion:nil];
獲取上一次的視圖控制器對象
lastViewController *last=(lastViewController *)[self presentingViewController];
隱藏狀態欄
-(BOOL)prefersStatusBarHidden{
return YES;
}
UINavigationController
切換到根視圖
[self.navigationController popToRootViewControllerAnimated:YES];
設置是否裁剪背景圖片(背景圖片不在狀態欄顯示)
self.navigationController.navigationBar.clipsToBounds=YES;
全部的視圖控制器共用一個navigationBar
可是每一個視圖控制器都須要設置本身的navigationItem
UIBarButtonItem
UIBarButtonItem *item4=[[UIBarButtonItem alloc]initWithCustomView:btn2];
UIBarButtonItem *itemSystem=[[UIBarButtonItem
alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self action:@selector(back:)];
設置空白項,寬度是根據佈局靈活變化的
UIBarButtonItem *item2=[[UIBarButtonItem
alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:@selector(back:)];
UIWebView:加載網絡資源
一、獲取資源的url
NSString *path=[[NSBundle mainBundle]pathForResource:@"about" ofType:@"html"];
NSURL *url=[NSURL fileURLWithPath:path];
2、將url封裝到一個NSURLRequest請求對象中
NSURLRequest *request=[NSURLRequest requestWithURL:url];
UIWebView *webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 375,667)];
[self.view addSubview:webView];
3、將請求到的資源在webveiw上顯示
[webView loadRequest:request];
小組件
[XXX addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
UISwitch:開關組件
UIActivityIndicatorView簡單的指示器,沒有監聽功能(就是那個下載時的菊花)
XXX.hidesWhenStopped=NO;
UISegmentedControl分段選取器
NSArray *titles=@[@"紅",@"綠",@"藍"];
UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:titles];
獲取用戶選擇的segment的下標(從0開始)
NSInteger index=sender.selectedSegmentIndex;
NSString *title=[sender titleForSegmentAtIndex:index];
定時器
_timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(XXX) userInfo:nil
repeats:YES];
中止定時器
[_timer invalidate];
_timer=nil;
xib能夠快速建立界面,代碼比較慢
代碼適合git等代碼管理,xib不適合
代碼適合維護升級,xib不適合
1)若是xib資源名和類文件的名稱相同,自動加載同名的資源
2)加載資源包中的MyView文件,取出其中的內容,爲一個數組,
第一個參數是xib文件名,第二個參數是相應的處理事件的對象,第三個參數通常爲nil
owner屬於誰,誰去監聽
NSArray *views= [[NSBundle mainBundle]loadNibNamed:@"MyView" owner:self options:nil];
NSUserDefaults:單例類
NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
將數據同步寫入文件裏面
[userDefaults synchronize];
沙盒目錄
/*獲取Document目錄,返回的是數組
第一個參數是獲取目錄的類型
第二個參數是獲取IOS應用程序的一個參數
第三個參數是否展開
*/
NSString *docPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
獲取Library目錄
NSString *libraryPath=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
獲取Cache 目錄,Library目錄下的Cache目錄
NSString *cachePath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
獲取應用程序的tmp目錄
NSString *tmpPath=NSTemporaryDirectory();
===========================================================================================================
app目錄
NSString *appPath=[NSBundle mainBundle].bundlePath;
獲取應用程序的數據家目錄
NSString * homePath=NSHomeDirectory();
絕對路徑,同上
NSString *path=[homePath stringByAppendingPathComponent:@"Documents/app.plist"];
使用圖片原始色彩樣式效果,阻止使用系統顏色渲染,防止出現系統顏色遮蓋原始色彩
image=[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
設置tabBarItem的徽章
view2.tabBarItem.badgeValue=@"2";
遍歷中的一些問題
聲明爲父類,指向任意類型的子類
for (UIViewController * vc in controllers) {
if ([vc.title isEqualToString:title]) {
tabBarController.selectedViewController=vc;
break;
}
}
指定爲父類的指針,只看對象不看指針
容許與用戶交互,默認是NO,子視圖也不能與用戶交互
UIView.userInteractionEnabled=YES;
UIResponder是全部的響應者對象的父類
UIView UIWindow AppDelegate UIViewController 都是UIResponder的子類
設置是否容許多點觸摸
view.multipleTouchEnabled=YES;
當用戶在屏幕上移動,尚未離開屏幕時
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
[touches count]獲取觸摸的手指數
將觸摸開始時手指在當前視圖的座標保存下來(用於計算偏移值之類)
CGPoint _originPoint=[touch locationInView:self];
=============================================================================
處理移動手勢
獲取手勢在某個視圖上的座標偏移值
CGPoint pt=[sender translationInView:sender.view];
從新清空手勢的偏移值(偏移值會累積)CGPointZero,至關於CGPointMake(0,0)
[sender setTranslation:CGPointZero inView:sender.view];
獲取滾動視圖的內容偏移值
CGPoint pt=scrollView.contentOffset;
處理旋轉手勢
設置view的旋轉角度(經過sender.rotation屬性獲取)
view.transform=CGAffineTransformRotate(view.transform, sender.rotation);
清空
sender.rotation=0
容許與用戶交互(能夠響應用戶事件)
imageView.userInteractionEnabled=YES;
一個單向關係兩個手勢識別器
[rotationGestureRecognizer canPreventGestureRecognizer:pinchGestureRecognizer];
響應手勢的協議方法
if (sender.state==UIGestureRecognizerStateBegan||sender.state==UIGestureRecognizerStateChanged)
設置是否實現同時識別多個手勢
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
判斷兩個手勢是否對同一個視圖的操做,若是是,能夠同時識別
if (gestureRecognizer.view==otherGestureRecognizer.view) {
return YES;
}return NO;
}
滾動視圖必須設置的項
scrollView.contentSize=size;
設置是否有回彈效果
scrollView.bounces=NO;
設置是否容許分頁
scrollView.pagingEnabled=NO;
設置內容視圖的最大、最小的縮放比例
scrollView.minimumZoomScale=0.5;
scrollView.maximumZoomScale=2;
若是設置pageEnable 爲yes,必定會調用此方法(減速效果)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
UIPageControl *_pageControl;頁碼控制器
動視圖當前視圖、頁碼指示器保持一致。
實時監聽
在消息通知中心訂閱消息(鍵盤將要顯示),當消息來到時,self調用@selector中的方法響應
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
將一個變量轉換成一個id類型 @{} @[] @()
警告框
自定義
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提示" message:@"你肯定要退
出嗎" preferredStyle:UIAlertControllerStyleAlert];
模態展現
[self presentViewController:alertController animated:YES completion:nil];
表格的可編輯狀態,默認NO
_tableView.editing=NO;
開關
[_tableView setEditing:!_tableView.editing animated:YES];
隱藏狀態欄
-(BOOL)prefersStatusBarHidden{
return YES;
}
=====================================================================================
ASI
預編譯
$(SRCROOT)/項目名/JDDemo-PrefixHeader.pch
自適應
當父視圖的尺寸發生變化時,子視圖以怎樣的方式發生變化,子視圖的寬度和高度靈活的隨着父視圖變化
topView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.view.adjustsFontSizeToFitWidth=YES;
注意手勢會冒泡上拋,一個view沒有實現的手勢,若是父類view有實現,父視圖就處理,若是不想讓父視圖處理,就把本視圖添加到底層window上
動畫===============================================================
UIImageView *imgview; 圖片動畫,區別於UIview動畫(屬性動畫和翻轉動畫)
[imgview startAnimating];
imageview.animationImages=圖片數組//連續動畫
方法能夠設置動畫的延遲時間、動畫效果、動畫結束後執行的代碼
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveLinear
animations:^{
} completion:^(BOOL finished) {
}];
===============================================================
可變屬性字符串
NSMutableAttributedString *selectInfo = [[NSMutableAttributedString alloc] initWithString:[NSString
stringWithFormat:@"已選 %@ %ld件",XXX,XXX]];
[selectInfo addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(3,
selectInfo.length - 3)];
方法調用和執行
reponseForSelector
performSelector: withObject://運行時肯定是否有這個方法,編譯時不關心