iOS基础控件的学习

来源:互联网 发布:网络诗选 编辑:程序博客网 时间:2024/04/29 09:27

1.UIControl  控制视图的事件处理(控件中会使用到)

基于触摸  事件处理:

UIControlEventTouchDown   用户按下时触发

UIControlEventTouchDownRepeat  点击计数大于1时触发

UIControlEventTouchDragInside  当触摸在控件内拖动时触发

UIControlEventTouchDragOutside  当触摸在控件之外拖动时触发

UIControlEventTouchDragEnter  当触摸从控件之外拖动到控件内部时触发

UIControlEventTouchDragExit  当触摸从控件之内拖动到控件外部时触发

UIControlEventTouchUpInside  控件之内触摸抬起时触发

UIControlEventTouchUpOutside  控件之外触摸抬起时触发

UIControlEventTouchCancel  触摸取消事件,设备被上锁(锁屏)或者电话呼叫打断

UIControlEventTouchValueChanged   当控件的值发生改变时。用于滑块、分段控件等控件(UISliser  UISegmentControl  UIPageControl  )

基于编辑  事件处理:

UIControlEventEditingDidBegin  文本控件中开始编辑时

UIControlEventEditingChanged  文本控件中的文本被改变时

UIControlEventEditingDidEnd  文本控件中编辑结束时

UIControlEventEditingDidOnExit  文本控件内通过按下回车键结束编辑时


UIControlEventAllTouchEvents  所有触摸事件

UIControlEventAllEditingEvents  文本编辑的所有事件

UIControlEventAllEvents  所有事件


2.button

巧用button的selected方法,可以把按钮设置成开关一样。

if ( button.selected ) {

button.selected = NO;

}else{

button.selected  = YES;

}

3.UIImageView

在imageView上加子控件时,记得为imageview设置一个属性,imageView.userInteractionEnable = YES;

4.UITextField

常用属性:

placeholder  提示用户输入内容文本

borderStyle  设置风格,默认没有风格,需要设置

clearsOnBeginEditing  用户编辑时是否clear内容,默认为NO

adjustsFontSizeToFitWidth  自适应调整字体大小,默认为NO

(UIImage)background  设置背景,需要将textField实例的风格设置为None

(UIImage)disabledBackground  设置textField不可用时的背景图片

(Bool)editing  设置是否可编辑

clearButtonMode  清除按钮的模式,默认不出现

leftView  自定义左视图

leftViewMode  自定义左视图出现的模式

inputView  不用系统键盘,自定义键盘

inputAccessoryView  系统键盘和自定义键盘共存


autocapitalizationType  自动大写类型

autocorrectionType  检查拼写是否正确

keyboardType  修改键盘类型

returnKeyType  修改返回类型

secureTextEntry  是否安全输入,比如用户输入密码


太麻烦了,直接上代码:

#import "ViewController.h"

#import "AlertView.h"


@interface ViewController ()<UIAlertViewDelegate,UIActionSheetDelegate,UITextFieldDelegate>


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

//    1.AlertView

    UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button1.frame =CGRectMake(10, 60, 140, 40);

    button1.backgroundColor = [UIColorredColor];

    [button1 setTitle:@"show Alert View"forState:UIControlStateNormal];

    [button1 addTarget:selfaction:@selector(showAlertView)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button1];

    

//    2.ActionSheet

    UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button2.frame =CGRectMake(160, 60, 140, 40);

    button2.backgroundColor = [UIColorredColor];

    [button2 setTitle:@"show Action Sheet"forState:UIControlStateNormal];

    [button2 addTarget:selfaction:@selector(showActionView)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button2];

    

//    3.进度环

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

    activityView.center =CGPointMake(60, 160);

    activityView.color = [UIColorredColor];

    activityView.hidesWhenStopped =NO;//停止转动后是否消失,,默认是YES

    [activityViewstartAnimating];

    [self.viewaddSubview:activityView];

    

    [NSTimer scheduledTimerWithTimeInterval:3 target:selfselector:@selector(timerTest:)userInfo:activityViewrepeats:NO];

    

