{
11、核心動(dòng)畫 需要簽協(xié)議,但是系統(tǒng)幫簽好
一、CABasicAnimation
1、創(chuàng)建基礎(chǔ)動(dòng)畫對(duì)象
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
2、設(shè)置動(dòng)畫屬性并包裝(執(zhí)行時(shí)間,起點(diǎn)(默認(rèn)0),終點(diǎn),重復(fù)執(zhí)行(MAXFLOAT非常大的浮點(diǎn)數(shù)))
animation.duration = 1;
animation.fromValue = @1;
animation.toValue = @2;
animation.repeatCount = 3;
3、將動(dòng)畫添加到視圖的layer上,key:標(biāo)識(shí)一下動(dòng)畫
[_imageView.layer addAnimation:animation forKey:@"CYC666"];
4、動(dòng)畫執(zhí)行完畢,保存最后狀態(tài)(默認(rèn)YES)+ 動(dòng)畫執(zhí)行完畢,不移除動(dòng)畫(枚舉)
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
二、關(guān)鍵幀動(dòng)畫
1、創(chuàng)建關(guān)鍵幀動(dòng)畫對(duì)象
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
2、包裝關(guān)鍵幀(點(diǎ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è)置動(dòng)畫屬性
animation.duration = 2;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
5、將動(dòng)畫添加到圖層layer上
[_imageView.layer addAnimation:animation forKey:@"CYC666"]
6、其他方法創(chuàng)建關(guān)鍵字
//路徑? animation.path = path.CGPath;
四、動(dòng)畫租
1、創(chuàng)建動(dòng)畫租
CAAnimationGroup *group=[[CAAnimationGroup alloc]init];
2、將動(dòng)畫放入動(dòng)畫租,交給動(dòng)畫組執(zhí)行
group.animations=@[an1,an2,an3];
3、設(shè)置動(dòng)畫組屬性(時(shí)長(zhǎng)、重復(fù)次數(shù))
4、將動(dòng)畫組添加到視圖layer
[_myImageView.layer addAnimation:group forKey:@"group1"];
五、轉(zhuǎn)場(chǎng)動(dòng)畫http://www.itdecent.cn/p/77e089b08b8b
1、創(chuàng)建轉(zhuǎn)場(chǎng)動(dòng)畫
CATransition *transition=[[CATransition alloc] init];
2、設(shè)置轉(zhuǎn)場(chǎng)動(dòng)畫的屬性
transition.duration=2;
transition.type=@"push"; //指定動(dòng)畫的類型
transition.subtype=@"fromTop";? ? //通過(guò)子類型,可以控制控制器切換的方向
3、添加到view的layer上
[_myImageView.layer addAnimation:transition forKey:@"tran1"];
}
==================================================================================================================================================
{
12、睡眠
1、[NSThread sleepForTimeInterval:9];
2、sleep(9);
}
==================================================================================================================================================
{
13、UIImagePicker 和 相冊(cè)
一、相片的加載方式
1.通過(guò)imageNamed:方式加載
第一次會(huì)去沙盒路徑加載圖片,將它緩存到內(nèi)存中,以后每次都從內(nèi)存中加載,速度得到提升
UIImage *image = [UIImage imageNamed:@"baby.jpg"];
2.通過(guò)包路徑加載,沙盒路徑
每一次都需要手動(dòng)加載,圖片特別大,占內(nèi)存,而且不經(jīng)常使用,就應(yīng)該使用這個(gè)方法
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"baby" ofType:@"jpg"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:data];
3.從網(wǎng)上加載圖片
加載的圖片大小有限定,超過(guò)最大值則不能下載
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、實(shí)現(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,? ? ? ?資源來(lái)自攝像頭
UIImagePickerControllerSourceTypeSavedPhotosAlbum 系統(tǒng)內(nèi)置相冊(cè)
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、實(shí)現(xiàn)代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
A、判斷資源是否來(lái)源于相冊(cè),并取出選中的相片
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary || picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
B、取出照片
UIImage *image = info[UIImagePickerControllerOriginalImage];
}
7、關(guān)閉,返回
[picker dismissViewControllerAnimated:YES completion:nil];
8、或者在點(diǎn)擊取消按鈕的協(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、實(shí)現(xiàn)代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
A、判斷相片來(lái)源是否是攝像頭
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
B、取出照片
UIImage *image = info[UIImagePickerControllerOriginalImage];
C、將照片存到相冊(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、或者在點(diǎn)擊取消按鈕的協(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、實(shí)現(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、實(shí)現(xiàn)代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
A、獲取媒體的類型
NSString *mediaType = info[UIImagePickerControllerMediaType];
B、判斷類型是否一致
if ([mediaType isEqualToString:@"public.image"]) {
C、從字典中獲取圖片對(duì)象
UIImage *image = info[UIImagePickerControllerOriginalImage];
D、當(dāng)圖片來(lái)自攝像頭,保存圖片
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//當(dāng)處于拍攝狀態(tài),將照片存入相冊(cè)
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)入庫(kù)文件
#import ? ? ? ? ? ? ? ? ? 資源
#import
#import ? ? ? ? ? ? 資源庫(kù)中某個(gè)相冊(cè)或文件夾
#import ? ? ? ? ? 整個(gè)資源庫(kù):所有視頻、照片
#import ? ? 資源描述
#import ? ? ?//必須要導(dǎo)入這個(gè)庫(kù)文件
2、創(chuàng)建資源庫(kù) 全局
_library = [[ALAssetsLibrary alloc] init];
3、通過(guò)資源庫(kù)訪問(wèn)資源,有多少個(gè)group就調(diào)用多少次block
[_library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//如果存在這個(gè)相冊(cè)就遍歷
if (group) {
//遍歷相冊(cè)中所有的資源
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
/*
if (index == 3) {? ?//遍歷4個(gè)相片
*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(@"訪問(wèn)失敗");
}];
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、通過(guò)選中的相片,進(jìn)行多選
}
==================================================================================================================================================
{
14、觸摸
一、UIResponder
1、觸摸開始
- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event {
A、系統(tǒng)會(huì)幫我們封裝好觸摸點(diǎn)對(duì)象,我們可以將它取出使用
UITouch *touch = [touches anyObject];
B、觸摸點(diǎn)的屬性
window 所在窗口
view 所在視圖
tapCount 短時(shí)間內(nèi)點(diǎn)觸摸的次數(shù),疊加
phase
C、觸摸點(diǎn)point
CGPoint locationPoint = [touch locationInView:self];? ? //獲取當(dāng)前觸摸的位置
CGPoint pLocationPoint = [touch previousLocationInView:self];? ?//獲取上一個(gè)點(diǎn)的位置
}
2、手勢(shì)觸摸并滑動(dòng)(時(shí)刻調(diào)用)
- (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event {
//事件event:加速、遠(yuǎn)程遙控、觸摸
//讓視圖隨手指移動(dòng)
UITouch *touch = [touches anyObject];
CGPoint locationPoint = [touch locationInView:self];? ? //獲取當(dāng)前觸摸的位置
CGPoint pLocationPoint = [touch previousLocationInView:self];? ?//獲取上一個(gè)點(diǎn)的位置
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、獲取觸摸點(diǎn)
UITouch *touch = [touches anyObject];
2、判斷點(diǎn)擊雙擊
if ([touch tapCount] == 1) {
//延遲0.2秒執(zhí)行單擊事件
//[self performSelector:@selector(singerTap) withObject:self afterDelay:.2];//這個(gè)方法調(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];
}
補(bǔ)充:
[A requireGestureRecognizerToFail:B]函數(shù),
它可以指定當(dāng)A手勢(shì)發(fā)生時(shí),即便A已經(jīng)滿足條件了,也不會(huì)立刻觸發(fā),會(huì)等到指定的手勢(shì)B確定失敗之后才觸發(fā)。
三、多點(diǎn)觸控
1、開啟視圖的多點(diǎn)觸控(默認(rèn)是關(guān)閉的)
self.multipleTouchEnabled = YES;
2、獲取多個(gè)手指觸摸點(diǎn)
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ù)以上兩個(gè)步奏
[子視圖? hitTest]
五、響應(yīng)者鏈
1、iOS系統(tǒng)捕獲點(diǎn)擊操作,打包成UIEven對(duì)象
2、UIApplication對(duì)象接收事件
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);
七、搖動(dòng)響應(yīng)(模擬器也支持搖一搖功能 [Hardware]-[Shake Gesture]或者command+shift+z來(lái)測(cè)試)
- (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、手勢(shì)
一、UITapGestureRecognizer 輕擊手勢(shì)
1、創(chuàng)建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapActiopn:)];
2、屬性
點(diǎn)擊的次數(shù)
tap.numberOfTapsRequired = 2;
點(diǎn)擊的手指?jìng)€(gè)數(shù)
tap.numberOfTouchesRequired = 2;
2、添加到視圖
[self.view addGestureRecognizer:tap];
3、實(shí)現(xiàn)手勢(shì)方法 state
狀態(tài)
UIGestureRecognizerStateBegan,? ? 開始
UIGestureRecognizerStateChanged, 正在改變
UIGestureRecognizerStateEnded, 結(jié)束
二、UIPinchGestureRecognizer 捏合手勢(shì)
scale 縮放倍數(shù)
velocity 速度
三、UIPanGestureRecognizer 平移
1、屬性
minimumNumberOfTouches 最少手指數(shù)
maximumNumberOfTouches 最多手指數(shù)
2、方法
- (CGPoint)translationInView:(nullable UIView *)view;? ? 獲取觸摸點(diǎn)
- (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;
- (CGPoint)velocityInView:(nullable UIView *)view;
四、UIRotationGestureRecognizer 旋轉(zhuǎn)
1、屬性
rotation 旋轉(zhuǎn)角度
velocity 速度
五、UILongPressGestureRecognizer 長(zhǎng)按
1、屬性
numberOfTapsRequired? ? 長(zhǎng)按之前需要輕擊的次數(shù)
numberOfTouchesRequired? ?長(zhǎng)按需要的手指數(shù)
minimumPressDuration? ? 長(zhǎng)按觸發(fā)的最小時(shí)間
allowableMovement? ?長(zhǎng)按時(shí)允許偏移的像素值
2、長(zhǎng)按手勢(shì)會(huì)調(diào)用兩次響應(yīng)方法,所以為了準(zhǔn)確調(diào)用,要在方法里判斷狀態(tài)(有多重狀態(tài))
if(longPressGesture.state==UIGestureRecognizerStateBegan)?{
//?do?something
}elseif(longPressGesture.state==UIGestureRecognizerStateEnded){
//?do?something
}
六、UISwipeGestureRecognizer 清掃手勢(shì)
1、屬性
direction 輕掃方向(枚舉)
numberOfTouchesRequired 輕掃必須的手指數(shù)
}
==================================================================================================================================================
{
16、多媒體 (后臺(tái)播放?)
一、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、準(zhǔn)備播放
[_audioPlayer prepareToPlay];
4、開始播放
[_audioPlayer play];
5、設(shè)置參數(shù)(當(dāng)前時(shí)間)
_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、注冊(cè)聲音成為系統(tǒng)聲音(__bridge橋接? 讓ARC管理CF框架的內(nèi)存釋放)? CFArrayRef -->NSArray? CFStringRef -->NSString
SystemSoundID soundID; //標(biāo)識(shí)符,全局變量
AudioServicesCreateSystemSoundID((__bridge CFURLRef)sysUrl, &soundID);
3、播放系統(tǒng)聲音
AudioServicesPlaySystemSound(soundID);
4、播放震動(dòng)只能真機(jī)演示
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? ? ? ? ? ? //采樣信號(hào)是整數(shù)還是浮點(diǎn)數(shù)
};
5、根據(jù)配置信息創(chuàng)建錄音對(duì)象,全局的,創(chuàng)建前先將其置為nil
_fileUrl = [NSURL fileURLWithPath:path];
NSError *error = nil;
_recoder = [[AVAudioRecorder alloc] initWithURL:_fileUrl
settings:info
error:&error];
6、準(zhǔn)備錄音
[_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 ? ? ? ? ? ? ? (定義 - 使用 - 實(shí)現(xiàn))
一、block的創(chuàng)建和使用
1、沒(méi)有返回值、沒(méi)有參數(shù)的block,去掉變量名,剩下的就是類型
A、創(chuàng)建
void (^myBlock1)();
B、實(shí)現(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對(duì)局部變量的內(nèi)存管理
1、block自帶自動(dòng)變量 調(diào)用的時(shí)候才會(huì)被創(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會(huì)保存一下外部變量的值,在block中調(diào)用的時(shí)候,使用的就是之前保存的值,并且此值之后的任何變化都不會(huì)影響
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)引用問(wèn)題
1、block使用了全局變量,那么block會(huì)持有(retain)變量所在的對(duì)象
2、_block作為當(dāng)前對(duì)象的屬性,self會(huì)持有屬性
self ---> _block
3、_block使用了全局屬性,會(huì)持有該屬性所在的對(duì)象
_block ---> self? ? ? ? 應(yīng)該斷開這條線
4、解決方法
使用__weak關(guān)鍵字創(chuàng)建一個(gè)弱引用的對(duì)象指向強(qiáng)引用的對(duì)象,使用弱引用對(duì)象去調(diào)用屬性? 不會(huì)造成循環(huán)引用
四、block變量本身的內(nèi)存管理
1、block根據(jù)在內(nèi)存中的位置,分為一下三中情況
NSConcreteStackBlock? 棧區(qū)的block? ? copy-->? ?拷貝到堆區(qū)
NSConcreteGlobalBlock? 全局區(qū)的block? copy--> 沒(méi)有任何變化
NSConcreteMallocBlock? 堆區(qū)的block? copy--> retainCount +1
2、棧區(qū)的block 局部變量,由系統(tǒng)管理內(nèi)存
3、如果使用@property聲明一個(gè)block,使用copy修飾
}
==================================================================================================================================================
{
18、Xcode添加字體
1、將字體font文件導(dǎo)入項(xiàng)目
2、在該plist里添加一個(gè)新的項(xiàng):Fonts provided by application,在里面的Item項(xiàng)里添加你的font文件的名字,如:迷你簡(jiǎn)胖娃.TTF(注意大小寫)
3、前往 項(xiàng)目 -> Build Phases -> Compile sources,將字體文件拖到這里
4、使用,不是文件名,而是用字體預(yù)覽冊(cè)打開字體時(shí)在標(biāo)題欄里顯示的文字就是你的字體的名字。
[label setFont:[UIFont fontWithName:@”font的名字” size:20]];
}
==================================================================================================================================================
{
19、常用控件
一、UILabel 文本標(biāo)簽
1、屬性
text 顯示的文本
textAlignment 對(duì)其方式(枚舉)
font 字體
textColor 字體顏色
numberofLines 文本最多行數(shù)
lineBreakMode 文本過(guò)長(zhǎng)時(shí)的截?cái)喾绞剑ㄖ挥衝umberofLines為0時(shí)才有效果)
label.lineBreakMode = NSLineBreakByCharWrapping;以字符為顯示單位顯示,后面部分省略不顯示。
label.lineBreakMode = NSLineBreakByClipping;剪切與文本寬度相同的內(nèi)容長(zhǎng)度,后半部分被刪除。
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 是否接受用戶交互(默認(rèn)是NO)
preferredMaxLayoutWidth 優(yōu)先選擇標(biāo)簽布局的最大寬度
baselineAdjustment (枚舉)如果adjustsFontSizeToFitWidth屬性設(shè)置為YES,這個(gè)屬性就來(lái)控制文本基線的行為
UIBaselineAdjustmentAlignBaselines 文本最上端與中線對(duì)齊
UIBaselineAdjustmentAlignCenters 文本中線與label中線對(duì)齊
UIBaselineAdjustmentNone 文本最低端與label中線對(duì)齊
attributedText 標(biāo)簽屬性文本,能一起設(shè)置
adjustsFontSizeToFitWidth=YES; ? ? 根據(jù)字?jǐn)?shù)調(diào)整text的大小
2、方法
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;
3、通過(guò)文本計(jì)算空間大小
NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:16]};
CGRect textRect= [self.model.text? boundingRectWithSize:CGSizeMake(kScreenWidth - 2 *kSpace, 999999)? attributes:dic context:nil];
二、UITextField 文本輸入框
1、屬性
enablesReturnKeyAutomatically 默認(rèn)為No,如果設(shè)置為Yes,文本框中沒(méi)有輸入任何字符的話,右下角的返回按鈕是disabled的
borderStyle 設(shè)置邊框樣式(枚舉),必須設(shè)置
backgroundColor 設(shè)置輸入框的背景顏色,此時(shí)設(shè)置為白色 如果使用了自定義的背景圖片邊框會(huì)被忽略掉
background 設(shè)置背景視圖,只有UITextBorderStyleNone的時(shí)候改屬性有效
disabledBackground 設(shè)置enable為no時(shí),textfield的背景
placeholder 占位符,(改變placeHolder的顏色,使用富文本,單純改變textcolor是不可以的)
clearButtonMode 輸入框中是否有個(gè)叉號(hào),在什么時(shí)候顯示,用于一次性刪除輸入框中的內(nèi)容(枚舉)
text 輸入框中的文字
secureTextEntry ? ? ? ? ? ? ? ? ? ? ? ?? ?????是否安全輸入
autocorrectionType ? ? ? ? ? ? ? ? ? ? ?????是否糾錯(cuò)(枚舉)
clearsOnBeginEditing ? ? ? ? ? ? ?? ?????是否再次編輯就清空
textAlignment ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????水平對(duì)其方式
contentVerticalAlignment ? ? ? ? ?????? 垂直對(duì)其方式
adjustsFontSizeToFitWidth 設(shè)置為YES時(shí)文本會(huì)自動(dòng)縮小以適應(yīng)文本窗口大小.默認(rèn)是保持原來(lái)大小,而讓長(zhǎng)文本滾動(dòng)
設(shè)置自動(dòng)縮小顯示的最小字體大小
text.minimumFontSize = 20;
keyboardType 設(shè)置鍵盤樣式(枚舉)
autocapitalizationType 首字母大寫模式(枚舉)
returnKeyType return按鈕樣式(枚舉)
keyboardAppearance 鍵盤的外觀(枚舉)
tintColor 設(shè)置光標(biāo)的顏色
rightView 最右側(cè)加圖片是以下代碼 左側(cè)類似
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
text.rightView=image;
text.rightViewMode = UITextFieldViewModeAlways;
editing 是否允許編輯
delegate 代理,對(duì)應(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)用戶使用自動(dòng)更正功能,把輸入的文字修改為推薦的文字時(shí),就會(huì)調(diào)用這個(gè)方法。
//這對(duì)于想要加入撤銷選項(xiàng)的應(yīng)用程序特別有用
//可以跟蹤字段內(nèi)所做的最后一次修改,也可以對(duì)所有編輯做日志記錄,用作審計(jì)用途。
//要防止文字被改變可以返回NO
//這個(gè)方法的參數(shù)中有一個(gè)NSRange對(duì)象,指明了被改變文字的位置,建議修改的文本也在其中
- (BOOL)textFieldShouldClear:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
[textField endEditing:YES];? ? ? ?//點(diǎn)擊 return 時(shí)收起鍵盤
3、鍵盤相關(guān)的通知事件
UITextFieldTextDidBeginEditingNotification? ? //已經(jīng)開始編輯
UITextFieldTextDidChangeNotification? ? ? ? //已經(jīng)開始改變
UITextFieldTextDidEndEditingNotification? ? //已經(jīng)結(jié)束編輯
UIKeyboardWillShowNotification? ?//鍵盤顯示之前發(fā)送
UIKeyboardDidShowNotification? ? //鍵盤顯示之后發(fā)送
UIKeyboardWillHideNotification? ?//鍵盤隱藏之前發(fā)送
UIKeyboardDidHideNotification? ? //鍵盤隱藏之后發(fā)送
4、失去焦點(diǎn)、光標(biāo)的問(wèn)題:
看看tintColor是否設(shè)置了,很有可能是因?yàn)閠intColor是白色的,你看不見(jiàn)看不見(jiàn)
三、UIImageView
1、屬性
highlightedImage 高亮圖片
animationImages 設(shè)置序列幀動(dòng)畫的圖片數(shù)組
highlightedAnimationImages 設(shè)置高亮狀態(tài)下序列幀動(dòng)畫的圖片數(shù)組(必須設(shè)置imageView為高亮狀態(tài))
animationDuration 設(shè)置序列幀動(dòng)畫播放的時(shí)長(zhǎng)
animationRepeatCount 設(shè)置序列幀動(dòng)畫播放的次數(shù)
userInteractionEnabled 設(shè)置是否允許用戶交互,默認(rèn)不允許用戶交互(重要)
highlighted 設(shè)置是否為高亮狀態(tài),默認(rèn)為普通狀態(tài)
2、方法
[imageView startAnimating];? ? ?//開啟圖片動(dòng)畫
- (void)startAnimating;
- (void)stopAnimating;
- (BOOL)isAnimating;
3、拉伸image(左右端蓋,上下端蓋,只對(duì)那一像素進(jìn)行拉伸,其他像素不變)
特別注意:image本身不應(yīng)該大于視圖的大小。如果image比視圖還大,那還拉伸個(gè)屁
兩個(gè)數(shù)值都是像素的大小,可以用image.size.width*0.5這種方式去設(shè)置
UIImage *newImage = [image stretchableImageWithLeftCapWidth:0 topCapHeight:4];
4、image的填充模式(填充模式必須在設(shè)置圖片之前設(shè)置,否則會(huì)出錯(cuò)或設(shè)置不湊效)
imageView.contentMode = UIViewContentModeScaleAspectFit;? ? 不縮放,一邊剛好一邊不夠
UIViewContentModeScaleToFill? ? 填充
UIViewContentModeScaleAspectFill? ? ?等比例填滿(nice)
5、壓縮image
NSData*thumbData =UIImageJPEGRepresentation(image,0.5);
6、設(shè)置圓角,必須設(shè)置剪切模式,不然只有描邊,沒(méi)有圓角
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)按鈕禁用的情況下,圖像的顏色會(huì)被畫深一點(diǎn),默認(rèn)為YES。
3.adjustsImageWhenHighlighted
當(dāng)按鈕高亮的情況下,圖像的顏色會(huì)被畫深一點(diǎn),默認(rèn)為YES。
4、方法
+ (instancetype)buttonWithType:(UIButtonType)buttonType? ? ?創(chuàng)建按鈕
5、設(shè)置按鈕內(nèi)容的對(duì)其方式
addressBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;? ? //左對(duì)齊
addressBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;? ? ? ? //向上對(duì)其
五、UIActivityIndicatorView 活動(dòng)指示器
1、創(chuàng)建,確定樣式。不必設(shè)置frame,因?yàn)閒rame只影響背景的大小,指示器大小不變
_activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
2、停止動(dòng)畫后隱藏,默認(rèn)是YES
_activity.hidesWhenStopped = NO;
3、開啟動(dòng)畫
[_activity startAnimating];
4、停止動(dòng)畫
[_activity stopAnimating];
5、是否在動(dòng)
[_activity isAnimating];
六、UIAlertView 提示
1、創(chuàng)建,注意UIAlertView調(diào)用show顯示出來(lái)的時(shí)候,系統(tǒng)會(huì)自動(dòng)強(qiáng)引用它,不會(huì)被釋放。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"麻煩開始了嗎?"
message:@"麻煩不斷"
delegate:self
cancelButtonTitle:@"拒絕"
otherButtonTitles:@"默默忍受",@"我不想說(shuō)話",@"嘿嘿,我們豎著排啦", nil];
2、顯示
[alert show];
3、標(biāo)題
alert.title = @"點(diǎn)擊這個(gè)昵稱灰會(huì)變美哦";
4、信息
alert.message = @"你只是個(gè)小馬仔,永遠(yuǎn)都是";
5、按鈕個(gè)數(shù)(只讀)
NSLog(@"%ld", alert.numberOfButtons);
6、取消按鈕的index,可以自己設(shè)置
alert.cancelButtonIndex = 1;
7、模式,沒(méi)效果
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
8、代理、協(xié)議、協(xié)議方法
當(dāng)點(diǎn)擊了按鈕
- (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è)置時(shí)區(qū)
timeZone
6、設(shè)置日歷(當(dāng)前)
[datePicker setCalendar:[NSCalendar currentCalendar]];
7、最小日期
minimumDate
8、最大日期
maxinumDate
9、手動(dòng)調(diào)整轉(zhuǎn)到日期
maxinumDate
10、監(jiān)聽值改變
[date addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
八、UIPageControl 頁(yè)碼指示器
1、創(chuàng)建
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 600, 150, 50)];
2、設(shè)置控制的頁(yè)數(shù)
_pageControl.numberOfPages = 6;
3、當(dāng)前頁(yè)數(shù)
_pageControl.currentPage = 3;
4、當(dāng)只有一頁(yè)時(shí),不顯示指示器(默認(rèn)NO)
_pageControl.hidesForSinglePage = YES;
5、設(shè)置頁(yè)碼指示器顏色
_pageControl.pageIndicatorTintColor = [UIColor redColor];
6、設(shè)置當(dāng)前頁(yè)碼指示器的顏色
_pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];
7、添加監(jiān)聽事件(手勢(shì)觸控才會(huì)觸發(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、樣式(無(wú)效)
segment.segmentedControlStyle = UISegmentedControlStyleBar;
4、顏色樣式(有像素的地方的顏色)
segment.tintColor = [UIColor cyanColor];
5、設(shè)置在點(diǎn)擊后是否恢復(fù)原樣(效果不好)
segment.momentary = YES;
6、選中項(xiàng)
segment.selectedSegmentIndex = 1;
7、設(shè)置標(biāo)題
[segment setTitle:@"二二" forSegmentAtIndex:1];
8、獲取標(biāo)題
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;? ? //使某一項(xiàng)不可選
十、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,在拖動(dòng)滑塊的任何時(shí)候,滑塊的值都會(huì)改變。默認(rèn)設(shè)置為YES
slider.continuous = NO;
6、給最左邊添加圖片,圖片不在滑動(dòng)條上
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
十一、UISwitch 開關(guān)
1、ON時(shí)的顏色 onTintColor
2、OFF時(shí)的顏色 tintColor
3、onImage
4、offImage
5、設(shè)置ON
swithImage.on = YES;
6、thumbTintColor 拇指顏色
7、增加事件響應(yīng)機(jī)制
十二、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、對(duì)其方式
textView.textAlignment = NSTextAlignmentLeft;
7、是否可以被輸入(默認(rèn)YES),當(dāng)設(shè)置為NO,不能編輯文字,但是能選中
textView.editable = NO;
8、當(dāng)設(shè)置為NO以后,就不能編輯了,不能選中文字
textView.selectable = NO;
9、設(shè)置文本,一旦設(shè)置了,上文全都沒(méi)了,而且格式也被清空
textView.attributedText = [[NSAttributedString alloc] initWithString:@"迎面吹來(lái)淡淡的幽香"];
10、編輯時(shí)底部彈出的視圖(默認(rèn)是鍵盤)
textView.inputView = [[UIDatePicker alloc] init];
11、設(shè)置彈出視圖上方的輔助視圖
textView.inputAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
12、在用戶使用虛擬鍵盤進(jìn)行輸入時(shí),全選之前的
textView.clearsOnInsertion = YES;
13、代理方法
- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text;
textView.text.length ? ? 計(jì)算的長(zhǎng)度不準(zhǔn)確,空格也算一個(gè)字符
text ? ? 輸入的字符
- (void)textViewDidChange:(UITextView*)textView;
textView.text.length ? ? 計(jì)算的長(zhǎng)度準(zhǔn)確,空格也算一個(gè)字符
14、使得輸入return時(shí)沒(méi)有任何效果
- (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(@"點(diǎn)擊確定后會(huì)執(zhí)行這個(gè)操作");
}]];
[_alert addAction:[UIAlertAction actionWithTitle:@"拒絕"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"被拒絕了");
}]];
3、模態(tài)彈出提示框
[self presentViewController:_alert animated:YES completion:nil];
彈框有輸入框
UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"驗(yàn)證原密碼"message:@"為了你的數(shù)據(jù)安全,操作前請(qǐng)先填寫原密碼"preferredStyle:(UIAlertControllerStyleAlert)];
//創(chuàng)建文本框
[alertController
addTextFieldWithConfigurationHandler:^(UITextField*textField){
textField.
placeholder=@"請(qǐng)輸入您的密碼";
textField.
secureTextEntry=YES;
}];
UIAlertAction*okAction = [UIAlertActionactionWithTitle:@"確定"style:(UIAlertActionStyleDefault)handler:^(UIAlertAction*action) {
//讀取文本框的值顯示出來(lái)
UIActionSheet的使用
UIActionSheet*actionSheet = [[UIActionSheetalloc]
initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"發(fā)送位置",@"位置實(shí)時(shí)共享",nil];
[actionSheetshowInView:self.view];
實(shí)現(xiàn)點(diǎ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)場(chǎng)風(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];
三、通過(guò)故事版加載視圖控制器
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、跳到下個(gè)導(dǎo)航控制器
[self.navigationController pushViewController:secondView animated:YES];
3、返回上一個(gè)導(dǎo)航控制器
[self.navigationController popViewControllerAnimated:YES];? ? ? //返回上一級(jí)
[self.navigationController popToRootViewControllerAnimated:YES];? ? ? ? //返回第一個(gè)
4、設(shè)置當(dāng)前導(dǎo)航控制器標(biāo)題
self.title = @"第一個(gè)視圖控制器";
5、當(dāng)跳轉(zhuǎn)到下一頁(yè),隱藏標(biāo)簽欄(默認(rèn)NO)
self.navigationController.hidesBottomBarWhenPushed = YES;
6、導(dǎo)航欄是否透明(默認(rèn)透明,需要在創(chuàng)建導(dǎo)航控制器的時(shí)候設(shè)定)
navigationController.navigationBar.translucent = NO;
7、導(dǎo)航欄是否隱藏(默認(rèn)不隱藏,創(chuàng)建的時(shí)候設(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è)置好的的圖片可能是藍(lán)色的,而不是所給圖像的顏色,可以根據(jù)以下代碼控制顏色
改變按鈕的顏色tintColor ? ? 改變導(dǎo)航欄顏色barTintColor
單獨(dú)設(shè)置可以使用[rightButtonsetTintColor:[UIColorblackColor]];
self.navigationController.navigationBar.tintColor= [UIColorwhiteColor];
設(shè)置按鈕的名字和顏色
self.navigationItem.backBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@“返回上一級(jí)"
style:UIBarButtonItemStyleDonetarget:self
action:@selector(backButtonItemAction:)];
self.navigationController.navigationBar.tintColor= [UIColorwhiteColor];
9、本頁(yè)的返回按鈕(在下一頁(yè)顯示)
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithImage:圖片 style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem = button3;
10、標(biāo)題視圖(是直接賦值,而不是添加子視圖)
self.navigationItem.titleView? = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"20140210141806-1621168773.jpg.png"]];
11、注意點(diǎn):
(1)、navigationItem是viewController的屬性,只有用viewController調(diào)用才會(huì)生效
(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í)候設(shè)置,或者再VC的viewDidAppear里設(shè)置,注意:不能再viewDidload里設(shè)置)
friendCircleNAV.navigationBar.barTintColor=
14、獲取某個(gè)視圖所在的導(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進(jìn)行push的時(shí)候,兩個(gè)頁(yè)面間的動(dòng)畫會(huì)出現(xiàn)卡頓一下再推出的效果,最后找出,是因?yàn)閕OS7 viewController背景顏色的問(wèn)題,其實(shí)不是卡頓,是由于透明色顏色重疊后視覺(jué)上的問(wèn)題,只要在新push里設(shè)置下背景顏色就好了
}
}
16、導(dǎo)航欄控制器的左邊返回按鈕
當(dāng)前控制器頁(yè)面的返回按鈕是屬于上一個(gè)控制器的屬性,所以要設(shè)置當(dāng)前控制器的返回按鈕樣式,就應(yīng)該在上一個(gè)控制器中設(shè)置,而且不應(yīng)該在原有的返回按鈕基礎(chǔ)上設(shè)置,應(yīng)當(dāng)創(chuàng)建新的導(dǎo)航控制器按鈕
UIBarButtonItem*backItem = [[UIBarButtonItemalloc]init];
backItem.
title=@"邦友圈";
self.navigationItem.backBarButtonItem= backItem;
17、點(diǎn)擊顯示、隱藏導(dǎo)航欄
self.navigationController.hidesBarsOnTap = YES;
==================================================================================================================================================
{
22、標(biāo)簽控制器
一、系統(tǒng)提供的樣式
1、創(chuàng)建
UITabBarController *tabBarController = [[UITabBarController alloc] init];
2、設(shè)置子控制器
tabBarController.viewControllers = @[viewControllerA,viewControllerB,viewControllerC,viewControllerD,viewControllerE,viewControllerF];
3、設(shè)置選中的標(biāo)簽
tabBarController.selectedIndex = 2;
4、設(shè)置單個(gè)標(biāo)簽
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,并添加同一個(gè)響應(yīng),button可以自定義
5、在button下面添加選中的背景圖片,跟隨選中的button進(jìn)行位移
_selectImage.center = buttonNew.center;
6、根據(jù)button的tap值切換標(biāo)簽(添加動(dòng)畫效果)
self.selectedIndex = sender.tag - 4980;
三、三級(jí)控制器
1、導(dǎo)航欄push后隱藏標(biāo)簽欄
navigationController.hidesBottomBarWhenPushed = YES;
}
==================================================================================================================================================
{
23、滑動(dòng)視圖
1、創(chuàng)建
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 375, 300)];
2、內(nèi)容尺寸
scrollView.contentSize = CGSizeMake(750, 400);
3、是否顯示滑動(dòng)條
scrollView.showsHorizontalScrollIndicator = NO;? ? ? ? //水平
scrollView.showsVerticalScrollIndicator = NO;? ? ? ? ? //垂直
4、滑動(dòng)條樣式
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
5、回彈效果
scrollView.bounces = NO;
6、分頁(yè)效果
scrollView.pagingEnabled = YES;
7、點(diǎn)擊狀態(tài)欄回到頂部(默認(rèn)YES)
scrollView.scrollsToTop = YES;
8、內(nèi)容尺寸的偏移量(初始(100,100)點(diǎn)會(huì)被移到原點(diǎn))
scrollView.contentOffset = CGPointMake(100, 100);
9、邊緣擴(kuò)展(上左下右)
scrollView.contentInset = UIEdgeInsetsMake(50, 100, 200, 150);
10、控制單方向移動(dòng)
scrollView.directionalLockEnabled = YES;
11、當(dāng)內(nèi)容尺寸小于滑動(dòng)視圖大小,仍然允許左右拖動(dòng)(默認(rèn)NO)
scrollView.alwaysBounceHorizontal = YES;
scrollView.alwaysBounceVertical = YES;
12、允許滑動(dòng)(默認(rèn)YES)
scrollView.scrollEnabled = NO;
13、控制滑動(dòng)條顯示的位置與邊界的距離(相當(dāng)于控制一個(gè)區(qū)域,滑動(dòng)條顯示在這個(gè)區(qū)域,不能超出)
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 100, 20, 0);
14、減速速度(使滑動(dòng)更加可控)
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時(shí),允許回彈效果
need-to-insert-img
20、縮放滑動(dòng)視圖,必須重寫以下方法,返回的view即為要縮放的view(前提是最大放大倍數(shù)必須大于最小放大倍數(shù),好像要大于一)
- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView;
return 要縮放的圖
}