{
11圈澈、核心動畫 需要簽協(xié)議痘昌,但是系統(tǒng)幫簽好
一茂嗓、CABasicAnimation
1餐茵、創(chuàng)建基礎(chǔ)動畫對象
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
2、設(shè)置動畫屬性并包裝(執(zhí)行時間述吸,起點(默認0)忿族,終點,重復(fù)執(zhí)行(MAXFLOAT非常大的浮點數(shù)))
animation.duration = 1;
animation.fromValue = @1;
animation.toValue = @2;
animation.repeatCount = 3;
3、將動畫添加到視圖的layer上道批,key:標識一下動畫
[_imageView.layer addAnimation:animation forKey:@"CYC666"];
4错英、動畫執(zhí)行完畢,保存最后狀態(tài)(默認YES)+ 動畫執(zhí)行完畢隆豹,不移除動畫(枚舉)
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
二走趋、關(guān)鍵幀動畫
1、創(chuàng)建關(guān)鍵幀動畫對象
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
2噪伊、包裝關(guān)鍵幀(點)
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(150, 600)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(250, 100)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(350, 500)];
3簿煌、將關(guān)鍵幀添加到數(shù)組
animation.values = @[value1, value2, value3, value4];
4、設(shè)置動畫屬性
animation.duration = 2;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
5鉴吹、將動畫添加到圖層layer上
[_imageView.layer addAnimation:animation forKey:@"CYC666"]
6姨伟、其他方法創(chuàng)建關(guān)鍵字
//路徑? animation.path = path.CGPath;
四、動畫租
1豆励、創(chuàng)建動畫租
CAAnimationGroup *group=[[CAAnimationGroup alloc]init];
2夺荒、將動畫放入動畫租,交給動畫組執(zhí)行
group.animations=@[an1,an2,an3];
3良蒸、設(shè)置動畫組屬性(時長技扼、重復(fù)次數(shù))
4、將動畫組添加到視圖layer
[_myImageView.layer addAnimation:group forKey:@"group1"];
五嫩痰、轉(zhuǎn)場動畫http://www.reibang.com/p/77e089b08b8b
1剿吻、創(chuàng)建轉(zhuǎn)場動畫
CATransition *transition=[[CATransition alloc] init];
2、設(shè)置轉(zhuǎn)場動畫的屬性
transition.duration=2;
transition.type=@"push"; //指定動畫的類型
transition.subtype=@"fromTop";? ? //通過子類型,可以控制控制器切換的方向
3串纺、添加到view的layer上
[_myImageView.layer addAnimation:transition forKey:@"tran1"];
}
==================================================================================================================================================
{
12丽旅、睡眠
1、[NSThread sleepForTimeInterval:9];
2纺棺、sleep(9);
}
==================================================================================================================================================
{
13榄笙、UIImagePicker 和 相冊
一、相片的加載方式
1.通過imageNamed:方式加載
第一次會去沙盒路徑加載圖片祷蝌,將它緩存到內(nèi)存中茅撞,以后每次都從內(nèi)存中加載,速度得到提升
UIImage *image = [UIImage imageNamed:@"baby.jpg"];
2.通過包路徑加載巨朦,沙盒路徑
每一次都需要手動加載,圖片特別大米丘,占內(nèi)存,而且不經(jīng)常使用罪郊,就應(yīng)該使用這個方法
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"baby" ofType:@"jpg"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:data];
3.從網(wǎng)上加載圖片
加載的圖片大小有限定蠕蚜,超過最大值則不能下載
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.tpqq.com/newpic/2010/0708/20107873131tupian.jpg"]];
UIImage *image = [UIImage imageWithData:imgData];
二匾竿、UIPickerView
1第美、創(chuàng)建
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(30, 100, 300, 200)];
2故黑、簽訂協(xié)議
3、設(shè)置代理
pickerView.dataSource = self;
pickerView.delegate = self;
4想诅、實現(xiàn)代理方法(必須的協(xié)議方法)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
三个唧、 打開照片
1钥顽、創(chuàng)建控制器(UIImagePickerController本身就是導(dǎo)航欄的子類)
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
2坟冲、設(shè)置資源類型
UIImagePickerControllerSourceTypePhotoLibrary, 所有文件夾
UIImagePickerControllerSourceTypeCamera,? ? ? ?資源來自攝像頭
UIImagePickerControllerSourceTypeSavedPhotosAlbum 系統(tǒng)內(nèi)置相冊
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
設(shè)置只顯示視頻
pickerImage.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*) kUTTypeMovie, (NSString*) kUTTypeVideo, nil];
3、簽訂協(xié)議
4邪蛔、設(shè)置代理
imagePickerController.delegate = self;
5急黎、模態(tài)跳轉(zhuǎn)到控制器
[self presentViewController:imagePickerController animated:YES completion:nil];
6、實現(xiàn)代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
A侧到、判斷資源是否來源于相冊勃教,并取出選中的相片
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary || picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
B、取出照片
UIImage *image = info[UIImagePickerControllerOriginalImage];
}
7匠抗、關(guān)閉,返回
[picker dismissViewControllerAnimated:YES completion:nil];
8故源、或者在點擊取消按鈕的協(xié)議方法中
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
四、拍照
1汞贸、判斷攝像頭是否存在
UIImagePickerControllerCameraDeviceRear 前置攝像頭
UIImagePickerControllerCameraDeviceFront 后置攝像頭
if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
return;
}
2绳军、創(chuàng)建控制器
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
3、簽訂協(xié)議
4矢腻、設(shè)置代理
imagePickerController.delegate = self;
5门驾、模態(tài)跳轉(zhuǎn)到控制器
[self presentViewController:imagePickerController animated:YES completion:nil];
6、實現(xiàn)代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
A多柑、判斷相片來源是否是攝像頭
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
B奶是、取出照片
UIImage *image = info[UIImagePickerControllerOriginalImage];
C、將照片存到相冊
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {}
7顷蟆、關(guān)閉,返回
[picker dismissViewControllerAnimated:YES completion:nil];
8诫隅、或者在點擊取消按鈕的協(xié)議方法中
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
五腐魂、打開視頻
1帐偎、創(chuàng)建控制器
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
2、設(shè)置資源類型
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
3蛔屹、媒體類型,圖片(@"public.image")削樊、視頻(@"public.video")
imagePickerController.mediaTypes = @[@"public.movie"];
4、簽訂協(xié)議
5兔毒、設(shè)置代理
imagePickerController.delegate = self;
6漫贞、模態(tài)跳轉(zhuǎn)到控制器
[self presentViewController:imagePickerController animated:YES completion:nil];
7、實現(xiàn)代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
8育叁、播放視頻
NSString *url = info[UIImagePickerControllerMediaURL];
_mpPlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_mpPlayer animated:YES completion:nil];
六迅脐、拍攝視頻
1、判斷后置攝像頭是否存在豪嗽,如果不存在
if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
//iOS9.0之后推薦使用這種提示方式
UIAlertController *alterController = [UIAlertController alertControllerWithTitle:@"攝像頭不存在"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
A谴蔑、兩種提示方式
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
UIAlertAction *alterAction = [UIAlertAction actionWithTitle:@"確定"
style:UIAlertActionStyleDefault
handler:nil];
更改顏色
[cancelActionsetValue:[UIColorredColor]forKey:@"titleTextColor"];
B豌骏、給控制器添加提示
[alterController addAction:alterAction];
[self presentViewController:alterController animated:YES completion:nil];
return;
}
2、創(chuàng)建控制器
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
3隐锭、指定資源類型
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
4窃躲、設(shè)置代理,簽訂協(xié)議
pickerController.delegate = self;
5钦睡、指定媒體類型
pickerController.mediaTypes = @[@"public.movie"];
6蒂窒、模態(tài)轉(zhuǎn)到控制器
[self presentViewController:pickerController animated:YES completion:nil];
7、實現(xiàn)代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
A荞怒、獲取媒體的類型
NSString *mediaType = info[UIImagePickerControllerMediaType];
B洒琢、判斷類型是否一致
if ([mediaType isEqualToString:@"public.image"]) {
C、從字典中獲取圖片對象
UIImage *image = info[UIImagePickerControllerOriginalImage];
D褐桌、當(dāng)圖片來自攝像頭纬凤,保存圖片
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//當(dāng)處于拍攝狀態(tài),將照片存入相冊
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);? //方法名必須這樣子寫}
} else {
E撩嚼、播放視頻
NSString *url = info[UIImagePickerControllerMediaURL];
_mpPlayer = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL URLWithString:url]];
//播放視頻
[self presentViewController:_mpPlayer animated:YES completion:nil];
}
8停士、返回主界面,顯示圖片
[self dismissViewControllerAnimated:YES completion:nil];
七、選取多張相片
1完丽、導(dǎo)入庫文件
#import ? ? ? ? ? ? ? ? ? 資源
#import
#import ? ? ? ? ? ? 資源庫中某個相冊或文件夾
#import ? ? ? ? ? 整個資源庫:所有視頻恋技、照片
#import ? ? 資源描述
#import ? ? ?//必須要導(dǎo)入這個庫文件
2、創(chuàng)建資源庫 全局
_library = [[ALAssetsLibrary alloc] init];
3逻族、通過資源庫訪問資源,有多少個group就調(diào)用多少次block
[_library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//如果存在這個相冊就遍歷
if (group) {
//遍歷相冊中所有的資源
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
/*
if (index == 3) {? ?//遍歷4個相片
*stop = YES; //指針
};
*/
//CGImageRef cimage = [result thumbnail];? //縮略圖
//UIImage *image = [UIImage imageWithCGImage:cimage];
if (result) {
//將數(shù)據(jù)存到數(shù)組中
[_imageArray addObject:result];
}
}];
};
//刷新
[_collectionView reloadData];
} failureBlock:^(NSError *error) {
NSLog(@"訪問失敗");
}];
4蜻底、創(chuàng)建集合視圖顯示收集的相片
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:1001];
//取出應(yīng)資源的數(shù)據(jù)
ALAsset *result = _imageArray[indexPath.row];
//獲取縮略圖
CGImageRef cimage = [result thumbnail];
//轉(zhuǎn)換
UIImage *image = [UIImage imageWithCGImage:cimage];
imageView.image = image;
//獲取到原始圖片
ALAssetRepresentation *presentation = [result defaultRepresentation];
CGImageRef cImage = [presentation fullResolutionImage];
UIImage *fullImage = [UIImage imageWithCGImage:cImage];
return cell;
}
5、通過選中的相片聘鳞,進行多選
}
==================================================================================================================================================
{
14薄辅、觸摸
一、UIResponder
1抠璃、觸摸開始
- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event {
A站楚、系統(tǒng)會幫我們封裝好觸摸點對象,我們可以將它取出使用
UITouch *touch = [touches anyObject];
B搏嗡、觸摸點的屬性
window 所在窗口
view 所在視圖
tapCount 短時間內(nèi)點觸摸的次數(shù)窿春,疊加
phase
C、觸摸點point
CGPoint locationPoint = [touch locationInView:self];? ? //獲取當(dāng)前觸摸的位置
CGPoint pLocationPoint = [touch previousLocationInView:self];? ?//獲取上一個點的位置
}
2采盒、手勢觸摸并滑動(時刻調(diào)用)
- (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event {
//事件event:加速旧乞、遠程遙控、觸摸
//讓視圖隨手指移動
UITouch *touch = [touches anyObject];
CGPoint locationPoint = [touch locationInView:self];? ? //獲取當(dāng)前觸摸的位置
CGPoint pLocationPoint = [touch previousLocationInView:self];? ?//獲取上一個點的位置
CGPoint center = touch.view.center;
center.x += locationPoint.x - pLocationPoint.x; //根據(jù)偏移量修改中心位置
center.y += locationPoint.y - pLocationPoint.y;
touch.view.center = center;
}
3磅氨、觸摸結(jié)束
- (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event;
4尺栖、觸摸取消、被打斷(例如電話打入)
- (void)touchesCancelled:(nullable NSSet *)touches withEvent:(nullable UIEvent *)event;
二烦租、單擊與雙擊
1延赌、獲取觸摸點
UITouch *touch = [touches anyObject];
2货徙、判斷點擊雙擊
if ([touch tapCount] == 1) {
//延遲0.2秒執(zhí)行單擊事件
//[self performSelector:@selector(singerTap) withObject:self afterDelay:.2];//這個方法調(diào)用,不能取消
[self performSelector:@selector(singerTap) withObject:nil afterDelay:.2];
} else if ([touch tapCount] == 2) {
//如果是雙擊皮胡,取消單擊事件痴颊,執(zhí)行雙擊事件
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singerTap) object:nil];
[self doubleTap];
}
補充:
[A requireGestureRecognizerToFail:B]函數(shù),
它可以指定當(dāng)A手勢發(fā)生時屡贺,即便A已經(jīng)滿足條件了蠢棱,也不會立刻觸發(fā),會等到指定的手勢B確定失敗之后才觸發(fā)甩栈。
三泻仙、多點觸控
1、開啟視圖的多點觸控(默認是關(guān)閉的)
self.multipleTouchEnabled = YES;
2量没、獲取多個手指觸摸點
NSArray *array = [touches allObjects];
UITouch *touchA = [array objectAtIndex:0];
UITouch *touchB = [array objectAtIndex:1];
四玉转、事件分發(fā)
1、事件分發(fā),找出最合適的視圖,處理事件 屏幕 -> 系統(tǒng) -> UIApplication ->UIWindow ->WhiteView
2殴蹄、此視圖是否接收觸摸事件
<1>userInteractionEnabled是否為YES
self.userInteractionEnabled
<2>視圖是否隱藏
self.hidden
<3>透明度<0.01
self.alpha
3究抓、觸摸的位置在不在視圖的范圍內(nèi)
4、遍歷自己的子視圖,從上往下遍歷所有的子視圖 重復(fù)以上兩個步奏
[子視圖? hitTest]
五袭灯、響應(yīng)者鏈
1刺下、iOS系統(tǒng)捕獲點擊操作,打包成UIEven對象
2稽荧、UIApplication對象接收事件
3橘茉、UIVindow
4、RootView
5姨丈、SubView -> hitTest -> view
6畅卓、如果view不接受事件,就原路返回
六蟋恬、按壓響應(yīng)
- (void)pressesBegan:(NSSet *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesChanged:(NSSet *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesEnded:(NSSet *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
- (void)pressesCancelled:(NSSet *)presses withEvent:(nullable UIPressesEvent *)event NS_AVAILABLE_IOS(9_0);
七翁潘、搖動響應(yīng)(模擬器也支持搖一搖功能 [Hardware]-[Shake Gesture]或者command+shift+z來測試)
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);
}
==================================================================================================================================================
{
15、手勢
一筋现、UITapGestureRecognizer 輕擊手勢
1唐础、創(chuàng)建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapActiopn:)];
2、屬性
點擊的次數(shù)
tap.numberOfTapsRequired = 2;
點擊的手指個數(shù)
tap.numberOfTouchesRequired = 2;
2矾飞、添加到視圖
[self.view addGestureRecognizer:tap];
3、實現(xiàn)手勢方法 state
狀態(tài)
UIGestureRecognizerStateBegan,? ? 開始
UIGestureRecognizerStateChanged, 正在改變
UIGestureRecognizerStateEnded, 結(jié)束
二呀邢、UIPinchGestureRecognizer 捏合手勢
scale 縮放倍數(shù)
velocity 速度
三洒沦、UIPanGestureRecognizer 平移
1、屬性
minimumNumberOfTouches 最少手指數(shù)
maximumNumberOfTouches 最多手指數(shù)
2价淌、方法
- (CGPoint)translationInView:(nullable UIView *)view;? ? 獲取觸摸點
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;
- (CGPoint)velocityInView:(nullable UIView *)view;
四申眼、UIRotationGestureRecognizer 旋轉(zhuǎn)
1瞒津、屬性
rotation 旋轉(zhuǎn)角度
velocity 速度
五、UILongPressGestureRecognizer 長按
1括尸、屬性
numberOfTapsRequired? ? 長按之前需要輕擊的次數(shù)
numberOfTouchesRequired? ?長按需要的手指數(shù)
minimumPressDuration? ? 長按觸發(fā)的最小時間
allowableMovement? ?長按時允許偏移的像素值
2巷蚪、長按手勢會調(diào)用兩次響應(yīng)方法,所以為了準確調(diào)用濒翻,要在方法里判斷狀態(tài)(有多重狀態(tài))
if(longPressGesture.state==UIGestureRecognizerStateBegan)?{
//?do?something
}elseif(longPressGesture.state==UIGestureRecognizerStateEnded){
//?do?something
}
六屁柏、UISwipeGestureRecognizer 清掃手勢
1、屬性
direction 輕掃方向(枚舉)
numberOfTouchesRequired 輕掃必須的手指數(shù)
}
==================================================================================================================================================
{
16有送、多媒體 (后臺播放?)
一淌喻、AVAudioPlayer? 只能播放本地音視頻,必須設(shè)置成全局變量
1雀摘、導(dǎo)入框架
#import
2裸删、創(chuàng)建
NSString *urlPath = [[NSBundle mainBundle] pathForResource:@"test.mp3" ofType:nil];
NSURL *url = [NSURL URLWithString:urlPath];
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
3、準備播放
[_audioPlayer prepareToPlay];
4阵赠、開始播放
[_audioPlayer play];
5涯塔、設(shè)置參數(shù)(當(dāng)前時間)
_audioPlayer.currentTime = 210;
二、AVPlayer 可播放本地以及網(wǎng)絡(luò)視頻
1清蚀、創(chuàng)建
_avPlayer = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://mp3.qqmusic.cc/yq/5023716.mp3"]];
2伤塌、播放
[_avPlayer play];
三、播放系統(tǒng)聲音
1轧铁、獲取本地音頻
NSString *sysPath=[[NSBundle mainBundle]pathForResource:@"44th Street Medium.caf" ofType:nil];
NSURL *sysUrl=[NSURL fileURLWithPath:sysPath];
2每聪、注冊聲音成為系統(tǒng)聲音(__bridge橋接? 讓ARC管理CF框架的內(nèi)存釋放)? CFArrayRef -->NSArray? CFStringRef -->NSString
SystemSoundID soundID; //標識符,全局變量
AudioServicesCreateSystemSoundID((__bridge CFURLRef)sysUrl, &soundID);
3齿风、播放系統(tǒng)聲音
AudioServicesPlaySystemSound(soundID);
4药薯、播放震動只能真機演示
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
5、暫停系統(tǒng)聲音? 還可以繼續(xù)播放
AudioServicesDisposeSystemSoundID(soundID);
6救斑、完全停止系統(tǒng)聲音
AudioServicesRemoveSystemSoundCompletion(soundID);
四童本、AVAudioRecorder 錄音
1、導(dǎo)入框架
#import
2脸候、設(shè)置存放錄音文件的沙盒路徑穷娱,錄音文件格式為aac,內(nèi)存小运沦,音質(zhì)好
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/recoder.aac"];
3泵额、每次錄音之前把舊的錄音文件刪掉
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
4、錄音配置信息
NSDictionary *info = @{
AVFormatIDKey :
[NSNumber numberWithInt:kAudioFormatMPEG4AAC], //定義文件的格式mac
AVSampleRateKey : @1000,? ? ? ? ? ? ? ? ?//采樣率 11400, 32000, 8000
AVNumberOfChannelsKey : @2,? ? ? ? ? ? //通道的數(shù)目(1單聲道,2立體聲)
AVLinearPCMBitDepthKey : @8,? ? ? ? ? ? //采樣位數(shù)(8, 16, 24, 32)
AVLinearPCMIsBigEndianKey : @NO,? ? ? ? ? ? //大端還是小端 (內(nèi)存的組織方式)
AVLinearPCMIsFloatKey : @NO? ? ? ? ? ? //采樣信號是整數(shù)還是浮點數(shù)
};
5携添、根據(jù)配置信息創(chuàng)建錄音對象嫁盲,全局的,創(chuàng)建前先將其置為nil
_fileUrl = [NSURL fileURLWithPath:path];
NSError *error = nil;
_recoder = [[AVAudioRecorder alloc] initWithURL:_fileUrl
settings:info
error:&error];
6烈掠、準備錄音
[_recoder prepareToRecord];
7羞秤、開始錄音
[_recoder record];
8缸托、播放錄音
_player = nil;
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:_fileUrl error:nil];
[_player prepareToPlay];
[_player play];
五、AVPlayerViewController? ? 可播放網(wǎng)絡(luò)視頻
1瘾蛋、創(chuàng)建AVPlayerViewController
_avplayerVC = [[AVPlayerViewController alloc] init];
2.設(shè)置AVPlayer
_avplayerVC.player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://vf1.mtime.cn/Video/2012/06/21/mp4/120621104820876931.mp4"]];
_avplayerVC.view.frame = CGRectMake((375-200)/2, 100, 200, 150);
[self.view addSubview:_avplayerVC.view];
}
==================================================================================================================================================
{
17俐镐、block ? ? ? ? ? ? ? (定義 - 使用 - 實現(xiàn))
一、block的創(chuàng)建和使用
1哺哼、沒有返回值佩抹、沒有參數(shù)的block,去掉變量名,剩下的就是類型
A幸斥、創(chuàng)建
void (^myBlock1)();
B匹摇、實現(xiàn)
myBlock1 = ^(){
NSLog(@"我就是代碼快啊");
};
C、調(diào)用甲葬、回調(diào)
myBlock1();
2廊勃、有返回值、有參數(shù)的block
NSString *(^myBlock2)(NSInteger a, NSInteger b) = ^(NSInteger a, NSInteger b) {
return @"哈哈";
};
NSString *string = myBlock2(99,88);
NSLog(@"string = %@", string);
3经窖、inlineblock快速召喚block
4坡垫、typedef定義block
A、定義
typedef void (^MyBlock)();
B画侣、創(chuàng)建block變量
MyBlock b = ^{
NSLog(@"typedef");
};
C冰悠、調(diào)用
b();
二、block對局部變量的內(nèi)存管理
1配乱、block自帶自動變量 調(diào)用的時候才會被創(chuàng)建
void (^block1)(int a);
block1 = ^(int a) {
NSLog(@"a = %d", a);
};
block1(10);? ? ?//2016-05-02 15:36:00.979 01_block[3307:117381] a = 10
2.如果block中使用了外部的局部變量,block會保存一下外部變量的值溉卓,在block中調(diào)用的時候,使用的就是之前保存的值搬泥,并且此值之后的任何變化都不會影響
int b = 10;
void (^block2)() = ^() {
NSLog(@"b = %d", b);
};
b = 20;
block2();? ? //2016-05-02 15:36:00.980 01_block[3307:117381] b = 10
3桑寨、外部局部變量在block中都被看做常量
int c = 30;
void (^block3)() = ^() {
//c = 100;? ? //被看做常亮,禁止修改
NSLog(@"c = %d", c);
};
block3();? ?//2016-05-02 15:39:38.201 01_block[3370:118356] c = 30
4忿檩、想要局部變量正常使用 __block修飾
__block int d = 12;
void (^block4)() = ^() {
d = 33;
NSLog(@"d = %d", d);
};
block4();? ?//2016-05-02 15:42:01.683 01_block[3405:119026] d = 33
5尉尾、全局變量都可以正常使用
test = 233;
void (^block5)() = ^() {
NSLog(@"test = %d", test);
};
block5();? ?//2016-05-02 15:44:15.272 01_block[3435:119642] test = 233
三、block導(dǎo)致的循環(huán)引用問題
1燥透、block使用了全局變量沙咏,那么block會持有(retain)變量所在的對象
2、_block作為當(dāng)前對象的屬性班套,self會持有屬性
self ---> _block
3肢藐、_block使用了全局屬性,會持有該屬性所在的對象
_block ---> self? ? ? ? 應(yīng)該斷開這條線
4孽尽、解決方法
使用__weak關(guān)鍵字創(chuàng)建一個弱引用的對象指向強引用的對象窖壕,使用弱引用對象去調(diào)用屬性? 不會造成循環(huán)引用
四、block變量本身的內(nèi)存管理
1杉女、block根據(jù)在內(nèi)存中的位置瞻讽,分為一下三中情況
NSConcreteStackBlock? 棧區(qū)的block? ? copy-->? ?拷貝到堆區(qū)
NSConcreteGlobalBlock? 全局區(qū)的block? copy--> 沒有任何變化
NSConcreteMallocBlock? 堆區(qū)的block? copy--> retainCount +1
2、棧區(qū)的block 局部變量熏挎,由系統(tǒng)管理內(nèi)存
3速勇、如果使用@property聲明一個block,使用copy修飾
}
==================================================================================================================================================
{
18坎拐、Xcode添加字體
1烦磁、將字體font文件導(dǎo)入項目
2、在該plist里添加一個新的項:Fonts provided by application哼勇,在里面的Item項里添加你的font文件的名字都伪,如:迷你簡胖娃.TTF(注意大小寫)
3、前往 項目 -> Build Phases -> Compile sources积担,將字體文件拖到這里
4陨晶、使用,不是文件名,而是用字體預(yù)覽冊打開字體時在標題欄里顯示的文字就是你的字體的名字帝璧。
[label setFont:[UIFont fontWithName:@”font的名字” size:20]];
}
==================================================================================================================================================
{
19先誉、常用控件
一、UILabel 文本標簽
1的烁、屬性
text 顯示的文本
textAlignment 對其方式(枚舉)
font 字體
textColor 字體顏色
numberofLines 文本最多行數(shù)
lineBreakMode 文本過長時的截斷方式(只有numberofLines為0時才有效果)
label.lineBreakMode = NSLineBreakByCharWrapping;以字符為顯示單位顯示褐耳,后面部分省略不顯示。
label.lineBreakMode = NSLineBreakByClipping;剪切與文本寬度相同的內(nèi)容長度渴庆,后半部分被刪除铃芦。
label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字以……方式省略,顯示尾部文字內(nèi)容襟雷。
label.lineBreakMode = NSLineBreakByTruncatingMiddle;中間的內(nèi)容以……方式省略刃滓,顯示頭尾的文字內(nèi)容。
label.lineBreakMode = theNSLineBreakByTruncatingTail;結(jié)尾部分的內(nèi)容以……方式省略嗤军,顯示頭的文字內(nèi)容注盈。
label.lineBreakMode = NSLineBreakByWordWrapping;以單詞為顯示單位顯示,后面部分省略不顯示叙赚。
enabled 設(shè)置文本內(nèi)容是否可變
minimumScaleFactor 文本最小字體
highlightedTextColor 高亮?xí)r的顏色(與highlighted一起使用)
shadowColor 文本陰影顏色
shadowOffset 陰影偏移量
userInteractionEnabled 是否接受用戶交互(默認是NO)
preferredMaxLayoutWidth 優(yōu)先選擇標簽布局的最大寬度
baselineAdjustment (枚舉)如果adjustsFontSizeToFitWidth屬性設(shè)置為YES老客,這個屬性就來控制文本基線的行為
UIBaselineAdjustmentAlignBaselines 文本最上端與中線對齊
UIBaselineAdjustmentAlignCenters 文本中線與label中線對齊
UIBaselineAdjustmentNone 文本最低端與label中線對齊
attributedText 標簽屬性文本,能一起設(shè)置
adjustsFontSizeToFitWidth=YES; ? ? 根據(jù)字數(shù)調(diào)整text的大小
2震叮、方法
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;
3胧砰、通過文本計算空間大小
NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:16]};
CGRect textRect= [self.model.text? boundingRectWithSize:CGSizeMake(kScreenWidth - 2 *kSpace, 999999)? attributes:dic context:nil];
二、UITextField 文本輸入框
1苇瓣、屬性
enablesReturnKeyAutomatically 默認為No,如果設(shè)置為Yes,文本框中沒有輸入任何字符的話尉间,右下角的返回按鈕是disabled的
borderStyle 設(shè)置邊框樣式(枚舉),必須設(shè)置
backgroundColor 設(shè)置輸入框的背景顏色,此時設(shè)置為白色 如果使用了自定義的背景圖片邊框會被忽略掉
background 設(shè)置背景視圖哲嘲,只有UITextBorderStyleNone的時候改屬性有效
disabledBackground 設(shè)置enable為no時贪薪,textfield的背景
placeholder 占位符,(改變placeHolder的顏色眠副,使用富文本画切,單純改變textcolor是不可以的)
clearButtonMode 輸入框中是否有個叉號,在什么時候顯示囱怕,用于一次性刪除輸入框中的內(nèi)容(枚舉)
text 輸入框中的文字
secureTextEntry ? ? ? ? ? ? ? ? ? ? ? ?? ?????是否安全輸入
autocorrectionType ? ? ? ? ? ? ? ? ? ? ?????是否糾錯(枚舉)
clearsOnBeginEditing ? ? ? ? ? ? ?? ?????是否再次編輯就清空
textAlignment ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????水平對其方式
contentVerticalAlignment ? ? ? ? ?????? 垂直對其方式
adjustsFontSizeToFitWidth 設(shè)置為YES時文本會自動縮小以適應(yīng)文本窗口大小.默認是保持原來大小,而讓長文本滾動
設(shè)置自動縮小顯示的最小字體大小
text.minimumFontSize = 20;
keyboardType 設(shè)置鍵盤樣式(枚舉)
autocapitalizationType 首字母大寫模式(枚舉)
returnKeyType return按鈕樣式(枚舉)
keyboardAppearance 鍵盤的外觀(枚舉)
tintColor 設(shè)置光標的顏色
rightView 最右側(cè)加圖片是以下代碼 左側(cè)類似
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
text.rightView=image;
text.rightViewMode = UITextFieldViewModeAlways;
editing 是否允許編輯
delegate 代理霍弹,對應(yīng)的協(xié)議為
2、代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
return YES;彈出鍵盤
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
//當(dāng)用戶使用自動更正功能娃弓,把輸入的文字修改為推薦的文字時典格,就會調(diào)用這個方法。
//這對于想要加入撤銷選項的應(yīng)用程序特別有用
//可以跟蹤字段內(nèi)所做的最后一次修改台丛,也可以對所有編輯做日志記錄,用作審計用途耍缴。
//要防止文字被改變可以返回NO
//這個方法的參數(shù)中有一個NSRange對象,指明了被改變文字的位置齐佳,建議修改的文本也在其中
- (BOOL)textFieldShouldClear:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
[textField endEditing:YES];? ? ? ?//點擊 return 時收起鍵盤
3私恬、鍵盤相關(guān)的通知事件
UITextFieldTextDidBeginEditingNotification? ? //已經(jīng)開始編輯
UITextFieldTextDidChangeNotification? ? ? ? //已經(jīng)開始改變
UITextFieldTextDidEndEditingNotification? ? //已經(jīng)結(jié)束編輯
UIKeyboardWillShowNotification? ?//鍵盤顯示之前發(fā)送
UIKeyboardDidShowNotification? ? //鍵盤顯示之后發(fā)送
UIKeyboardWillHideNotification? ?//鍵盤隱藏之前發(fā)送
UIKeyboardDidHideNotification? ? //鍵盤隱藏之后發(fā)送
4、失去焦點炼吴、光標的問題:
看看tintColor是否設(shè)置了本鸣,很有可能是因為tintColor是白色的,你看不見看不見
三硅蹦、UIImageView
1荣德、屬性
highlightedImage 高亮圖片
animationImages 設(shè)置序列幀動畫的圖片數(shù)組
highlightedAnimationImages 設(shè)置高亮狀態(tài)下序列幀動畫的圖片數(shù)組(必須設(shè)置imageView為高亮狀態(tài))
animationDuration 設(shè)置序列幀動畫播放的時長
animationRepeatCount 設(shè)置序列幀動畫播放的次數(shù)
userInteractionEnabled 設(shè)置是否允許用戶交互,默認不允許用戶交互(重要)
highlighted 設(shè)置是否為高亮狀態(tài)童芹,默認為普通狀態(tài)
2涮瞻、方法
[imageView startAnimating];? ? ?//開啟圖片動畫
- (void)startAnimating;
- (void)stopAnimating;
- (BOOL)isAnimating;
3、拉伸image(左右端蓋假褪,上下端蓋署咽,只對那一像素進行拉伸,其他像素不變)
特別注意:image本身不應(yīng)該大于視圖的大小生音。如果image比視圖還大宁否,那還拉伸個屁
兩個數(shù)值都是像素的大小,可以用image.size.width*0.5這種方式去設(shè)置
UIImage *newImage = [image stretchableImageWithLeftCapWidth:0 topCapHeight:4];
4缀遍、image的填充模式(填充模式必須在設(shè)置圖片之前設(shè)置慕匠,否則會出錯或設(shè)置不湊效)
imageView.contentMode = UIViewContentModeScaleAspectFit;? ? 不縮放,一邊剛好一邊不夠
UIViewContentModeScaleToFill? ? 填充
UIViewContentModeScaleAspectFill? ? ?等比例填滿(nice)
5域醇、壓縮image
NSData*thumbData =UIImageJPEGRepresentation(image,0.5);
6台谊、設(shè)置圓角蓉媳,必須設(shè)置剪切模式,不然只有描邊锅铅,沒有圓角
headImageView.layer.masksToBounds=YES;
7酪呻、從網(wǎng)上獲取image
NSURL*imageUrl = [NSURLURLWithString:[USER_DobjectForKey:@"head_img"]];
UIImage*image = [UIImageimageWithData:[NSDatadataWithContentsOfURL:imageUrl]];
//圖片壓縮到指定大小
- (
UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withImage:(UIImage*)image {
UIImage*sourceImage = image;
UIImage*newImage =nil;
CGSizeimageSize = sourceImage.size;
CGFloatwidth = imageSize.width;
CGFloatheight = imageSize.height;
CGFloattargetWidth = targetSize.width;
CGFloattargetHeight = targetSize.height;
CGFloatscaleFactor =0.0;
CGFloatscaledWidth = targetWidth;
CGFloatscaledHeight = targetHeight;
CGPointthumbnailPoint =CGPointMake(0.0,0.0);
if(CGSizeEqualToSize(imageSize, targetSize) ==NO)
{
CGFloatwidthFactor = targetWidth / width;
CGFloatheightFactor = targetHeight / height;
if(widthFactor > heightFactor)
scaleFactor = widthFactor;
// scale to fit heightelse
scaleFactor = heightFactor;
// scale to fit width
scaledWidth= width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the imageif(widthFactor > heightFactor)
{
thumbnailPoint.
y= (targetHeight - scaledHeight) *0.5;
}
elseif(widthFactor < heightFactor)
{
thumbnailPoint.
x= (targetWidth - scaledWidth) *0.5;
}
}
UIGraphicsBeginImageContext(targetSize);// this will crop
CGRectthumbnailRect =CGRectZero;
thumbnailRect.
origin= thumbnailPoint;
thumbnailRect.
size.width= scaledWidth;
thumbnailRect.
size.height= scaledHeight;
[sourceImage
drawInRect:thumbnailRect];
newImage =
UIGraphicsGetImageFromCurrentImageContext();
if(newImage ==nil)
NSLog(@"could not scale image");
//pop the context to get back to the defaultUIGraphicsEndImageContext();
returnnewImage;
}
四、UIButton 按鈕
1狠角、屬性
buttonType 類型(枚舉)
2.adjustsImageWhenDisabled
當(dāng)按鈕禁用的情況下号杠,圖像的顏色會被畫深一點蚪腋,默認為YES丰歌。
3.adjustsImageWhenHighlighted
當(dāng)按鈕高亮的情況下,圖像的顏色會被畫深一點屉凯,默認為YES立帖。
4、方法
+ (instancetype)buttonWithType:(UIButtonType)buttonType? ? ?創(chuàng)建按鈕
5悠砚、設(shè)置按鈕內(nèi)容的對其方式
addressBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;? ? //左對齊
addressBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;? ? ? ? //向上對其
五晓勇、UIActivityIndicatorView 活動指示器
1、創(chuàng)建灌旧,確定樣式绑咱。不必設(shè)置frame,因為frame只影響背景的大小枢泰,指示器大小不變
_activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
2描融、停止動畫后隱藏,默認是YES
_activity.hidesWhenStopped = NO;
3衡蚂、開啟動畫
[_activity startAnimating];
4窿克、停止動畫
[_activity stopAnimating];
5、是否在動
[_activity isAnimating];
六毛甲、UIAlertView 提示
1年叮、創(chuàng)建,注意UIAlertView調(diào)用show顯示出來的時候玻募,系統(tǒng)會自動強引用它只损,不會被釋放。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"麻煩開始了嗎七咧?"
message:@"麻煩不斷"
delegate:self
cancelButtonTitle:@"拒絕"
otherButtonTitles:@"默默忍受",@"我不想說話",@"嘿嘿跃惫,我們豎著排啦", nil];
2、顯示
[alert show];
3坑雅、標題
alert.title = @"點擊這個昵稱灰會變美哦";
4辈挂、信息
alert.message = @"你只是個小馬仔,永遠都是";
5裹粤、按鈕個數(shù)(只讀)
NSLog(@"%ld", alert.numberOfButtons);
6终蒂、取消按鈕的index,可以自己設(shè)置
alert.cancelButtonIndex = 1;
7蜂林、模式,沒效果
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
8、代理拇泣、協(xié)議噪叙、協(xié)議方法
當(dāng)點擊了按鈕
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
消失
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex霉翔;
即將消失
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex睁蕾;
七、UIDatePicker 日期采集器
1债朵、創(chuàng)建
UIDatePicker *date = [[UIDatePicker alloc] init];
2子眶、模式
date.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
3、設(shè)置picker的顯示模式:只顯示日期
date.datePickerMode = UIDatePickerModeDate;
4序芦、日期
NSLog(@"%@", date.date);
5臭杰、設(shè)置時區(qū)
timeZone
6、設(shè)置日歷(當(dāng)前)
[datePicker setCalendar:[NSCalendar currentCalendar]];
7谚中、最小日期
minimumDate
8渴杆、最大日期
maxinumDate
9、手動調(diào)整轉(zhuǎn)到日期
maxinumDate
10宪塔、監(jiān)聽值改變
[date addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
八磁奖、UIPageControl 頁碼指示器
1、創(chuàng)建
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 600, 150, 50)];
2、設(shè)置控制的頁數(shù)
_pageControl.numberOfPages = 6;
3、當(dāng)前頁數(shù)
_pageControl.currentPage = 3;
4赊级、當(dāng)只有一頁時,不顯示指示器(默認NO)
_pageControl.hidesForSinglePage = YES;
5敢辩、設(shè)置頁碼指示器顏色
_pageControl.pageIndicatorTintColor = [UIColor redColor];
6、設(shè)置當(dāng)前頁碼指示器的顏色
_pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];
7弟疆、添加監(jiān)聽事件(手勢觸控才會觸發(fā))
[_pageControl addTarget:self action:@selector(pageControlAction) forControlEvents:UIControlEventValueChanged];
九戚长、UISegment 分段控制器
1、創(chuàng)建
UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"第一",@"第二",@"第三",@"第四"]];
2怠苔、數(shù)目(只讀)
NSLog(@"%ld", segment.numberOfSegments);
3同廉、樣式(無效)
segment.segmentedControlStyle = UISegmentedControlStyleBar;
4、顏色樣式(有像素的地方的顏色)
segment.tintColor = [UIColor cyanColor];
5柑司、設(shè)置在點擊后是否恢復(fù)原樣(效果不好)
segment.momentary = YES;
6迫肖、選中項
segment.selectedSegmentIndex = 1;
7、設(shè)置標題
[segment setTitle:@"二二" forSegmentAtIndex:1];
8攒驰、獲取標題
NSLog(@"%@", [segment titleForSegmentAtIndex:1]);
9蟆湖、添加值改變事件
[segment addTarget:self action:@selector(segmentAction) forControlEvents:UIControlEventValueChanged];
10、其他方法
- (void)setImage:(nullable UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
- (nullable UIImage *)imageForSegmentAtIndex:(NSUInteger)segment;
- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
- (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment;
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;? ? //使某一項不可選
十玻粪、UISlider 滑塊
1隅津、創(chuàng)建
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(35.5, 300, 300, 50)];
2诬垂、最小值、最大值伦仍、當(dāng)前值
slider.maximumValue = 10;
slider.minimumValue = 0;
slider.value = 5;
3结窘、值小邊顏色、值大邊顏色充蓝、滑塊顏色
slider.maximumTrackTintColor = [UIColor lightGrayColor];
slider.minimumTrackTintColor = [UIColor redColor];
slider.thumbTintColor = [UIColor orangeColor];
4隧枫、添加值改變響應(yīng)
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
5、如果設(shè)置YES谓苟,在拖動滑塊的任何時候官脓,滑塊的值都會改變。默認設(shè)置為YES
slider.continuous = NO;
6娜谊、給最左邊添加圖片确买,圖片不在滑動條上
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
十一、UISwitch 開關(guān)
1纱皆、ON時的顏色 onTintColor
2、OFF時的顏色 tintColor
3芭商、onImage
4派草、offImage
5、設(shè)置ON
swithImage.on = YES;
6铛楣、thumbTintColor 拇指顏色
7近迁、增加事件響應(yīng)機制
十二、UITextView
1簸州、創(chuàng)建
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(35.5, 20, 300, 600)];
2鉴竭、text: 設(shè)置textView中文本
textView.text =
3、字體
textView.font = [UIFont systemFontOfSize:20];
4岸浑、顏色
textView.textColor = [UIColor whiteColor];
5搏存、背景顏色
textView.backgroundColor = [UIColor brownColor];
6、對其方式
textView.textAlignment = NSTextAlignmentLeft;
7矢洲、是否可以被輸入(默認YES),當(dāng)設(shè)置為NO璧眠,不能編輯文字,但是能選中
textView.editable = NO;
8读虏、當(dāng)設(shè)置為NO以后责静,就不能編輯了,不能選中文字
textView.selectable = NO;
9盖桥、設(shè)置文本灾螃,一旦設(shè)置了,上文全都沒了揩徊,而且格式也被清空
textView.attributedText = [[NSAttributedString alloc] initWithString:@"迎面吹來淡淡的幽香"];
10腰鬼、編輯時底部彈出的視圖(默認是鍵盤)
textView.inputView = [[UIDatePicker alloc] init];
11藐握、設(shè)置彈出視圖上方的輔助視圖
textView.inputAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
12、在用戶使用虛擬鍵盤進行輸入時垃喊,全選之前的
textView.clearsOnInsertion = YES;
13猾普、代理方法
- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text;
textView.text.length ? ? 計算的長度不準確,空格也算一個字符
text ? ? 輸入的字符
- (void)textViewDidChange:(UITextView*)textView本谜;
textView.text.length ? ? 計算的長度準確初家,空格也算一個字符
14、使得輸入return時沒有任何效果
- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
if([textisEqualToString:@"\n"]) {
returnNO;
}
returnYES;
}
十三乌助、UIAlertController
1溜在、創(chuàng)建
_alert = [UIAlertController alertControllerWithTitle:@"警示框"
message:@"嫁給我好嗎?"
preferredStyle:UIAlertControllerStyleAlert];
2、給警示框添加按鈕
[_alert addAction:[UIAlertAction actionWithTitle:@"好的"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊確定后會執(zhí)行這個操作");
}]];
[_alert addAction:[UIAlertAction actionWithTitle:@"拒絕"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"被拒絕了");
}]];
3他托、模態(tài)彈出提示框
[self presentViewController:_alert animated:YES completion:nil];
彈框有輸入框
UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"驗證原密碼"message:@"為了你的數(shù)據(jù)安全掖肋,操作前請先填寫原密碼"preferredStyle:(UIAlertControllerStyleAlert)];
//創(chuàng)建文本框
[alertController
addTextFieldWithConfigurationHandler:^(UITextField*textField){
textField.
placeholder=@"請輸入您的密碼";
textField.
secureTextEntry=YES;
}];
UIAlertAction*okAction = [UIAlertActionactionWithTitle:@"確定"style:(UIAlertActionStyleDefault)handler:^(UIAlertAction*action) {
//讀取文本框的值顯示出來
UIActionSheet的使用
UIActionSheet*actionSheet = [[UIActionSheetalloc]
initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"發(fā)送位置",@"位置實時共享",nil];
[actionSheetshowInView:self.view];
實現(xiàn)點擊協(xié)議方法
- (void)actionSheet:(UIActionSheet*)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
}
搜索框
UISearchBar
==================================================================================================================================================
{
20、視圖控制器
一赏参、生命周期(需要調(diào)用父類的方法)
1志笼、視圖已經(jīng)家在完成
- (void)viewDidLoad;
2、視圖即將出現(xiàn)
- (void)viewWillAppear:(BOOL)animated
3把篓、視圖已經(jīng)出現(xiàn)
- (void)viewDidAppear:(BOOL)animated
4纫溃、視圖即將消失
- (void)viewWillDisappear:(BOOL)animated
5、視圖已經(jīng)消失
- (void)viewDidDisappear:(BOOL)animated
6韧掩、接收到內(nèi)存警告
- (void)didReceiveMemoryWarning
二紊浩、模態(tài)視圖
1、轉(zhuǎn)場風(fēng)格(枚舉)
viewControllerB.modalTransitionStyle = UIModalTransitionStylePartialCurl;
2疗锐、模態(tài)跳轉(zhuǎn)(原控制器還未被銷毀)
[self presentViewController:viewControllerB animated:YES completion:nil];
3坊谁、模態(tài)返回(本控制器被銷毀)
[self dismissViewControllerAnimated:YES completion:nil];
三、通過故事版加載視圖控制器
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyViewController *myVC = [storyBoard instantiateViewControllerWithIdentifier:@"CYC666"];
}
==================================================================================================================================================
{
21滑臊、導(dǎo)航控制器
一口芍、基本用法
1、創(chuàng)建
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc] init]];
2简珠、跳到下個導(dǎo)航控制器
[self.navigationController pushViewController:secondView animated:YES];
3阶界、返回上一個導(dǎo)航控制器
[self.navigationController popViewControllerAnimated:YES];? ? ? //返回上一級
[self.navigationController popToRootViewControllerAnimated:YES];? ? ? ? //返回第一個
4、設(shè)置當(dāng)前導(dǎo)航控制器標題
self.title = @"第一個視圖控制器";
5聋庵、當(dāng)跳轉(zhuǎn)到下一頁膘融,隱藏標簽欄(默認NO)
self.navigationController.hidesBottomBarWhenPushed = YES;
6、導(dǎo)航欄是否透明(默認透明祭玉,需要在創(chuàng)建導(dǎo)航控制器的時候設(shè)定)
navigationController.navigationBar.translucent = NO;
7氧映、導(dǎo)航欄是否隱藏(默認不隱藏,創(chuàng)建的時候設(shè)定)
navigationController.navigationBarHidden = YES;
8脱货、右邊按鈕
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:系統(tǒng)提供的按鈕類型 target:self action:響應(yīng)方法];
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithTitle:@"程" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItems = @[button1,button2岛都,…];
[self.navigationItem setRightBarButtonItem:sendItem];
[self.navigationItem setLeftBarButtonItem:closeItem];
注意:
設(shè)置好的的圖片可能是藍色的律姨,而不是所給圖像的顏色,可以根據(jù)以下代碼控制顏色
改變按鈕的顏色tintColor ? ? 改變導(dǎo)航欄顏色barTintColor
單獨設(shè)置可以使用[rightButtonsetTintColor:[UIColorblackColor]];
self.navigationController.navigationBar.tintColor= [UIColorwhiteColor];
設(shè)置按鈕的名字和顏色
self.navigationItem.backBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@“返回上一級"
style:UIBarButtonItemStyleDonetarget:self
action:@selector(backButtonItemAction:)];
self.navigationController.navigationBar.tintColor= [UIColorwhiteColor];
9臼疫、本頁的返回按鈕(在下一頁顯示)
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithImage:圖片 style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem = button3;
10择份、標題視圖(是直接賦值,而不是添加子視圖)
self.navigationItem.titleView? = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"20140210141806-1621168773.jpg.png"]];
11烫堤、注意點:
(1)荣赶、navigationItem是viewController的屬性,只有用viewController調(diào)用才會生效
(2)鸽斟、navigationBar是navigationController的屬性拔创,需要navigationController調(diào)用
12、設(shè)置導(dǎo)航欄提示用戶的內(nèi)容
self.navigationItem.prompt = @“加載”;
self.navigationItem.prompt = nil;
13富蓄、設(shè)置導(dǎo)航控制器的顏色(在創(chuàng)建nav的時候設(shè)置剩燥,或者再VC的viewDidAppear里設(shè)置,注意:不能再viewDidload里設(shè)置)
friendCircleNAV.navigationBar.barTintColor=
14立倍、獲取某個視圖所在的導(dǎo)航控制器
- (UIViewController*)viewController {
for(UIView* next = [selfsuperview]; next; next = next.superview) {
UIResponder* nextResponder = [nextnextResponder];
if([nextResponderisKindOfClass:[UINavigationControllerclass]]) {
return(UIViewController*)nextResponder;
}
}
returnnil;
15灭红、在使用navigation的pushViewController進行push的時候,兩個頁面間的動畫會出現(xiàn)卡頓一下再推出的效果帐萎,最后找出比伏,是因為iOS7 viewController背景顏色的問題,其實不是卡頓疆导,是由于透明色顏色重疊后視覺上的問題,只要在新push里設(shè)置下背景顏色就好了
}
}
16葛躏、導(dǎo)航欄控制器的左邊返回按鈕
當(dāng)前控制器頁面的返回按鈕是屬于上一個控制器的屬性澈段,所以要設(shè)置當(dāng)前控制器的返回按鈕樣式,就應(yīng)該在上一個控制器中設(shè)置舰攒,而且不應(yīng)該在原有的返回按鈕基礎(chǔ)上設(shè)置败富,應(yīng)當(dāng)創(chuàng)建新的導(dǎo)航控制器按鈕
UIBarButtonItem*backItem = [[UIBarButtonItemalloc]init];
backItem.
title=@"邦友圈";
self.navigationItem.backBarButtonItem= backItem;
17、點擊顯示摩窃、隱藏導(dǎo)航欄
self.navigationController.hidesBarsOnTap = YES;
==================================================================================================================================================
{
22兽叮、標簽控制器
一、系統(tǒng)提供的樣式
1猾愿、創(chuàng)建
UITabBarController *tabBarController = [[UITabBarController alloc] init];
2鹦聪、設(shè)置子控制器
tabBarController.viewControllers = @[viewControllerA,viewControllerB,viewControllerC,viewControllerD,viewControllerE,viewControllerF];
3、設(shè)置選中的標簽
tabBarController.selectedIndex = 2;
4蒂秘、設(shè)置單個標簽
UITabBar *tabBar = tabBarController.tabBar;
5泽本、顏色
tabBar.tintColor = [UIColor redColor];
6、item的分布樣式
tabBar.itemPositioning = UITabBarItemPositioningCentered;
7姻僧、是否透明
tabBar.translucent = NO;
8规丽、item 大小距離
tabBar.itemWidth = 150;
tabBar.itemSpacing = 150;
9蒲牧、系統(tǒng)樣式
viewControllerA.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1000];
10、協(xié)議方法(需要設(shè)置代理赌莺,協(xié)議好像已經(jīng)簽好)
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0) {
NSLog(@"即將選中");
return YES;
}
二冰抢、自定義tabbar(子類化)
1、添加視圖
self.viewControllers
2艘狭、隱藏系統(tǒng)的tabbar
self.tabBar.hidden = YES;
3挎扰、設(shè)置背景圖片(imageView,開啟用戶交互)
4缓升、for循環(huán)添加button鼓鲁,并添加同一個響應(yīng),button可以自定義
5港谊、在button下面添加選中的背景圖片骇吭,跟隨選中的button進行位移
_selectImage.center = buttonNew.center;
6、根據(jù)button的tap值切換標簽(添加動畫效果)
self.selectedIndex = sender.tag - 4980;
三歧寺、三級控制器
1燥狰、導(dǎo)航欄push后隱藏標簽欄
navigationController.hidesBottomBarWhenPushed = YES;
}
==================================================================================================================================================
{
23、滑動視圖
1斜筐、創(chuàng)建
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 300)];
2龙致、內(nèi)容尺寸
scrollView.contentSize = CGSizeMake(750, 400);
3、是否顯示滑動條
scrollView.showsHorizontalScrollIndicator = NO;? ? ? ? //水平
scrollView.showsVerticalScrollIndicator = NO;? ? ? ? ? //垂直
4顷链、滑動條樣式
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
5目代、回彈效果
scrollView.bounces = NO;
6、分頁效果
scrollView.pagingEnabled = YES;
7嗤练、點擊狀態(tài)欄回到頂部(默認YES)
scrollView.scrollsToTop = YES;
8榛了、內(nèi)容尺寸的偏移量(初始(100,100)點會被移到原點)
scrollView.contentOffset = CGPointMake(100, 100);
9煞抬、邊緣擴展(上左下右)
scrollView.contentInset = UIEdgeInsetsMake(50, 100, 200, 150);
10霜大、控制單方向移動
scrollView.directionalLockEnabled = YES;
11、當(dāng)內(nèi)容尺寸小于滑動視圖大小革答,仍然允許左右拖動(默認NO)
scrollView.alwaysBounceHorizontal = YES;
scrollView.alwaysBounceVertical = YES;
12战坤、允許滑動(默認YES)
scrollView.scrollEnabled = NO;
13、控制滑動條顯示的位置與邊界的距離(相當(dāng)于控制一個區(qū)域残拐,滑動條顯示在這個區(qū)域途茫,不能超出)
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 100, 20, 0);
14、減速速度(使滑動更加可控)
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
15蹦骑、控制最小慈省、最大縮放倍數(shù)
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 3;
16、縮放倍數(shù)
scrollView.zoomScale = 1.5;
17、方法
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated;
- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated NS_AVAILABLE_IOS(3_0)
18边败、協(xié)議方法
(1)袱衷、開始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
(2)笑窜、將要拖拽結(jié)束
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0)致燥;
(3)、已經(jīng)結(jié)束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
(4)排截、即將開始減速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
(5)嫌蚤、已經(jīng)結(jié)束減速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
19、在內(nèi)容尺寸小于frame時断傲,允許回彈效果
need-to-insert-img
20脱吱、縮放滑動視圖,必須重寫以下方法认罩,返回的view即為要縮放的view(前提是最大放大倍數(shù)必須大于最小放大倍數(shù),好像要大于一)
- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView箱蝠;
return 要縮放的圖
}