//    4.滑动条UISlider

    UISlider *slider = [[UISlideralloc]initWithFrame:CGRectMake(30, 210, 230, 20)];//高度是默认的,你设置的没用,没法设置它

    slider.value = 0.5;            //设置当前值

    slider.maximumValue = 1;       //设置最大值

    slider.minimumValue = 0;       //设置最小值

    slider.tintColor = [UIColorredColor];//设置颜色

    [slider addTarget:selfaction:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged];

    [self.viewaddSubview:slider];

    

    [NSTimer scheduledTimerWithTimeInterval:1 target:selfselector:@selector(sliderTest:)userInfo:sliderrepeats:YES];

    

//    5.分段控件UISegmentControl

   NSArray *array = @[@"aaa",@"bbb",@"ccc"];

    UISegmentedControl *segmented = [[UISegmentedControlalloc]initWithItems:array];

    segmented.frame =CGRectMake(50, 260,300,40);

    segmented.selectedSegmentIndex = 1;

    segmented.tintColor = [UIColorredColor];//设置颜色

    [segmented addTarget:selfaction:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];

    [self.viewaddSubview:segmented];

    

    [NSTimer scheduledTimerWithTimeInterval:3 target:selfselector:@selector(segmentTest:)userInfo:segmentedrepeats:YES];

    

//    6.UIPageControl分页控制(通常与UIScrollView连用,提示用户当前显示的页数)

    UIPageControl *pageControl = [[UIPageControlalloc]initWithFrame:CGRectMake(60, 320, 100, 40)];

//    pageControl.backgroundColor = [UIColor grayColor];//灰色背景

    pageControl.numberOfPages = 10;

    pageControl.currentPage = 1;

    pageControl.currentPageIndicatorTintColor = [UIColorredColor];

    pageControl.PageIndicatorTintColor = [UIColorgrayColor];

    [pageControl addTarget:selfaction:@selector(pageAction:)forControlEvents:UIControlEventValueChanged];

    [self.viewaddSubview:pageControl];

    

//    7.UITextField

    UITextField *tf = [[UITextFieldalloc]initWithFrame:CGRectMake(60, 380, 200, 40)];

    tf.tag = 110;

    tf.delegate =self;

    tf.textColor = [UIColorredColor];

    tf.placeholder =@"用来提示用户";

//    tf.adjustsFontSizeToFitWidth = YES;

//    tf.clearsOnBeginEditing = YES;//在编辑之前先清空内容

    tf.clearButtonMode =UITextFieldViewModeWhileEditing;//当正在编辑的时候显示最右边的清除按钮

    tf.borderStyle =UITextBorderStyleRoundedRect;//圆角

//    tf.keyboardType = UIKeyboardTypeNumberPad;//设置键盘type。数字键盘

//    tf.secureTextEntry = YES;//设置密码

    tf.returnKeyType =UIReturnKeySearch;//设置键盘回车键的类型(搜索)

    

    //自定义左视图

    UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0, 0, 20, 20)];

    view.backgroundColor = [UIColorredColor];

    tf.leftView = view;

    tf.leftViewMode =UITextFieldViewModeAlways;

    

//    自定义系统键盘

    UIView *scView = [[UIViewalloc]initWithFrame:CGRectMake(0, 0, 320, 44)];

    scView.backgroundColor = [UIColorredColor];

    tf.inputAccessoryView = scView;

//    tf.inputView = scView;

    

    [self.viewaddSubview:tf];

    

    

//    8.UILable

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(60, 450, 200, 40)];

    label.text =@"abndnfkdabndnfkdfabndnfkdfabndnfkdff";//文本

    label.textColor = [UIColorredColor];//文本颜色

    label.textAlignment =NSTextAlignmentCenter;//对齐方式

    label.backgroundColor = [UIColorwhiteColor];//背景色

