常见控件的总结

一、计步器和计数器

- (void)creatStepper{

    //步进器 、计数器

    //事件驱动型

    UIStepper *stepper  = [[UIStepper alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];

    

    //设置默认色调

    //stepper.tintColor = [UIColor redColor];

    

    //设置最小值 默认是0

    stepper.minimumValue = 0;

    //设置最大值 默认是100

    stepper.maximumValue = 1000;

    //设置步长 默认是1

    stepper.stepValue = 10;

    

    //设置长按 之后是否可以连续触发 函数

    /*

     YES:长按的时候 连续 触发调用stepperClick:

     NO:长按的时候 的时候 stepperClick:最后松手调用一次

     */

    stepper.continuous  = YES;

    

    //长按的时候是否可以  反复修改stepper的值

    /*

     YES:可以连续的修改 值

     NO: 不去连续改值  长按的时候只改一次

     */

    stepper.autorepeat = YES;

    

    

    //增加事件

    [stepper addTarget:self action:@selector(stepperClick:) forControlEvents:UIControlEventValueChanged];

    

    [self.view addSubview:stepper];

    [stepper release];

  

}

- (void)stepperClick:(UIStepper *)stepper {

    NSLog(@"value:%f",stepper.value);//打印当前的值

}

 

二、操作表单

 

                        需要遵守协议

@interface ViewController7 ()  <UIActionSheetDelegate>

 

@end

 

 

- (void)creatButton{

    UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(110, 100, 100, 50) target:self sel:@selector(btnClick:) tag:101 image:nil title:@"分享"];

    [self.view addSubview:button];

    

}

- (void)btnClick:(UIButton *)button {

    //操作表单

    /*

     destructiveButtonTitle: 把一些危险的操作 按钮 写在这里 会显示红色

     */

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"危险操作按钮" otherButtonTitles:@"短信分享",@"朋友圈分享",@"QQ分享" ,nil];

    

    //从self.view上弹出

    [sheet showInView:self.view];

    [sheet release];

}

#pragma mark   -  UIActionSheetDelegate

 

//当点击actionSheet的按钮的时候 通知代理调用处理

 

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

    //索引值 从上至下 从0开始

    NSLog(@"index:%ld",buttonIndex);

}

 

三、分段控制器和时间驱动器

- (void)creatSegmentedControl{

 

    //分段控制器

    //事件驱动型

    NSArray *titles = @[@"小红",@"小黄",@"屎壳郎"];

    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:titles];

    seg.frame = CGRectMake(10, 100, 300, 30);

    //设置属性

    //设置默认选中的分段

    seg.selectedSegmentIndex = 0;

    

    /*

     //增加标题分段

     - (void)insertSegmentWithTitle:(NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;

     //增加图片分段

     - (void)insertSegmentWithImage:(UIImage *)image  atIndex:(NSUInteger)segment animated:(BOOL)animated;

     //删除指定

     - (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;

     //删除所有

     - (void)removeAllSegments;

     */

    //增加图片分段 显示原始图片

    [seg insertSegmentWithImage:[[UIImage imageNamed:@"002"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] atIndex:1 animated:YES];

    //禁用 指定分段

    [seg setEnabled:NO forSegmentAtIndex:3];

    //设置指定分段宽度0 自动设置设置宽

    //[seg setWidth:40 forSegmentAtIndex:0];

    //设置tintColor

    seg.tintColor = [UIColor redColor];

    //设置背景图片 竖屏

    [seg setBackgroundImage:[UIImage imageNamed:@"toolBar"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    //设置 内容偏移量

    //width >0 右偏移 height >0 向下偏移

    [seg setContentOffset:CGSizeMake(5, 5) forSegmentAtIndex:0];

    

    //更改属性字符串(修改内容字符串的颜色和大小)

    NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor yellowColor]};

    

    [seg setTitleTextAttributes:dict forState:UIControlStateNormal];

    

    //增加事件 点击分段值改变触发

    [seg addTarget:self action:@selector(segClick:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:seg];

    [seg release];

}

- (void)segClick:(UISegmentedControl *)seg {

    //seg.selectedSegmentIndex 被点击的分段索引

    //[seg titleForSegmentAtIndex:seg.selectedSegmentIndex]获取分段的标题

    NSLog(@"index:%ld title:%@",seg.selectedSegmentIndex,[seg titleForSegmentAtIndex:seg.selectedSegmentIndex]);

}

 

四、警告视图

 

遵守协议<UIAlertViewDelegate>

 

@interface ViewController8 ()<UIAlertViewDelegate>

 

@end

- (void)creatAlertView{

    //警告视图

    NSArray *titles = @[@"普通",@"用户",@"密码",@"登录注册"];

    for (NSInteger i = 0; i < titles.count; i++) {

        UIButton *button = [MyControl creatButtonWithFrame:CGRectMake(110, 100+40*i, 100, 30) target:self sel:@selector(btnClick:) tag:101+i image:nil title:titles[i]];

        [self.view addSubview:button];

    }

}

- (void)btnClick:(UIButton *)button {

    //四种不同风格的警告视图

    /*

     UIAlertViewStyleDefault = 0,

     UIAlertViewStyleSecureTextInput,

     UIAlertViewStylePlainTextInput,

     UIAlertViewStyleLoginAndPasswordInput

 

     */

    switch (button.tag) {

        case 101://普通

        {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"非法入侵" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

            //设置类型

            alertView.alertViewStyle = UIAlertViewStyleDefault;

            alertView.tag = 201;

            

            [alertView show];

            [alertView release];

            

        }

            break;

        case 102://用户

        {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"角色名称" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

            //设置类型

            alertView.alertViewStyle = UIAlertViewStylePlainTextInput;

            alertView.tag = 202;

            

            [alertView show];

            [alertView release];

            

        }

            break;

        case 103://密码

        {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"密码" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

            //设置类型

            alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;

            alertView.tag = 203;

            

            [alertView show];

            [alertView release];

 

        }

            break;

        case 104://登录注册

        {

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"登录" message:nil delegate:self cancelButtonTitle:@"登录" otherButtonTitles:@"注册", nil];

            //设置类型

            alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

            alertView.tag = 204;

            

            [alertView show];

            [alertView release];

        }

            break;

        

            

        default:

            break;

    }

}

