一.UIPikerView的屬性
1.?????numberOfComponents:返回UIPickerView當(dāng)前的列數(shù)
NSIntegernum =_pickerView.numberOfComponents;
NSLog(@"%d", num);
2. -(NSInteger)numberOfRowsInComponent:(NSInteger)component; 返回component列中有多少行。
NSIntegernumInCp = [_pickerViewnumberOfRowsInComponent:0];
NSLog(@"%d",numInCp);
3. - ?(CGSize)rowSizeForComponent:(NSInteger)component;返回component中一行的尺寸垂蜗。
CGSizesize = [_pickerViewrowSizeForComponent:0];
NSLog(@"%@",NSStringFromCGSize(size));
2.0 設(shè)置UIPickerView代理
_pickerView.delegate=self;
//設(shè)置UIPickView每行顯示的內(nèi)容
2.1 - (NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return@"showData";
}
2.2?? - (UIView*)pickerView:(UIPickerView*)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView*)view;
// 返回一個視圖粤策,用來設(shè)置pickerView的每行顯示的內(nèi)容抗斤。
-(UIView*)pickerView:(UIPickerView*)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView*)view
{
UIView*myView=[[UIViewalloc]init];
myView.backgroundColor= [UIColorredColor];
returnmyView;
}
效果:
3.?????dataSource:數(shù)據(jù)源
#pragma mark? -dataSource method
//設(shè)置每列顯示3行
- (NSInteger)pickerView:(UIPickerView*)pickerViewnumberOfRowsInComponent:(NSInteger)component
{
return3;
}
//設(shè)置顯示2列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
return2;
}
4. showsSelectionIndicator:是否顯示指示器登钥,默認為NO
_pickerView.showsSelectionIndicator=NO;
注意:設(shè)置UIPickerView的行數(shù)與列數(shù)需要設(shè)置數(shù)據(jù)源沥寥,遵守UIPickerViewDataSource秀姐,設(shè)置UIPickerView的內(nèi)容需要設(shè)置代理埂奈,并且遵守代理方法UIPickerViewDelegate。
當(dāng)點擊UIPickerView的某一列中某一行的時候奈应,就會調(diào)用這個方法澜掩。
- (CGFloat)pickerView:(UIPickerView*)pickerView
rowHeightForComponent:(NSInteger)component;
一旦調(diào)用了這個方法,就會重新給數(shù)據(jù)源發(fā)送消息計算這列的行數(shù)杖挣、重新給代理發(fā)送消息獲得這列的內(nèi)容
[pickerViewreloadComponent:1];
- (void)reloadAllComponents;
-(NSInteger)selectedRowInComponent:(NSInteger)component;
1.?????numberOfPages // 設(shè)置有多少頁 默認為0
// 2)設(shè)置頁數(shù)
[pageControlsetNumberOfPages:kImageCount];
2.?????currentPage? // 設(shè)置當(dāng)前頁
[pageControlsetCurrentPage:0];
3.?????pageIndicatorTintColor // 設(shè)置頁碼指示器顏色
[pageControlsetPageIndicatorTintColor:[UIColorblackColor]];
4.?????currentPageIndicatorTintColor// 設(shè)置當(dāng)前頁碼指示器顏色
[pageControlsetCurrentPageIndicatorTintColor:[UIColorredColor]];
5.添加分頁控件的監(jiān)聽事件(監(jiān)聽值改變事件)
[pageControladdTarget:selfaction:@selector(pageChanged:)forControlEvents:UIControlEventValueChanged];
UIImageView*_imageView = [[UIImageViewalloc]init];
_imageView.image= [UIImageimageNamed:@"me.png"];
2.highlightedImage 設(shè)置高亮狀態(tài)下顯示的圖片
_imageView.highlightedImage= [UIImageimageNamed:@"other.png"];
3.animationImages 設(shè)置序列幀動畫的圖片數(shù)組
[_imageViewsetAnimationImages:[NSArrayarray]];
4.highlightedAnimationImages設(shè)置高亮狀態(tài)下序列幀動畫的圖片數(shù)組
[_imageViewsetHighlightedAnimationImages:[NSArrayarray]];
5.animationDuration 設(shè)置序列幀動畫播放的時常
[_imageViewsetAnimationDuration:0.3f];
6.animationRepeatCount 設(shè)置序列幀動畫播放的次數(shù)
[_imageViewsetAnimationRepeatCount:2];
7.userInteractionEnabled設(shè)置是否允許用戶交互惩妇,默認不允許用戶交互
[_imageViewsetUserInteractionEnabled:YES];
8.highlighted 設(shè)置是否為高亮狀態(tài)株汉,默認為普通狀態(tài)
_imageView.highlightedImage= [UIImageimageNamed:@"other.png"];
[_imageViewsetHighlighted:YES];
注意的是在highlighted狀態(tài)下設(shè)置的圖片與序列幀動畫要顯示,必須同時設(shè)置UIImageView的狀態(tài)為highlighted歌殃。
//檢查照片源是否可用
[UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]2.allowsEditing 默認NO
是否允許編輯
允許編輯.
[imagePickersetAllowsEditing:YES];
設(shè)置UIImagePicker的最大視頻持續(xù)時間.默認10分鐘
4. + availableMediaTypesForSourceType: // 指定源可用的媒體種類
//獲得相機模式下支持的媒體類型
NSArray*availableMediaTypes = [UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
設(shè)置UIImagePicker照片源類型,默認有3種氓皱。
照片源類型
UIImagePickerControllerSourceTypeCamera照相機
UIImagePickerControllerSourceTypePhotoLibrary照片庫(通過同步存放的路召,用戶不能刪除)
UIImagePickerControllerSourceTypeSavedPhotosAlbum保存的照片(通過拍照或者截屏保存的,用戶可以刪除)
檢查指定源是否可用.isSourceTypeAvailable:方法.
檢查可用媒體(視頻還是只能是圖片)availableMediaTypesForSourceType:方法.
設(shè)置界面媒體屬性mediaTypes property.
顯示界面使用presentViewController:animated:completion:方法.iPad中是popover形式.需要確保sourceType有效.
相關(guān)操作,移除視圖.
如果想創(chuàng)建一個完全自定義界面的imagepicker來瀏覽圖片,使用 AssetsLibrary Framework Reference中的類. (AV Foundation Programming Guide 中的 “MediaCapture and Access to Camera” )
+ availableMediaTypesForSourceType: // 指定源可用的媒體種類
+ isSourceTypeAvailable: // 指定源是否在設(shè)備上可用
sourceType
// 運行相關(guān)接口前需要指明源類型.必須有效,否則拋出異常. picker已經(jīng)顯示的時候改變這個值,picker會相應(yīng)改變來適應(yīng).默認UIImagePickerControllerSourceTypePhotoLibrary.
allowsEditing //是否可編輯
delegate
mediaTypes
// 指示picker中顯示的媒體類型.設(shè)置每種類型之前應(yīng)用availableMediaTypesForSourceType:檢查一下.如果為空或者array中類型都不可用,會發(fā)生異常.默認kUTTypeImage, 只能顯示圖片.
videoQuality //視頻拍攝選取時的編碼質(zhì)量.只有mediaTypes包含kUTTypeMovie時有效.
videoMaximumDuration //秒,video最大記錄時間,默認10分鐘.只用當(dāng)mediaTypes包含kUTTypeMovie時有效.
showsCameraControls
// 指示 picker 是否顯示默認的cameracontrols.默認是YES,設(shè)置成NO隱藏默認的controls來使用自定義的overlayview.(從而可以實現(xiàn)多選而不是選一張picker就dismiss了).只有UIImagePickerControllerSourceTypeCamera源有效,否則NSInvalidArgumentException異常.
cameraOverlayView
//自定義的用于顯示在picker之上的view.只有當(dāng)源是UIImagePickerControllerSourceTypeCamera時有效.其他時候使用拋出NSInvalidArgumentException異常.
cameraViewTransform
//預(yù)先動畫.只影響預(yù)先圖像,對自定義的overlayview和默認的picker無效.只用當(dāng)picker的源是UIImagePickerControllerSourceTypeCamera時有效,否則NSInvalidArgumentException異常.
– takePicture
//使用攝像頭選取一個圖片波材。自定義overlay可以多選股淡。已經(jīng)有圖片正在選取是調(diào)用無效,必須要等delegate收到imagePickerController:didFinishPickingMediaWithInfo:消息后才能再次選取廷区。非UIImagePickerControllerSourceTypeCamera源會導(dǎo)致異常唯灵。
– startVideoCapture
– stopVideoCapture
//結(jié)束視頻選取,之后系統(tǒng)調(diào)用delegate的imagePickerController:didFinishPickingMediaWithInfo:方法隙轻。
cameraDevice //使用的鏡頭(默認后置的)
+ isCameraDeviceAvailable: // 攝像設(shè)備是否可用.
+ availableCaptureModesForCameraDevice: // 設(shè)備可用的選取模式
cameraCaptureMode //相機捕獲模式
cameraFlashMode //閃光燈模式(默認自動)
+ isFlashAvailableForCameraDevice: // 是否有閃光能力
13.UIImagePickerControllerDelegate
使用UIImageWriteToSavedPhotosAlbum保存圖像,UISaveVideoAtPathToSavedPhotosAlbum保存視頻. 4.0后使用writeImageToSavedPhotosAlbum:metadata:completionBlock:保存元數(shù)據(jù).
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//包含選擇的圖片或者一個視頻的URL,詳見“EditingInformation Keys.”
//如果是設(shè)置可編輯屬性,那么picker會預(yù)顯示選中的媒體,編輯后的與初始的都會保存在info中.
– imagePickerControllerDidCancel:
– imagePickerController:didFinishPickingImage:editingInfo://DeprecatediniOS3.0
NSString *const UIImagePickerControllerMediaType;// 媒體類型
NSString *const UIImagePickerControllerOriginalImage;// 原始未編輯的圖像
NSString *const UIImagePickerControllerEditedImage;// 編輯后的圖像
NSString *const UIImagePickerControllerCropRect;// 源圖像可編輯(有效?)區(qū)域
NSString *const UIImagePickerControllerMediaURL;// 視頻的路徑
NSString *const UIImagePickerControllerReferenceURL;// 原始選擇項的URL
NSString *const UIImagePickerControllerMediaMetadata;// 只有在使用攝像頭并且是圖像類型的時候有效.包含選擇圖像信息的字典類型
14. UIImagePickerController小例子
UIImagePickerController的代理需要遵守這兩個協(xié)議.
#pragma mark選擇照片
- (void)selectPhoto
{
// 1.首先判斷照片源是否可用
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
// 0)實例化控制器
UIImagePickerController*picker = [[UIImagePickerControlleralloc]init];
// 1)設(shè)置照片源
[pickersetSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// 2)設(shè)置允許修改
[pickersetAllowsEditing:YES];
// 3)設(shè)置代理
[pickersetDelegate:self];
// 4)顯示控制器
[selfpresentViewController:pickeranimated:YEScompletion:nil];
}else{
NSLog(@"照片源不可用");
}
}
#pragma mark - imagePicker代理方法
- (void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage*image = info[@"UIImagePickerControllerEditedImage"];
[_imageButtonsetImage:imageforState:UIControlStateNormal];
//關(guān)閉照片選擇器
[selfdismissViewControllerAnimated:YEScompletion:nil];
//需要將照片保存至應(yīng)用程序沙箱早敬,由于涉及到數(shù)據(jù)存儲忌傻,同時與界面無關(guān)
//可以使用多線程來保存圖像
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
//保存圖像
// 1.取圖像路徑
NSArray*docs =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*imagePath = [docs[0]stringByAppendingPathComponent:@"abc.png"];
// 2.轉(zhuǎn)換成NSData保存
NSData*imageData =UIImagePNGRepresentation(image);
[imageDatawriteToFile:imagePathatomically:YES];
});
}
設(shè)置DatePicker的地區(qū),即設(shè)置DatePicker顯示的語言。
// 1.跟蹤所有可用的地區(qū)搞监,取出想要的地區(qū)
NSLog(@"%@", [NSLocaleavailableLocaleIdentifiers]);
// 2.設(shè)置日期選擇控件的地區(qū)
[datePickersetLocale:[[NSLocale
alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
效果:
// 2)設(shè)置日期選擇控件的地區(qū)
[datePickersetLocale:[[NSLocalealloc]initWithLocaleIdentifier:@"en_SC"]];
效果:
設(shè)置DatePicker的日歷水孩。
默認為當(dāng)天。
[datePickersetCalendar:[NSCalendarcurrentCalendar]];
設(shè)置DatePicker的時區(qū)琐驴。
默認為設(shè)置為:[datePickersetTimeZone:[NSTimeZonedefaultTimeZone]];
設(shè)置DatePicker的日期俘种。
默認設(shè)置為: [datePickersetDate:[NSDatedate]];
設(shè)置DatePicker的允許的最小日期。
設(shè)置DatePicker的允許的最大日期绝淡。
設(shè)置DatePicker的倒計時間.
// 1)設(shè)置日期選擇的模
[self.datePickersetDatePickerMode:UIDatePickerModeCountDownTimer];
// 2)設(shè)置倒計時的時長
//注意:設(shè)置倒計時時長需要在確定模式之后指定
//倒計時的時長宙刘,以秒為單位
[self.datePickersetCountDownDuration:10*60];
效果:
你可以將分鐘表盤設(shè)置為以不同的時間間隔來顯示分鐘,前提是該間隔要能夠讓60整除牢酵。默認間隔是一分鐘悬包。如果要使用不同的間隔,需要改變 minuteInterval屬性:
//設(shè)置分鐘間隔
datePicker.minuteInterval=15;
9.1
UIDatePickerModeTime,//Displays hour, minute, and optionally AM/PM designation depending on the localesetting (e.g. 6 | 53 | PM)
顯示小時馍乙,分鐘和AM/PM,這個的名稱是根據(jù)本地設(shè)置的
[datePickersetDatePickerMode:UIDatePickerModeTime];
效果圖:
9.2
UIDatePickerModeDate,// Displays month, day, and year depending on the locale setting (e.g.November | 15 | 2007)
顯示年月日布近,名稱根據(jù)本地設(shè)置的
[datePickersetDatePickerMode:UIDatePickerModeDate];
效果圖:
9.3 默認是顯示這種模式
UIDatePickerModeDateAndTime,// Displays date, hour, minute, and optionally AM/PMdesignation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
顯示日期,小時丝格,分鐘撑瞧,和AM/PM,名稱是根據(jù)本地設(shè)置的
[datePickersetDatePickerMode:UIDatePickerModeDateAndTime];
效果圖:
9.4
UIDatePickerModeCountDownTimer// Displays hour and minute (e.g. 1 | 53)
顯示小時和分鐘
[datePickersetDatePickerMode:UIDatePickerModeCountDownTimer];
//不用設(shè)置寬高显蝌,因為它的寬高是固定的
UIDatePicker*datePicker = [[UIDatePickeralloc]init];
//設(shè)置區(qū)域為中國簡體中文
datePicker.locale= [[NSLocale alloc]
initWithLocaleIdentifier:@"zh_CN"];
//設(shè)置picker的顯示模式:只顯示日期
datePicker.datePickerMode =UIDatePickerModeDate;
10.3UIDatePicker需要監(jiān)聽值的改變
[datePickeraddTarget:selfaction:@selector(dateChange:)
forControlEvents:UIControlEventValueChanged];
你可以通過設(shè)置mininumDate和 maxinumDate 屬性,來指定使用的日期范圍曼尊。如果用戶試圖滾動到超出這一范圍的日期酬诀,表盤會回滾到最近的有效日期。兩個方法都需要NSDate 對象作參數(shù):
1.NSDate*?minDate?=?[[NSDate?alloc]initWithString:@"1900-01-01?00:00:00?-0500"];
2.NSDate*?maxDate?=?[[NSDate?alloc]initWithString:@"2099-01-01?00:00:00?-0500"];
3.
4.datePicker.minimumDate?=?minDate;
5.datePicker.maximumDate?=?maxDate;
11.2 如果兩個日期范圍屬性中任何一個未被設(shè)置骆撇,則默認行為將會允許用戶選擇過去或未來的任意日期瞒御。這在某些情況下很有用處,比如艾船,當(dāng)選擇生日時葵腹,可以是過去的任意日期高每,但終止與當(dāng)前日期屿岂。如果你希望設(shè)置默認顯示的日期,可以使用date屬性:
1.datePicker.date?=?minDate;
11.3 此外鲸匿,你還可以用 setDate 方法爷怀。如果選擇了使用動畫,則表盤會滾動到你指定的日期:
1.[?datePicker?setDate:maxDate?animated:YES];
1.?????activityIndicatorViewStyle
設(shè)置指示器的樣式
UIActivityIndicatorViewStyleWhiteLarge
UIActivityIndicatorViewStyleWhite ?(默認樣式)
UIActivityIndicatorViewStyleGray
當(dāng)停止動畫的時候带欢,是否隱藏运授。默認為YES烤惊。
3. 實例化指示器對象,根據(jù)樣式設(shè)置尺寸吁朦,不需要手動設(shè)置柒室。
-(id)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style;
- (void)startAnimating;
- (void)stopAnimating;
- (BOOL)isAnimating;
7. UIActivityIndicatorView使用注意
7.1初始化的時候不需要設(shè)置尺寸,設(shè)置尺寸也沒有效果逗宜。
7.2 必須調(diào)用startAnimating才會顯示UIActivityIndicatorView
//初始化指示器
UIActivityIndicatorView*indicator = [[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//設(shè)置指示器位置
indicator.center=CGPointMake(self.view.frame.size.width*0.5,self.view.frame.size.height*0.5);
//開啟動畫雄右,必須調(diào)用,否則無法顯示
[indicatorstartAnimating];
[self.viewaddSubview:indicator];
1.NSKernAttributeName: @10 調(diào)整字句 kerning 字句調(diào)整
2.NSFontAttributeName: [UIFont systemFontOfSize:_fontSize] 設(shè)置字體
3.NSForegroundColorAttributeName:[UIColor redColor] 設(shè)置文字顏色
4.NSParagraphStyleAttributeName: paragraph 設(shè)置段落樣式
5.NSMutableParagraphStyle*paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment= NSTextAlignmentCenter;
6.NSBackgroundColorAttributeName:[UIColor blackColor] 設(shè)置背景顏色
7.NSStrokeColorAttributeName設(shè)置文字描邊顏色纺讲,需要和NSStrokeWidthAttributeName設(shè)置描邊寬度擂仍,這樣就能使文字空心.
NSStrokeWidthAttributeName這個屬性所對應(yīng)的值是一個 NSNumber 對象(小數(shù))。該值改變描邊寬度(相對于字體size 的百分比)熬甚。默認為 0逢渔,即不改變。正數(shù)只改變描邊寬度乡括。負數(shù)同時改變文字的描邊和填充寬度肃廓。例如,對于常見的空心字粟判,這個值通常為3.0亿昏。
同時設(shè)置了空心的兩個屬性,并且NSStrokeWidthAttributeName屬性設(shè)置為整數(shù)档礁,文字前景色就無效果了
效果:
效果:
8. NSStrikethroughStyleAttributeName 添加刪除線角钩,strikethrough刪除線
效果:
9. NSUnderlineStyleAttributeName 添加下劃線
效果:
10. NSShadowAttributeName 設(shè)置陰影,單獨設(shè)置不好使呻澜,必須和其他屬性搭配才好使递礼。
和這三個任一個都好使,NSVerticalGlyphFormAttributeName羹幸,NSObliquenessAttributeName脊髓,NSExpansionAttributeName
11.NSVerticalGlyphFormAttributeName
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))。0 表示橫排文本栅受。1 表示豎排文本将硝。在 iOS 中,總是使用橫排文本屏镊,0 以外的值都未定義依疼。
效果:
12. NSObliquenessAttributeName設(shè)置字體傾斜。Skew斜
效果: