您的位置:首页 > 移动开发 > IOS开发

iOS基础之OC简单控件知识了解(三)

2017-05-12 16:15 513 查看

一.UIPikerView的属性

1.     numberOfComponents:返回UIPickerView当前的列数

NSInteger num =
_pickerView.numberOfComponents;
NSLog(
@"%d", num);

2. -(NSInteger)numberOfRowsInComponent:(NSInteger)component; 返回component列中有多少行。

NSInteger numInCp = [_pickerView
numberOfRowsInComponent:0];
NSLog(@"%d",numInCp);

3. -  (CGSize)rowSizeForComponent:(NSInteger)component;返回component中一行的尺寸。

 
CGSize size = [_pickerView
rowSizeForComponent:0];
NSLog(@"%@",
NSStringFromCGSize(size));

 

2.     delegate:

2.0 设置UIPickerView代理

_pickerView.delegate =
self;
// 设置UIPickView每行显示的内容
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;

 

// 返回一个视图,用来设置pickerView的每行显示的内容。

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component
reusingView:(UIView *)view
{
    UIView *myView=[[UIView
alloc]init];
    myView.backgroundColor = [UIColor
redColor];
    return myView;
}

效果:

3.     dataSource:数据源

#pragma mark  -dataSource method
// 设置每列显示3行
- (NSInteger)pickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent:(NSInteger)component
{
    return
3;
}
// 设置显示2列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return
2;
}

4. showsSelectionIndicator:是否显示指示器,默认为NO  

_pickerView.showsSelectionIndicator =
NO;

注意:设置UIPickerView的行数与列数需要设置数据源,遵守UIPickerViewDataSource,设置UIPickerView的内容需要设置代理,并且遵守代理方法UIPickerViewDelegate。

 
 

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

当点击UIPickerView的某一列中某一行的时候,就会调用这个方法。

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

- (CGFloat)pickerView:(UIPickerView*)pickerView

rowHeightForComponent:(NSInteger)component;

 

7.刷新某一列的数据

一旦调用了这个方法,就会重新给数据源发送消息计算这列的行数、重新给代理发送消息获得这列的内容

[pickerView reloadComponent:1];

8. 刷新所有列的数据

- (void)reloadAllComponents;

9. 返回选中的是第component列的第几行。

-(NSInteger)selectedRowInComponent:(NSInteger)component;

二.UIPageControl

1.     numberOfPages // 设置有多少页 默认为0

// 2) 设置页数
   [pageControl setNumberOfPages:kImageCount];

2.     currentPage  // 设置当前页

[pageControl setCurrentPage:0];
 

3.     pageIndicatorTintColor // 设置页码指示器颜色

 [pageControl setPageIndicatorTintColor:[UIColor
blackColor]];

4.     currentPageIndicatorTintColor// 设置当前页码指示器颜色

 

[pageControl setCurrentPageIndicatorTintColor:[UIColor
redColor]];
 
5.添加分页控件的监听事件(监听值改变事件)
[pageControl addTarget:self
action:@selector(pageChanged:)
forControlEvents:UIControlEventValueChanged];
 

 

三.UIImageView属性

1.Image 设置图片,默认显示

 UIImageView *_imageView = [[UIImageView
alloc]init];
 

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

2.highlightedImage 设置高亮状态下显示的图片

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

3.animationImages 设置序列帧动画的图片数组

 [_imageView
setAnimationImages:[NSArray
array]];

4.highlightedAnimationImages设置高亮状态下序列帧动画的图片数组

[_imageView
setHighlightedAnimationImages:[NSArray
array]];
 

5.animationDuration 设置序列帧动画播放的时常

[_imageView se
12469
tAnimationDuration:0.3f];

6.animationRepeatCount 设置序列帧动画播放的次数

[_imageView setAnimationRepeatCount:2];

7.userInteractionEnabled设置是否允许用户交互,默认不允许用户交互

[_imageView
setUserInteractionEnabled:YES];
 

8.highlighted 设置是否为高亮状态,默认为普通状态

_imageView.highlightedImage = [UIImage
imageNamed:@"other.png"];
[_imageView setHighlighted:YES];

 

注意的是在highlighted状态下设置的图片与序列帧动画要显示,必须同时设置UIImageView的状态为highlighted。

四.UIImagePickerController