#pragma mark - 代理方法

 

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

    //通过tag区分alertView

    //通过类型也区分 不同类型的alertView

    switch (alertView.alertViewStyle) {

        case UIAlertViewStyleDefault:

        {

            NSLog(@"普通警告视图:%ld",buttonIndex);

        }

            break;

        case UIAlertViewStylePlainTextInput:

        {

            //获取 alertView中textField

            UITextField *text1 = [alertView textFieldAtIndex:0];

            NSLog(@"用户:%@",text1.text);

        }

            break;

        case UIAlertViewStyleSecureTextInput:

        {

            //获取 alertView中textField

            UITextField *text1 = [alertView textFieldAtIndex:0];

            NSLog(@"密码:%@",text1.text);

        }

            break;

        case UIAlertViewStyleLoginAndPasswordInput:

        {

            UITextField *text1 = [alertView textFieldAtIndex:0];

            NSLog(@"用户名:%@",text1.text);

            

            UITextField *text2 = [alertView textFieldAtIndex:1];

            NSLog(@"密码:%@",text2.text);

        }

            break;

        

            

        default:

            break;

    }

}

 五、菊花

- (void)creatAIV{

    //UIActivityIndicatorView 活动指示器 -》菊花/风火轮

    //普通的视图

    /*

     UIActivityIndicatorViewStyleWhiteLarge,//大白

     UIActivityIndicatorViewStyleWhite,//小白

     UIActivityIndicatorViewStyleGray,//小灰

     */

    UIActivityIndicatorView *view = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    

    view.frame = CGRectMake(100, 100, 100, 100);

    //放到中间

    view.center = self.view.center;

    

    //默认必须转动 才显示

    //view.hidesWhenStopped = NO; 默认 停止转动隐藏

    //改变 转动的颜色

    view.color = [UIColor redColor];

    

    [view startAnimating];

    

    //2s之后 停止菊花转动

    //withObject:传得就是 click:的参数

    [self performSelector:@selector(click:) withObject:view afterDelay:2];

    

    //打开 状态栏上的系统自带的网络小菊花

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    

    [self.view addSubview:view];

    [view release];

    

}

 

- (void)click:(UIActivityIndicatorView *)view {

    if (view.isAnimating) {

        [view stopAnimating];//停止转动

        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;//关闭

    }

}

 六、文本输入框

- (void)creatUITextView{

 

    //UITextField 只能输入一行

    

    //iOS7之后 滚动视图粘到带导航的视图上时,滚动视图的内容会自动向下偏移64像素,

    //如果不想自动偏移 那么我们需要取消导航的影响

    

    self.automaticallyAdjustsScrollViewInsets = NO;

    //或者

    //self.edgesForExtendedLayout = UIRectEdgeNone;

    

    

    //UITextView //文本输入 多行 (可以滚动)

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 100, 300, 200)];

    textView.tag = 101;

    

    //设置背景

    textView.backgroundColor = [UIColor orangeColor];

    //设置内容

    textView.text = @"xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情xxxxxxx小红和小黄有激情";

    //字体颜色

    textView.textColor = [UIColor redColor];

    //字体大小

    textView.font = [UIFont systemFontOfSize:20];

    

    //设置是否可以滚动

    //textView.scrollEnabled = NO;

    //设置是否可以编辑

    //textView.editable = NO;

    

    //设置代理

    textView.delegate = self;

    

    

    [self.view addSubview:textView];

    [textView release];

}

 

//收键盘

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITextView *text = (UITextView *)[self.view viewWithTag:101];

    [text resignFirstResponder];

}

#pragma mark - UITextViewDelegate

//将要进入编辑模式的时候调用

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {

    NSLog(@"%s",__func__);

    return YES;//是否可以进入编辑模式

}

//已经进入编辑模式

