美文网首页
其它控件

其它控件

作者: su_19 | 来源:发表于2016-04-12 17:49 被阅读49次
    
    //
    //  AppDelegate.m
    //  01 - 其他控件集合
    //
    //  Created by 余婷 on 16/4/11.
    //  Copyright (c) 2016年 余婷. All rights reserved.
    //
    
    #import "AppDelegate.h"
    
    @interface AppDelegate ()<UIActionSheetDelegate, UIAlertViewDelegate, UITextViewDelegate>{
    
        UILabel * _showLabel;
    }
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor = [UIColor whiteColor];
        
        self.window.rootViewController = [[UIViewController alloc] init];
        self.window.rootViewController.view.userInteractionEnabled = NO;
        
        //创建一个label
        _showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 100, 40)];
        [self.window addSubview:_showLabel];
        
        //======================================
        //1.开关
       // [self creatSwitch];
        
        //2.滑块
       // [self creatSlider];
        
        //3.进度条
       // [self creatProgressView];
        
        //4.步进器
       // [self creatStepper];
        
        //5.活动指示器
       /// [self creatActivity];
        
        //6.多段选择器
        [self creatSegmentedControl];
        
        
        //7.表单视图
       //[self creatActionSheet];
        
        //8.警告框
    //    [self creatAlertView];
        
        //9.文本输入视图
        //[self creatTextView];
        
        
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    #pragma mark - UITextView
    - (void)creatTextView{
    
        //UITextView的功能和UITextField的功能基本一致,之前很多的UITextField的属性和方法,UITextView都有类似或者一样的属性和方法
        //区别:1.textFiled只能一行显示文字;textView可以多行显示并且可以滚动。 2.textFiled有占位文字和密文显示功能,但是textView没有的
        //1.创建文本输入视图的对象
        //UITextView : UIScrollView
        UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 300, 200, 100)];
        
        //2.显示在界面上
        [_window addSubview:textView];
        
        //3.设置背景颜色
        textView.backgroundColor = [UIColor lightGrayColor];
        
        //4.设置文字
        textView.text = @"askfhaslhfjashaasldkfhaskhflkahslkdfhaksjhfdklashfdlkahslkdfhaklshdfklashdfkjasldfhajklshfklashjdjfhasklfhasjkfdhasklhdfklashfkashkljaskdfhaskldfhlaskdhflkashdfklhaskldfhklsahdflkashkldfhaskldhfaskjhdflkdashjdfkaljshdflkashfkjlsahdfkhwiaeyfiashfkashfkasjh";
        
        //5.设置是否可以编辑(默认是:YES)
        //如果设置成NO,textView就是纯展示文字的控件
        textView.editable = YES;
        
        //6.设置是否可以选中文字(默认是:YES)
        //设置成NO:关闭选中文字的功能
        textView.selectable = YES;
        
        //7.设置键盘的类型(textField也有这个属性)
    //    UIKeyboardTypeDefault,
    //    UIKeyboardTypeASCIICapable,
    //    UIKeyboardTypeNumbersAndPunctuation,
    //    UIKeyboardTypeURL,
    //    UIKeyboardTypeNumberPad,
    //    UIKeyboardTypePhonePad,
    //    UIKeyboardTypeNamePhonePad,
    //    UIKeyboardTypeEmailAddress,
    //    UIKeyboardTypeDecimalPad
    //    UIKeyboardTypeTwitter
    //    UIKeyboardTypeWebSearch
    //    UIKeyboardTypeAlphabet
        [textView setKeyboardType:UIKeyboardTypeDefault];
        
        //8.设置return按钮的样式(textField也有这个属性)
    //    UIReturnKeyDefault,
    //    UIReturnKeyGo,
    //    UIReturnKeyGoogle,
    //    UIReturnKeyJoin,
    //    UIReturnKeyNext,
    //    UIReturnKeyRoute,
    //    UIReturnKeySearch,
    //    UIReturnKeySend,
    //    UIReturnKeyYahoo,
    //    UIReturnKeyDone,
    //    UIReturnKeyEmergencyCall,
        [textView setReturnKeyType:UIReturnKeySend];
        
        //9.设置代理
        textView.delegate = self;
        
    
    }
    
    //MARK:协议方法
    
    //使用键盘输入文字的时候实时调用这个方法
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
        NSLog(@"%@", text);
        
        //如果是点击了键盘上的返回键
        if ([text isEqualToString:@"\n"]) {
            
            [textView resignFirstResponder];
            
        }
        return YES;
    }
    //已经开始编辑的时候会调用
    - (void)textViewDidBeginEditing:(UITextView *)textView{
    
        NSLog(@"已经开始编辑");
    }
    //已经结束编辑的时候会调用
    - (void)textViewDidEndEditing:(UITextView *)textView{
    
        NSLog(@"已经结束编辑");
        
    }
    
    
    
    
    #pragma mark - UIAlertView
    - (void)creatAlertView{
    
        //1.创建警告框对象
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"标题" message:@"信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        
        //2.显示在界面上
        [alert show];
    }
    
    
    //MARK:协议方法
    //当警告框上的按钮被点击的时候会调用这个方法
    //参数1:委托
    //参数2:被点击的按钮的下标
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        NSLog(@"%lu", buttonIndex);
        
        if (buttonIndex == 1) {
            
            NSLog(@"确定删除");
        }
    }
    
    
    #pragma mark - UIActionSheet
    - (void)creatActionSheet{
    
        //1.创建表单视图对象
        //UIActionSheet:UIView
        //参数1:标题
        //参数2:设置代理
        //参数3:最下面那个独立按钮的标题
        //参数4:最上面那个红色按钮的标题
        //参数5:中间普通按钮的标题
        UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle:@"标题" delegate:self cancelButtonTitle:@"保存" destructiveButtonTitle:@"红色按钮" otherButtonTitles:@"111",@"222",@"333", nil];
        
        //2.显示在界面上
        [sheet showInView:_window];
        
        
    }
    
    //MARK:协议方法
    //当表单上的按钮被点击的时候会调用这个方法
    //参数1:委托
    //参数2:被点击的按钮的下标(当前表单上所有的按钮从上往下下标依次增加)
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
        NSLog(@"%lu", buttonIndex);
        
        //根据不同的按钮点击,做出不一样的响应
        switch (buttonIndex) {
            case 0:{
            
                break;
            }
            case 1:{
                
                break;
            }
            case 2:{
                
                break;
            }
               
            case 3:{
                
                break;
            }
                
                
            default:
                break;
        }
    }
    
    
    
    #pragma mark - UISegmentedControl
    - (void)creatSegmentedControl{
    
        //1.创建多段选择器对象
        //UISegmentedControl : UIControl : UIView
        //参数:当前分段选择中所有分段的标题的数组
        UISegmentedControl * segment = [[UISegmentedControl alloc] initWithItems:@[@"海贼王",@"火影忍者",@"死神"]];
        
        //2.设置frame(宽度和高度都有效)
        segment.frame = CGRectMake(100, 320, 250, 30);
        
        
        //3.显示在界面上
        [self.window addSubview:segment];
        
        //4.当前选中的分段(核心属性)
        //设置选中分段的下标
        segment.selectedSegmentIndex = 0;
        
        //5.添加事件
        [segment addTarget:self action:@selector(segment:) forControlEvents:UIControlEventValueChanged];
        
        //6.获取当前分段数
        NSLog(@"%lu", segment.numberOfSegments);
        
        //7.插入一个文字分段
        [segment insertSegmentWithTitle:@"死亡笔记" atIndex:0 animated:YES];
        
        //8.插入一张图片分段
        //imageWithRenderingMode:设置图片的渲染模式(图片不能正常显示的时候,一般就去设置图片的渲染模式为UIImageRenderingModeAlwaysOriginal)
        [segment insertSegmentWithImage:[[UIImage imageNamed:@"luffy"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] atIndex:1 animated:YES];
        
        //9.移除指定下标的分段(下标不可以越界)
        //- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
        [segment removeSegmentAtIndex:4 animated:YES];
        
        
        //10.移除所有的分段
        //- (void)removeAllSegments;
        //[segment removeAllSegments];
        
        //11.设置指定下标的分段的title(下标不可以越界)
        [segment setTitle:@"妖精的尾巴" forSegmentAtIndex:0];
        
        //12.拿到指定下标分段的文字
        //- (NSString *)titleForSegmentAtIndex:(NSUInteger)segment
        NSString * title = [segment titleForSegmentAtIndex:0];
        NSLog(@"%@",title);
        
        //13.设置填充颜色
        [segment setTintColor:[UIColor orangeColor]];
        
    }
    
    - (void)segment:(UISegmentedControl *)segment{
    
        NSLog(@"%ld", (long)segment.selectedSegmentIndex);
    }
    
    
    #pragma mark - UIActivityIndicatorView
    - (void)creatActivity{
        
        
    
        //1.创建活动指示器对象
        //UIActivityIndicatorView : UIView
    //    UIActivityIndicatorViewStyleWhiteLarge,
    //    UIActivityIndicatorViewStyleWhite,
    //    UIActivityIndicatorViewStyleGray
        UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        
        //设置frame
        //frame中大小无效
        activity.frame = CGRectMake(100, 260, 0, 0);
        
        //显示在界面上(活动指示器只有在动的时候才能看得见)
        [self.window addSubview:activity];
        
        //2.让活动指示器动
        [activity startAnimating];
        
        //3.让活动指示器停止
    //    [activity stopAnimating];
        
        
        
        
    }
    
    #pragma mark - UIStepper
    - (void)creatStepper{
    
        //1.创建步进器对象
        //UIStepper : UIControl : UIView
        //宽度和高度无效
        UIStepper * stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 200, 0, 0)];
        
        //显示在界面上
        [self.window addSubview:stepper];
        
        //2.步进器的值(默认是0,范围是0 - 100)
        stepper.value = 98;
        _showLabel.text = [NSString stringWithFormat:@" %.2lf", stepper.value];
        
        //3.添加事件
        //按stepper的加和减的按钮,都会自动改变stepper的value的值
        [stepper addTarget:self action:@selector(stepper:) forControlEvents:UIControlEventValueChanged];
        
        //4.步进值(每按一次加和减变化的值,默认是1;只能是正数)
        stepper.stepValue = 0.5;
        
        //5.设置最大值和最小值
        stepper.maximumValue = 10;
        stepper.minimumValue = - 10;
        
        //6.设置填充颜色
        stepper.tintColor = [UIColor redColor];
        
    }
    
    - (void)stepper:(UIStepper *)stepper{
    
        //使用stepper最新的值,去更新label的显示
        _showLabel.text = [NSString stringWithFormat:@"%.2lf", stepper.value];
    }
    
    #pragma mark - UIProgressView
    - (void)creatProgressView{
    
        //1.创建进度条对象
        //UIProgressView : UIView
    //    UIProgressViewStyleDefault,  //颜色是灰色的
    //    UIProgressViewStyleBar   //颜色是白色
        UIProgressView * progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        
        //2.设置frame
        //设置高度无效
        progressView.frame = CGRectMake(100, 150, 200, 0);
        
        //显示在界面上
        [self.window addSubview:progressView];
        
        //3.进度(核心属性)(0 - 1)
        progressView.progress = 0.5;
        
        //设置tag值
        progressView.tag = 100;
        
        
        //4.设置从开始到当前进度位置,进度条的填充颜色
        progressView.progressTintColor = [UIColor greenColor];
        
        //5.从进度位置到结束,进度条的填充颜色
        progressView.trackTintColor = [UIColor orangeColor];
        
        //6.设置进度带动画效果
        //- (void)setProgress:(float)progress animated:(BOOL)animated
        [progressView setProgress:0.2 animated:YES];
        
    }
    
    
    #pragma mark - UISlider
    - (void)creatSlider{
    
        //1.创建滑块对象
        //UISlider : UIControl : UIView
        UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
        
        //显示在界面上
        [self.window addSubview:slider];
        
        //3.设置滑块的最小值和最大值
        //最小值(默认是0)
        slider.minimumValue = 10;
        //最大值(默认是1)
        slider.maximumValue = 100;
        
        //2.当前滑块位置对应的值(默认值的范围是0 - 1)
        slider.value = 50.0f;
        
        //4.添加事件
        //能添加的事件只有:UIControlEventValueChanged
        [slider addTarget:self action:@selector(slider:) forControlEvents:UIControlEventValueChanged];
        
        //5.设置slider最左边显示的图片(一般不用)
        [slider setMinimumValueImage:[UIImage imageNamed:@"luffy"]];
        
        //6.设置滑块图片(常用)
        //给滑块正常状态设置图片
        [slider setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
        //给滑块高亮状态设置图片
        [slider setThumbImage:[UIImage imageNamed:@"luffy"] forState:UIControlStateHighlighted];
        
        //7.滑条和滑块的颜色
        //最小值到滑块部分的填充颜色
    //    @property(nonatomic,retain) UIColor *minimumTrackTintColor
        [slider setMinimumTrackTintColor:[UIColor yellowColor]];
        //滑块到最大值部分的填充颜色
    //    @property(nonatomic,retain) UIColor *maximumTrackTintColor
        [slider setMaximumTrackTintColor:[UIColor redColor]];
        //滑块的填充颜色
    //    @property(nonatomic,retain) UIColor *thumbTintColor
        
        //8.设置滑块的值是否实时(连续)改变(默认:YES)
        slider.continuous = NO;
        
    }
    
    //当滑块滑动的时候实时调用这个方法
    - (void)slider:(UISlider *)slider{
    
        NSLog(@"%f", slider.value);
        
        //改变进度条的进度
        UIProgressView * progressView = (UIProgressView *)[_window viewWithTag:100];
        CGFloat progress = slider.value / (slider.maximumValue - slider.minimumValue);
        
        [progressView setProgress:progress animated:YES];
        
    }
    
    #pragma mark - UISwitch
    - (void)creatSwitch{
    
        //1.创建开关对象
        //设置frame只有坐标有效
        //UISwitch : UIControl : UIView
        UISwitch * sw = [[UISwitch alloc] initWithFrame:CGRectMake(100, 30, 10, 10)];
        
        //显示在界面上
        [self.window addSubview:sw];
        
        //2.开关关闭状态(开\关) - 核心属性
        //YES:开关  NO:关(默认)
        //设置开关状态
        [sw setOn:YES];
        //获取开关状态
        BOOL ret = [sw isOn];
        if (ret) {
            NSLog(@"开启");
        }else{
        
            NSLog(@"关闭");
        }
        
        //3.给开关添加事件
        //参数1:效应消息的对象
        //参数2:消息 (可以不带参,也可以带参;如果带参只能带一个参数,并且这个参数的实参就是添加事件的开关本身)
        //参数3:事件(UIControlEventValueChanged)
        [sw addTarget:self action:@selector(switchOn:) forControlEvents:UIControlEventValueChanged];
        
        //4.设置开关开启状态的填充颜色
        sw.onTintColor = [UIColor yellowColor];
        
        //5.设置关闭状态的填充颜色
        sw.tintColor = [UIColor redColor];
        
        //6.设置开关上的滑块的颜色
        sw.thumbTintColor = [UIColor blueColor];
    
    }
    
    - (void)switchOn:(UISwitch *)sw{
        
        //每次点击开关都创建表单
        [self creatActionSheet];
    
        //每次触摸开关,开关的状态都会发生改变
        if (sw.on) {
            
            NSLog(@"开关开启");
            
        }else{
        
            NSLog(@"开关关闭");
        }
    }
    
    
    
    @end
    
    
    

    相关文章

      网友评论

          本文标题:其它控件

          本文链接:https://www.haomeiwen.com/subject/gamilttx.html