1.+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;                 检查指定源是否在设备上可用。

//检查照片源是否可用

[UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]2.allowsEditing 默认NO

是否允许编辑

允许编辑.

[imagePicker setAllowsEditing:YES];

3. videoMaximumDuration

设置UIImagePicker的最大视频持续时间.默认10分钟

4. + availableMediaTypesForSourceType: // 指定源可用的媒体种类

 //获得相机模式下支持的媒体类型

NSArray*availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];

5. sourceType

设置UIImagePicker照片源类型,默认有3种。

照片源类型
 
 UIImagePickerControllerSourceTypeCamera           
照相机
 UIImagePickerControllerSourceTypePhotoLibrary     
照片库(通过同步存放的,用户不能删除)
 UIImagePickerControllerSourceTypeSavedPhotosAlbum 
保存的照片(通过拍照或者截屏保存的,用户可以删除)

 

6.UIImagePicker使用步骤:

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

    检查可用媒体(视频还是只能是图片)availableMediaTypesForSourceType:方法.

    设置界面媒体属性mediaTypes property.

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

    相关操作,移除视图.

如果想创建一个完全自定义界面的imagepicker来浏览图片,使用 AssetsLibrary Framework Reference中的类. (AV Foundation Programming Guide 中的 “MediaCapture and Access to Camera” )

 

7.设置源

    + availableMediaTypesForSourceType: // 指定源可用的媒体种类

    + isSourceTypeAvailable: // 指定源是否在设备上可用

      sourceType

// 运行相关接口前需要指明源类型.必须有效,否则抛出异常. picker已经显示的时候改变这个值,picker会相应改变来适应.默认UIImagePickerControllerSourceTypePhotoLibrary.

8.设置picker属性

      allowsEditing //是否可编辑

      delegate

      mediaTypes

// 指示picker中显示的媒体类型.设置每种类型之前应用availableMediaTypesForSourceType:检查一下.如果为空或者array中类型都不可用,会发生异常.默认kUTTypeImage, 只能显示图片.

    

9.video选取参数

      videoQuality //视频拍摄选取时的编码质量.只有mediaTypes包含kUTTypeMovie时有效.

      videoMaximumDuration //秒,video最大记录时间,默认10分钟.只用当mediaTypes包含kUTTypeMovie时有效.

10.自定义界面

      showsCameraControls

// 指示 picker 是否显示默认的cameracontrols.默认是YES,设置成NO隐藏默认的controls来使用自定义的overlayview.(从而可以实现多选而不是选一张picker就dismiss了).只有UIImagePickerControllerSourceTypeCamera源有效,否则NSInvalidArgumentException异常.

      cameraOverlayView

//自定义的用于显示在picker之上的view.只有当源是UIImagePickerControllerSourceTypeCamera时有效.其他时候使用抛出NSInvalidArgumentException异常.

      cameraViewTransform

//预先动画.只影响预先图像,对自定义的overlayview和默认的picker无效.只用当picker的源是UIImagePickerControllerSourceTypeCamera时有效,否则NSInvalidArgumentException异常.

11.选取媒体

    – takePicture

//使用摄像头选取一个图片。自定义overlay可以多选。已经有图片正在选取是调用无效,必须要等delegate收到imagePickerController:didFinishPickingMediaWithInfo:消息后才能再次选取。非UIImagePickerControllerSourceTypeCamera源会导致异常。

    – startVideoCapture

    – stopVideoCapture

//结束视频选取,之后系统调用delegate的imagePickerController:didFinishPickingMediaWithInfo:方法。

12.设置摄像头

      cameraDevice //使用的镜头(默认后置的)

    + isCameraDeviceAvailable: // 摄像设备是否可用.

    + availableCaptureModesForCameraDevice: // 设备可用的选取模式

      cameraCaptureMode //相机捕获模式

      cameraFlashMode //闪光灯模式(默认自动)

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

13.UIImagePickerControllerDelegate

使用UIImageWriteToSavedPhotosAlbum保存图像,UISaveVideoAtPathToSavedPhotosAlbum保存视频. 4.0后使用writeImageToSavedPhotosAlbum:metadata:completionBlock:保存元数据.

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

//包含选择的图片或者一个视频的URL,详见“EditingInformation Keys.”