- (void)textViewDidBeginEditing:(UITextView *)textView {

    NSLog(@"%s",__func__);

}

//将要退出编辑模式调用

//决定是否可以结束编辑模式

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {

    NSLog(@"%s",__func__);

    return YES;

}

//已经退出编辑模式

- (void)textViewDidEndEditing:(UITextView *)textView {

    NSLog(@"%s",__func__);

}

//已经修改内容就会调用

//修改的时候一直调用

- (void)textViewDidChange:(UITextView *)textView {

    NSLog(@"%s",__func__);

}

//只要修改 或者 选中 就会调用

- (void)textViewDidChangeSelection:(UITextView *)textView {

    NSLog(@"%s",__func__);

}

//将要把内容 输入到 textView 的时候调用

 

//YES 表示可以输入到textView中  NO 不可以

//range 光标的位置 text :将要输入的内容

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    NSLog(@"range:%@ text:%@",NSStringFromRange(range),text);

    return YES;

}

 八、下载进度条

 

- (void)creatProgressView{

    //进度条  下载进度等

    UIProgressView *proView = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 100, 300, 30)];

    

    //更改 progress就可以显示进度

    //proView.progress = 0.5;

    //更改轨道颜色

    //    proView.progressTintColor = [UIColor redColor];

    //进度颜色

    //    proView.trackTintColor = [UIColor greenColor];

    

    //模拟下载

    //启动一个定时器 每个1s 更改下载进度

    //userInfo 给定时器 触发的 函数传入一个对象 内部timer.userInfo获取

    

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerClick:) userInfo:proView repeats:YES];

    

    [self.view addSubview: proView];

    [proView release];

 

    

}

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

    //获取传入的值

    UIProgressView *view = timer.userInfo;

    if (view.progress < 1.0) {

        view.progress += 0.1;

    }else {

        //下载完成 终止定时器

        [timer invalidate];

    }

}

 

九、滑动条

 

 

- (void)creatSlider{

 

    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 200, 200, 30)];

 

    //设置最小值

 

    slider.minimumValue = 0;

 

    //设置最大值

 

    slider.maximumValue = 1;//默认是1

 

    //设置轨道颜色

 

    slider.minimumTrackTintColor = [UIColor redColor];

 

    slider.maximumTrackTintColor = [UIColor yellowColor];

 

    //设置 最小值、最大值的图片

 

    slider.minimumValueImage = [UIImage imageNamed:@"003"];

 

    slider.maximumValueImage = [UIImage imageNamed:@"002"];

 

    //设置 小白球的图片

 

    

 

    [slider setThumbImage:[UIImage imageNamed:@"003"] forState:UIControlStateNormal];//正常状态

 

    

 

    [slider setThumbImage:[UIImage imageNamed:@"002"] forState:UIControlStateHighlighted];//高亮状态

 

    

 

    //设置 旋转  修改 transform --》旋转 / 翻转 / 缩放

 

    //相对于 最原始的位置旋转-九十度

 

    slider.transform = CGAffineTransformMakeRotation(-M_PI_2);

 

    

 

    //长按是否连续触发 事件函数

 

    slider.continuous = YES;//NO长按滑动 只触发一次 YES连续触发函数

 

    

 

    //增加事件

 

    [slider addTarget:self action:@selector(sliderClick:) forControlEvents:UIControlEventValueChanged];

 

    

 

    

 

    [self.view addSubview:slider];

 

    [slider release];

 

 

 

 

 

    

 

}

 

- (void)sliderClick:(UISlider *)sd {

 

    NSLog(@"sd:%f",sd.value);//打印值

 

}

 

 十、开关控件

 

//常见控件 1.普通视图2.事件驱动型控件(一般UIControl的子类)

- (void)creatView {

    //开关 1.用设置界面

    

    UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(0, 100, 200, 50)];

    //frame 只能改变位置不能改变 实际大小 (系统有默认大小)

    //iOS7之后没有效果  设置 开关大小  边框的颜色

    sw.tintColor = [UIColor grayColor];

    //设置 开得时候 颜色

    sw.onTintColor = [UIColor orangeColor];

    //设置小白球颜色

    sw.thumbTintColor = [UIColor purpleColor];

    //iOS7之后无效果

    //sw.onImage = [UIImage imageNamed:@"002"];

    //sw.offImage = [UIImage imageNamed:@"002"];

    

    //设置开关的默认状态开/关

    sw.on = YES;

    

    //增加事件

    //当 手指 触发小球 开关状态发生改变的时候调用

    [sw addTarget:self action:@selector(swClick:) forControlEvents:UIControlEventValueChanged];

    

    [self.view addSubview:sw];

    [sw release];

}

//给谁增加的事件 参数就是谁

//当 手指 触发小球 开关状态发生改变的时候调用

- (void)swClick:(UISwitch *)sw {

    NSLog(@"开关被触发%d",sw.on);//sw.on /sw.isOn 获取开关的状态

}

 

posted @ 2015-08-04 21:28  heyuan123  阅读(241)  评论(0编辑  收藏  举报