iOS基礎(chǔ)之OC簡(jiǎn)單控件知識(shí)了解(三)

一.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.?????delegate:

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;

// 返回一個(gè)視圖,用來(lái)設(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:是否顯示指示器,默認(rèn)為NO

_pickerView.showsSelectionIndicator=NO;

注意:設(shè)置UIPickerView的行數(shù)與列數(shù)需要設(shè)置數(shù)據(jù)源,遵守UIPickerViewDataSource,設(shè)置UIPickerView的內(nèi)容需要設(shè)置代理,并且遵守代理方法UIPickerViewDelegate。

5.-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

當(dāng)點(diǎn)擊UIPickerView的某一列中某一行的時(shí)候,就會(huì)調(diào)用這個(gè)方法。

6. 返回第component列每一行的高度

- (CGFloat)pickerView:(UIPickerView*)pickerView

rowHeightForComponent:(NSInteger)component;

7.刷新某一列的數(shù)據(jù)

一旦調(diào)用了這個(gè)方法,就會(huì)重新給數(shù)據(jù)源發(fā)送消息計(jì)算這列的行數(shù)、重新給代理發(fā)送消息獲得這列的內(nèi)容

[pickerViewreloadComponent:1];

8. 刷新所有列的數(shù)據(jù)

- (void)reloadAllComponents;

9. 返回選中的是第component列的第幾行。

-(NSInteger)selectedRowInComponent:(NSInteger)component;

二.UIPageControl

1.?????numberOfPages // 設(shè)置有多少頁(yè) 默認(rèn)為0

// 2)設(shè)置頁(yè)數(shù)

[pageControlsetNumberOfPages:kImageCount];

2.?????currentPage? // 設(shè)置當(dāng)前頁(yè)

[pageControlsetCurrentPage:0];

3.?????pageIndicatorTintColor // 設(shè)置頁(yè)碼指示器顏色

[pageControlsetPageIndicatorTintColor:[UIColorblackColor]];

4.?????currentPageIndicatorTintColor// 設(shè)置當(dāng)前頁(yè)碼指示器顏色

[pageControlsetCurrentPageIndicatorTintColor:[UIColorredColor]];

5.添加分頁(yè)控件的監(jiān)聽事件(監(jiān)聽值改變事件)

[pageControladdTarget:selfaction:@selector(pageChanged:)forControlEvents:UIControlEventValueChanged];

三.UIImageView屬性

1.Image 設(shè)置圖片,默認(rèn)顯示

UIImageView*_imageView = [[UIImageViewalloc]init];

_imageView.image= [UIImageimageNamed:@"me.png"];

2.highlightedImage 設(shè)置高亮狀態(tài)下顯示的圖片

_imageView.highlightedImage= [UIImageimageNamed:@"other.png"];

3.animationImages 設(shè)置序列幀動(dòng)畫的圖片數(shù)組

[_imageViewsetAnimationImages:[NSArrayarray]];

4.highlightedAnimationImages設(shè)置高亮狀態(tài)下序列幀動(dòng)畫的圖片數(shù)組

[_imageViewsetHighlightedAnimationImages:[NSArrayarray]];

5.animationDuration 設(shè)置序列幀動(dòng)畫播放的時(shí)常

[_imageViewsetAnimationDuration:0.3f];

6.animationRepeatCount 設(shè)置序列幀動(dòng)畫播放的次數(shù)

[_imageViewsetAnimationRepeatCount:2];

7.userInteractionEnabled設(shè)置是否允許用戶交互,默認(rèn)不允許用戶交互

[_imageViewsetUserInteractionEnabled:YES];

8.highlighted 設(shè)置是否為高亮狀態(tài),默認(rèn)為普通狀態(tài)

_imageView.highlightedImage= [UIImageimageNamed:@"other.png"];

[_imageViewsetHighlighted:YES];

注意的是在highlighted狀態(tài)下設(shè)置的圖片與序列幀動(dòng)畫要顯示,必須同時(shí)設(shè)置UIImageView的狀態(tài)為highlighted。

四.UIImagePickerController

1.+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;???????????????? 檢查指定源是否在設(shè)備上可用。

//檢查照片源是否可用

[UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]2.allowsEditing 默認(rèn)NO

是否允許編輯

允許編輯.

[imagePickersetAllowsEditing:YES];

3. videoMaximumDuration

設(shè)置UIImagePicker的最大視頻持續(xù)時(shí)間.默認(rèn)10分鐘