//如果是设置可编辑属性,那么picker会预显示选中的媒体,编辑后的与初始的都会保存在info中.

    – imagePickerControllerDidCancel:

    – imagePickerController:didFinishPickingImage:editingInfo://Deprecatedin iOS 3.0

NSString *const UIImagePickerControllerMediaType;// 媒体类型

NSString *const UIImagePickerControllerOriginalImage;// 原始未编辑的图像

NSString *const UIImagePickerControllerEditedImage;// 编辑后的图像

NSString *const UIImagePickerControllerCropRect;// 源图像可编辑(有效?)区域

NSString *const UIImagePickerControllerMediaURL;// 视频的路径

NSString *const UIImagePickerControllerReferenceURL;// 原始选择项的URL

NSString *const UIImagePickerControllerMediaMetadata;// 只有在使用摄像头并且是图像类型的时候有效.包含选择图像信息的字典类型

14. UIImagePickerController小例子

UIImagePickerController的代理需要遵守这两个协议.<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>
 
#pragma mark 选择照片
- (void)selectPhoto
{
    // 1. 首先判断照片源是否可用
    if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
       
        // 0)实例化控制器
        UIImagePickerController *picker = [[UIImagePickerController
alloc]init];
        // 1)设置照片源
        [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
       
        // 2) 设置允许修改
        [picker setAllowsEditing:YES];
        // 3) 设置代理
        [picker setDelegate:self];
        // 4) 显示控制器
        [self
presentViewController:picker animated:YES
completion:nil];
       
    } else {
        NSLog(@"照片源不可用");
    }
   
}
 
#pragma mark - imagePicker代理方法
- (void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    [_imageButton
setImage:image forState:UIControlStateNormal];
   
    // 关闭照片选择器
    [self
dismissViewControllerAnimated:YES
completion:nil];
   
    // 需要将照片保存至应用程序沙箱,由于涉及到数据存储,同时与界面无关
    // 可以使用多线程来保存图像
    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. 转换成NSData保存
        NSData *imageData =
UIImagePNGRepresentation(image);
        [imageData writeToFile:imagePath
atomically:YES];
    });
}
 

 

 五.UIDatePicker 

1. Locale

设置DatePicker的地区,即设置DatePicker显示的语言。

// 1.跟踪所有可用的地区,取出想要的地区
    NSLog(@"%@", [NSLocale
availableLocaleIdentifiers]);
   