//    label.shadowColor = [UIColor blueColor];//偏移颜色

//    label.shadowOffset = CGSizeMake(-1, 1);//偏移量

//    label.font = [UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>]//字体

    label.font = [UIFontsystemFontOfSize:14];

    label.numberOfLines = 1;//最多显示几行

    label.lineBreakMode =NSLineBreakByTruncatingMiddle;//文本太多显示不完时,在哪里打省略号

    [label sizeToFit];////根据文本自动调整label的宽度和高度

    [self.viewaddSubview:label];

    

}

#pragma mark - 7.UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    NSLog(@"ShouldBeginEditing");

//    [textField becomeFirstResponder];

    return YES;

}

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    NSLog(@"DidBeginEditing");

}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    NSLog(@"ShouldEndEditing");

    return YES;

}

- (void)textFieldDidEndEditing:(UITextField *)textField{

    NSLog(@"DidEndEditing");

}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSLog(@"houldChangeCharactersInRange");

   NSLog(@"::%@",string);

    return YES;

}


- (BOOL)textFieldShouldClear:(UITextField *)textField{

    NSLog(@"ShouldClear");

    return YES;

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    NSLog(@"搜索。。。");

//    执行用户按下键盘上的return

    [textFieldendEditing:YES];

    return YES;

}


#pragma mark - 6.pageControl

-(void)pageAction:(UIPageControl*)pageControl{

   NSLog(@"page Control:%ld",(long)pageControl.currentPage);

}


#pragma mark - 5.UISlider

-(void)segmentAction:(UISegmentedControl*)segment{

    NSLog(@"segment value:%ld",(long)segment.selectedSegmentIndex);

}

//定时器设置的,将会每三秒执行一次

-(void)segmentTest:(NSTimer*)timer{

    UISegmentedControl *segment = [timeruserInfo];

    [segment insertSegmentWithTitle:@"add"atIndex:1animated:YES];

}


#pragma mark - 4.滑动条,一秒钟切换当前值

-(void)sliderTest:(NSTimer*)timer{

   UISlider *slider = [timeruserInfo];

    [slidersetValue:0.8animated:YES];

}

//值发生改变  事件

-(void)sliderAction:(UISlider*)slider{

    NSLog(@"slider.value:%f",slider.value);

}


#pragma mark - 3.定时器,三秒钟后停止activity的转动

-(void)timerTest:(NSTimer*)timer{

    UIActivityIndicatorView *activityview = [timeruserInfo];

    [activityviewstopAnimating];

}



#pragma mark - 1.button1:AlertView

int identifier = 1;

-(void)showAlertView{

    

//    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Alert View" message:@"message" delegate:nil cancelButtonTitle:@"sure" otherButtonTitles:@"cancel",@"other", nil];

//    [alertView show];

    //自定义alertView

    AlertView *alertView = [[AlertViewalloc]initWithTitle:@"Alert View"message:@"message"delegate:selfcancelButtonTitle:@"sure"otherButtonTitles:@"cancel",@"other",nil];

    [alertViewshow];

    alertView.tag =identifier;

    identifier ++;

    

}

#pragma mark alertView的代理方法,用于处理警告框按钮点击事件

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

   NSLog(@"alert button:%ld",(long)buttonIndex);

}


#pragma mark - 2.button2:ActionSheet

-(void)showActionView{

    

    UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:@"title"delegate:selfcancelButtonTitle:@"cancel"destructiveButtonTitle:@"destructive"otherButtonTitles:@"other button",@"other button",@"other button",nil];

    [actionSheetshowInView:self.view];

}

#pragma mark - actionSheet的代理方法,用于处理按钮点击事件

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

   NSLog(@"alert button:%ld",(long)buttonIndex);

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




0 0
原创粉丝点击