4. + availableMediaTypesForSourceType: // 指定源可用的媒體種類

//獲得相機(jī)模式下支持的媒體類型

NSArray*availableMediaTypes = [UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

5. sourceType

設(shè)置UIImagePicker照片源類型,默認(rèn)有3種。

照片源類型

UIImagePickerControllerSourceTypeCamera照相機(jī)

UIImagePickerControllerSourceTypePhotoLibrary照片庫(kù)(通過同步存放的,用戶不能刪除)

UIImagePickerControllerSourceTypeSavedPhotosAlbum保存的照片(通過拍照或者截屏保存的,用戶可以刪除)

6.UIImagePicker使用步驟:

檢查指定源是否可用.isSourceTypeAvailable:方法.

檢查可用媒體(視頻還是只能是圖片)availableMediaTypesForSourceType:方法.

設(shè)置界面媒體屬性mediaTypes property.

顯示界面使用presentViewController:animated:completion:方法.iPad中是popover形式.需要確保sourceType有效.

相關(guān)操作,移除視圖.

如果想創(chuàng)建一個(gè)完全自定義界面的imagepicker來(lái)瀏覽圖片,使用 AssetsLibrary Framework Reference中的類. (AV Foundation Programming Guide 中的 “MediaCapture and Access to Camera” )

7.設(shè)置源

+ availableMediaTypesForSourceType: // 指定源可用的媒體種類

+ isSourceTypeAvailable: // 指定源是否在設(shè)備上可用

sourceType

// 運(yùn)行相關(guān)接口前需要指明源類型.必須有效,否則拋出異常. picker已經(jīng)顯示的時(shí)候改變這個(gè)值,picker會(huì)相應(yīng)改變來(lái)適應(yīng).默認(rèn)UIImagePickerControllerSourceTypePhotoLibrary.

8.設(shè)置picker屬性

allowsEditing //是否可編輯

delegate

mediaTypes

// 指示picker中顯示的媒體類型.設(shè)置每種類型之前應(yīng)用availableMediaTypesForSourceType:檢查一下.如果為空或者array中類型都不可用,會(huì)發(fā)生異常.默認(rèn)kUTTypeImage, 只能顯示圖片.

9.video選取參數(shù)

videoQuality //視頻拍攝選取時(shí)的編碼質(zhì)量.只有mediaTypes包含kUTTypeMovie時(shí)有效.

videoMaximumDuration //秒,video最大記錄時(shí)間,默認(rèn)10分鐘.只用當(dāng)mediaTypes包含kUTTypeMovie時(shí)有效.

10.自定義界面

showsCameraControls

// 指示 picker 是否顯示默認(rèn)的cameracontrols.默認(rèn)是YES,設(shè)置成NO隱藏默認(rèn)的controls來(lái)使用自定義的overlayview.(從而可以實(shí)現(xiàn)多選而不是選一張picker就dismiss了).只有UIImagePickerControllerSourceTypeCamera源有效,否則NSInvalidArgumentException異常.

cameraOverlayView

//自定義的用于顯示在picker之上的view.只有當(dāng)源是UIImagePickerControllerSourceTypeCamera時(shí)有效.其他時(shí)候使用拋出NSInvalidArgumentException異常.

cameraViewTransform

//預(yù)先動(dòng)畫.只影響預(yù)先圖像,對(duì)自定義的overlayview和默認(rèn)的picker無(wú)效.只用當(dāng)picker的源是UIImagePickerControllerSourceTypeCamera時(shí)有效,否則NSInvalidArgumentException異常.

11.選取媒體

– takePicture

//使用攝像頭選取一個(gè)圖片。自定義overlay可以多選。已經(jīng)有圖片正在選取是調(diào)用無(wú)效,必須要等delegate收到imagePickerController:didFinishPickingMediaWithInfo:消息后才能再次選取。非UIImagePickerControllerSourceTypeCamera源會(huì)導(dǎo)致異常。

– startVideoCapture

– stopVideoCapture

//結(jié)束視頻選取,之后系統(tǒng)調(diào)用delegate的imagePickerController:didFinishPickingMediaWithInfo:方法。

12.設(shè)置攝像頭

cameraDevice //使用的鏡頭(默認(rèn)后置的)

+ isCameraDeviceAvailable: // 攝像設(shè)備是否可用.

+ availableCaptureModesForCameraDevice: // 設(shè)備可用的選取模式

cameraCaptureMode //相機(jī)捕獲模式

cameraFlashMode //閃光燈模式(默認(rèn)自動(dòng))

+ isFlashAvailableForCameraDevice: // 是否有閃光能力

13.UIImagePickerControllerDelegate

使用UIImageWriteToSavedPhotosAlbum保存圖像,UISaveVideoAtPathToSavedPhotosAlbum保存視頻. 4.0后使用writeImageToSavedPhotosAlbum:metadata:completionBlock:保存元數(shù)據(jù).

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

//包含選擇的圖片或者一個(gè)視頻的URL,詳見“EditingInformation Keys.”

//如果是設(shè)置可編輯屬性,那么picker會(huì)預(yù)顯示選中的媒體,編輯后的與初始的都會(huì)保存在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;// 原始選擇項(xiàng)的URL

NSString *const UIImagePickerControllerMediaMetadata;// 只有在使用攝像頭并且是圖像類型的時(shí)候有效.包含選擇圖像信息的字典類型

14. UIImagePickerController小例子

UIImagePickerController的代理需要遵守這兩個(gè)協(xié)議.

#pragma mark選擇照片

- (void)selectPhoto

{

// 1.首先判斷照片源是否可用

if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

// 0)實(shí)例化控制器

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ù)存儲(chǔ),同時(shí)與界面無(wú)關(guān)

//可以使用多線程來(lái)保存圖像

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];

});

}