// 2. 设置日期选择控件的地区
[datePicker setLocale:[[NSLocale
alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
效果:

// 2) 设置日期选择控件的地区
[datePicker setLocale:[[NSLocale
alloc]initWithLocaleIdentifier:@"en_SC"]];
效果:

 

2. Calendar

设置DatePicker的日历。

默认为当天。

[datePicker setCalendar:[NSCalendar
currentCalendar]];
 
 

3. timeZone

设置DatePicker的时区。

默认为设置为:[datePicker setTimeZone:[NSTimeZone
defaultTimeZone]];
 

4. date

设置DatePicker的日期。

默认设置为: [datePicker setDate:[NSDate
date]];
 

 

5. minimumDate

设置DatePicker的允许的最小日期。

 

6. maximumDate

设置DatePicker的允许的最大日期。

 

7. countDownDuration

设置DatePicker的倒计时间.

   // 1) 设置日期选择的模
   [self.datePicker
setDatePickerMode:UIDatePickerModeCountDownTimer];
           
   // 2) 设置倒计时的时长
   // 注意:设置倒计时时长需要在确定模式之后指定
   // 倒计时的时长,以秒为单位
   [self.datePicker
setCountDownDuration:10 *
60];
效果:
   
 

8. minuteInterval

你可以将分钟表盘设置为以不同的时间间隔来显示分钟,前提是该间隔要能够让60整除。默认间隔是一分钟。如果要使用不同的间隔,需要改变 minuteInterval属性:

// 设置分钟间隔
    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)
显示小时,分钟和AM/PM,这个的名称是根据本地设置的
[datePicker
setDatePickerMode:UIDatePickerModeTime];
效果图:

 
9.2    
UIDatePickerModeDate,           // Displays month, day, and year depending on the locale setting (e.g.November | 15 | 2007)
显示年月日,名称根据本地设置的
[datePicker
setDatePickerMode: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,名称是根据本地设置的
[datePicker
setDatePickerMode:UIDatePickerModeDateAndTime];  

效果图:

 9.4 
 UIDatePickerModeCountDownTimer  // Displays hour and minute (e.g. 1 | 53)
           显示小时和分钟

   [datePicker
setDatePickerMode:UIDatePickerModeCountDownTimer];

10. UIDatePicker使用教程一。

10.1初始化

// 不用设置宽高,因为它的宽高是固定的

UIDatePicker *datePicker = [[UIDatePicker
alloc] init];

 

10.2常用设置

// 设置区域为中国简体中文
datePicker.locale= [[NSLocale alloc]
                        initWithLocaleIdentifier:@"zh_CN"];
//设置picker的显示模式:只显示日期
datePicker.datePickerMode =UIDatePickerModeDate;

 

10.3UIDatePicker需要监听值的改变

[datePicker addTarget:self
action:@selector(dateChange:)

forControlEvents:UIControlEventValueChanged];

11.UIDatePicker使用教程二。

11.1日期范围

你可以通过设置mininumDate和 maxinumDate 属性,来指定使用的日期范围。如果用户试图滚动到超出这一范围的日期,表盘会回滚到最近的有效日期。两个方法都需要NSDate 对象作参数:

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 如果两个日期范围属性中任何一个未被设置,则默认行为将会允许用户选择过去或未来的任意日期。这在某些情况下很有用处,比如,当选择生日时,可以是过去的任意日期,但终止与当前日期。如果你希望设置默认显示的日期,可以使用date属性:

1.   datePicker.date = minDate;  

11.3 此外,你还可以用 setDate 方法。如果选择了使用动画,则表盘会滚动到你指定的日期:

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

 

 

六.UIActivityIndicatorView

1.     activityIndicatorViewStyle

设置指示器的样式

UIActivityIndicatorViewStyleWhiteLarge
UIActivityIndicatorViewStyleWhite  (默认样式)
UIActivityIndicatorViewStyleGray  

2.hidesWhenStopped

当停止动画的时候,是否隐藏。默认为YES。

3. 实例化指示器对象,根据样式设置尺寸,不需要手动设置。

-(id)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style;

4.开启动画

- (void)startAnimating;

5.关闭动画

- (void)stopAnimating;

6.是否在动画

- (BOOL)isAnimating;
 
 

7. UIActivityIndicatorView使用注意

7.1初始化的时候不需要设置尺寸,设置尺寸也没有效果。

7.2 必须调用startAnimating才会显示UIActivityIndicatorView

// 初始化指示器
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView
alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    // 设置指示器位置
    indicator.center =
CGPointMake(self.view.frame.size.width *
0.5, self.view.frame.size.height *
0.5);
    // 开启动画,必须调用,否则无法显示
    [indicator startAnimating];
   
    [self.view
addSubview:indicator];
 

七.文本属性Attributes

1.NSKernAttributeName: @10 调整字句 kerning 字句调整

2.NSFontAttributeName: [UIFont systemFontOfSize:_fontSize] 设置字体

3.NSForegroundColorAttributeName:[UIColor redColor] 设置文字颜色

4.NSParagraphStyleAttributeName: paragraph 设置段落样式

5.NSMutableParagraphStyle*paragraph = [[NSMutableParagraphStyle alloc] init];

paragraph.alignment= NSTextAlignmentCenter;

6.NSBackgroundColorAttributeName:[UIColor blackColor] 设置背景颜色

7.NSStrokeColorAttributeName设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心.

NSStrokeWidthAttributeName这个属性所对应的值是一个 NSNumber 对象(小数)。该值改变描边宽度(相对于字体size 的百分比)。默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。
同时设置了空心的两个属性,并且NSStrokeWidthAttributeName属性设置为整数,文字前景色就无效果了

 

效果:

 

效果:

8. NSStrikethroughStyleAttributeName 添加删除线,strikethrough删除线

效果:

9. NSUnderlineStyleAttributeName 添加下划线

效果:

10. NSShadowAttributeName 设置阴影,单独设置不好使,必须和其他属性搭配才好使。

和这三个任一个都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName

 

11.NSVerticalGlyphFormAttributeName

该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。

效果:

 

12. NSObliquenessAttributeName设置字体倾斜。Skew

 

效果:

 

13. NSExpansionAttributeName 设置文本扁平化

 

效果:

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息