五.UIDatePicker

1. Locale

設(shè)置DatePicker的地區(qū),即設(shè)置DatePicker顯示的語(yǔ)言。

// 1.跟蹤所有可用的地區(qū),取出想要的地區(qū)

NSLog(@"%@", [NSLocaleavailableLocaleIdentifiers]);

// 2.設(shè)置日期選擇控件的地區(qū)

[datePickersetLocale:[[NSLocale

alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];

效果:

// 2)設(shè)置日期選擇控件的地區(qū)

[datePickersetLocale:[[NSLocalealloc]initWithLocaleIdentifier:@"en_SC"]];

效果:

2. Calendar

設(shè)置DatePicker的日歷。

默認(rèn)為當(dāng)天。

[datePickersetCalendar:[NSCalendarcurrentCalendar]];

3. timeZone

設(shè)置DatePicker的時(shí)區(qū)。

默認(rèn)為設(shè)置為:[datePickersetTimeZone:[NSTimeZonedefaultTimeZone]];

4. date

設(shè)置DatePicker的日期。

默認(rèn)設(shè)置為: [datePickersetDate:[NSDatedate]];

5. minimumDate

設(shè)置DatePicker的允許的最小日期。

6. maximumDate

設(shè)置DatePicker的允許的最大日期。

7. countDownDuration

設(shè)置DatePicker的倒計(jì)時(shí)間.

// 1)設(shè)置日期選擇的模

[self.datePickersetDatePickerMode:UIDatePickerModeCountDownTimer];

// 2)設(shè)置倒計(jì)時(shí)的時(shí)長(zhǎng)

//注意:設(shè)置倒計(jì)時(shí)時(shí)長(zhǎng)需要在確定模式之后指定

//倒計(jì)時(shí)的時(shí)長(zhǎng),以秒為單位

[self.datePickersetCountDownDuration:10*60];

效果:

8. minuteInterval

你可以將分鐘表盤設(shè)置為以不同的時(shí)間間隔來(lái)顯示分鐘,前提是該間隔要能夠讓60整除。默認(rèn)間隔是一分鐘。如果要使用不同的間隔,需要改變 minuteInterval屬性:

//設(shè)置分鐘間隔

datePicker.minuteInterval=15;

9. datePickerMode

9.1

UIDatePickerModeTime,//Displays hour, minute, and optionally AM/PM designation depending on the localesetting (e.g. 6 | 53 | PM)

顯示小時(shí),分鐘和AM/PM,這個(gè)的名稱是根據(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 默認(rèn)是顯示這種模式

UIDatePickerModeDateAndTime,// Displays date, hour, minute, and optionally AM/PMdesignation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)

顯示日期,小時(shí),分鐘,和AM/PM,名稱是根據(jù)本地設(shè)置的

[datePickersetDatePickerMode:UIDatePickerModeDateAndTime];

效果圖:

9.4

UIDatePickerModeCountDownTimer// Displays hour and minute (e.g. 1 | 53)

顯示小時(shí)和分鐘

[datePickersetDatePickerMode:UIDatePickerModeCountDownTimer];

10. UIDatePicker使用教程一。

10.1初始化

//不用設(shè)置寬高,因?yàn)樗膶捀呤枪潭ǖ?/p>

UIDatePicker*datePicker = [[UIDatePickeralloc]init];

10.2常用設(shè)置

//設(shè)置區(qū)域?yàn)橹袊?guó)簡(jiǎn)體中文

datePicker.locale= [[NSLocale alloc]

initWithLocaleIdentifier:@"zh_CN"];

//設(shè)置picker的顯示模式:只顯示日期

datePicker.datePickerMode =UIDatePickerModeDate;

10.3UIDatePicker需要監(jiān)聽值的改變

[datePickeraddTarget:selfaction:@selector(dateChange:)

forControlEvents:UIControlEventValueChanged];

11.UIDatePicker使用教程二。

11.1日期范圍

你可以通過設(shè)置mininumDate和 maxinumDate 屬性,來(lái)指定使用的日期范圍。如果用戶試圖滾動(dòng)到超出這一范圍的日期,表盤會(huì)回滾到最近的有效日期。兩個(gè)方法都需要NSDate 對(duì)象作參數(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 如果兩個(gè)日期范圍屬性中任何一個(gè)未被設(shè)置,則默認(rèn)行為將會(huì)允許用戶選擇過去或未來(lái)的任意日期。這在某些情況下很有用處,比如,當(dāng)選擇生日時(shí),可以是過去的任意日期,但終止與當(dāng)前日期。如果你希望設(shè)置默認(rèn)顯示的日期,可以使用date屬性:

1.datePicker.date?=?minDate;

11.3 此外,你還可以用 setDate 方法。如果選擇了使用動(dòng)畫,則表盤會(huì)滾動(dòng)到你指定的日期:

1.[?datePicker?setDate:maxDate?animated:YES];

六.UIActivityIndicatorView

1.?????activityIndicatorViewStyle

設(shè)置指示器的樣式

UIActivityIndicatorViewStyleWhiteLarge

UIActivityIndicatorViewStyleWhite ?(默認(rèn)樣式)

UIActivityIndicatorViewStyleGray

2.hidesWhenStopped

當(dāng)停止動(dòng)畫的時(shí)候,是否隱藏。默認(rèn)為YES。

3. 實(shí)例化指示器對(duì)象,根據(jù)樣式設(shè)置尺寸,不需要手動(dòng)設(shè)置。

-(id)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style;

4.開啟動(dòng)畫

- (void)startAnimating;

5.關(guān)閉動(dòng)畫

- (void)stopAnimating;

6.是否在動(dòng)畫

- (BOOL)isAnimating;

7. UIActivityIndicatorView使用注意

7.1初始化的時(shí)候不需要設(shè)置尺寸,設(shè)置尺寸也沒有效果。

7.2 必須調(diào)用startAnimating才會(huì)顯示UIActivityIndicatorView

//初始化指示器

UIActivityIndicatorView*indicator = [[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

//設(shè)置指示器位置

indicator.center=CGPointMake(self.view.frame.size.width*0.5,self.view.frame.size.height*0.5);

//開啟動(dòng)畫,必須調(diào)用,否則無(wú)法顯示

[indicatorstartAnimating];

[self.viewaddSubview:indicator];

七.文本屬性Attributes

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這個(gè)屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(小數(shù))。該值改變描邊寬度(相對(duì)于字體size 的百分比)。默認(rèn)為 0,即不改變。正數(shù)只改變描邊寬度。負(fù)數(shù)同時(shí)改變文字的描邊和填充寬度。例如,對(duì)于常見的空心字,這個(gè)值通常為3.0。

同時(shí)設(shè)置了空心的兩個(gè)屬性,并且NSStrokeWidthAttributeName屬性設(shè)置為整數(shù),文字前景色就無(wú)效果了

效果:

效果:

8. NSStrikethroughStyleAttributeName 添加刪除線,strikethrough刪除線

效果:

9. NSUnderlineStyleAttributeName 添加下劃線

效果:

10. NSShadowAttributeName 設(shè)置陰影,單獨(dú)設(shè)置不好使,必須和其他屬性搭配才好使。

和這三個(gè)任一個(gè)都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName

11.NSVerticalGlyphFormAttributeName

該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(整數(shù))。0 表示橫排文本。1 表示豎排文本。在 iOS 中,總是使用橫排文本,0 以外的值都未定義。

效果:

12. NSObliquenessAttributeName設(shè)置字體傾斜。Skew斜

效果:

13. NSExpansionAttributeName 設(shè)置文本扁平化

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容