美文网首页
ios(常用控件)UIStepper、UISlider、UISw

ios(常用控件)UIStepper、UISlider、UISw

作者: 我不白先生 | 来源:发表于2020-10-14 11:36 被阅读0次

    UIStepper步进控件

    记录一个可变化的浮点数值,与用户的交互动作为点击动作,可以精确控制数字的变化

    • 属性:
      value 用于记录的浮点数
      Autorepeat 按住不松手是否会连续改变数值
      Continuous是否能够看到连续变化的过程,勾上,则按下不松手会看到数字连续变化的过程
      Wrap数字是否可以循环,即到达最大或最小以后,是否还会继续变化
    • 事件:valueChanged
    @implementation MyViewController
    - (IBAction)valueChanged:(UIStepper *)sender {
        self.label.text = [NSString stringWithFormat:@"%f",sender.value];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        self.label.text = [NSString stringWithFormat:@"%f",self.stepper.value];
    }
    

    UISlider滑块控件

    记录一个可变化的浮点数值,与用户的交互方法为滑动动作,不太能精确控制数字,但能快速得到某个范围内的数值

    • 属性:value
    • 事件:valueChanged
      MyViewController.m
    @property (weak, nonatomic) IBOutlet UIView *myView;
    @property (weak, nonatomic) IBOutlet UISlider *redSlider;
    @property (weak, nonatomic) IBOutlet UISlider *greenSlider;
    @property (weak, nonatomic) IBOutlet UISlider *blueSlider;
    @property (weak, nonatomic) IBOutlet UISlider *alphaSlider;
    @property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
    
    @end
    
    @implementation MyViewController
    -(void)setSliderEnabled{
        self.redSlider.enabled = self.mySwitch.on;
        self.greenSlider.enabled = self.mySwitch.on;
        self.blueSlider.enabled = self.mySwitch.on;
        self.alphaSlider.enabled = self.mySwitch.on;
    }
    -(void)setMyViewBackGroundColor
    {
        self.myView.backgroundColor =
        [UIColor colorWithRed:self.redSlider.value
        green:self.greenSlider.value blue:self.blueSlider.value alpha:self.alphaSlider.value];
    }
    - (IBAction)mySwitch:(UISwitch *)sender {
        [self setSliderEnabled];
        
    }
    - (IBAction)Valuechanged:(UISlider *)sender {
        [self setMyViewBackGroundColor];
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        [self setMyViewBackGroundColor];
        
    }
    

    UISwitch开关控件

    记录了一个布尔型的值

    • 属性:on
    • 事件:valueChanged
      SwitchViewController.m
    @implementation SwitchViewController
    - (IBAction)valuechage:(UISwitch *)sender {
        self.labei.text = sender.on ? @"开":@"关";
    }
    - (IBAction)buttonClick:(id)sender {
        //取出控件getter方法
        //设置控件setter方法
        //self.mySwitch.on = !self.mySwitch.on;
       [self.mySwitch setOn:!self.mySwitch.on animated:YES];
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
         self.labei.text = self.mySwitch.on ? @"开":@"关";
    }
    

    UISegmenterControl分段控件(单选控件)

    记录用户的一个选择

    • 属性:selectedSegmentIndex(从0开始计数)
    • 事件:valueChanged

    UIDatePicker日期选择器

    属性:date(NSDate类型)

    UINavigationController导航控制器

    不在于提供一个可以展示的完整的界面,而是用于管理子控制器的切换。所有控制器在切换界面之前,先和导航沟通,由导航控制器负责推出新的界面,或返回到上一个界面

    • 如何使用

    step1:创建导航控制器实例(alloc init)
    step2:设置导航的根视图控制器
    step3:为了显示导航管理的界面,还需要将导航设置为window的根视图控制器

    • 子控制器的切换

    step1:如果需要导航推出新的控制器,则使用导航的pushViewController方法即可
    step2:如何需要导航返回到上一个控制器,则使用导航的popViewController方法即可
    AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        //创建一个导航控制器
        UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:[[MyViewController alloc]init]];
        self.window.rootViewController = navi;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    MyViewController.m

    #import "MyViewController.h"
    #import "BViewController.h"
    @interface MyViewController ()
    @end
    @implementation MyViewController
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        BViewController *bvc = [[BViewController alloc]init];
      //如果希望BVC 也在 管理MyViewController的导航控制器下
      //就要获取 管理当前 MyViewController 的导航控制器,让后在该导航控制器下 推出一个新的界面
        [self.navigationController pushViewController:bvc animated:YES];
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        //设置导航条上的标题
        self.navigationItem.title = @"登陆";
        self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    }
    

    BViewController.m

    #import "BViewController.h"
    #import "CViewController.h"
    @interface BViewController ()
    @end
    @implementation BViewController
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        //回到上一个 子控制器界面
        //[self.navigationController popViewControllerAnimated:YES];
        
        
        //获取导航控制器下的所有 子控制器 rootViewController是该数组下标为0的元素
    //    NSArray<UIViewController*> *vcs =self.navigationController.viewControllers;
    //    UIViewController *myVc = vcs[0];
        //回到指定控制器界面
        //[self.navigationController popToViewController:myVc animated:YES];
        //回到 导航控制器 的rootViewController
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    - (IBAction)gotoCVC:(id)sender {
        CViewController *cvc = [[CViewController alloc]init];
        UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:cvc];
        [self presentViewController:navi animated:YES completion:nil];
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    }
    

    导航栏的配置

    @implementation MyViewController
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        self.navigationController.toolbarHidden = ! self.navigationController.toolbarHidden;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        /******************** 中间  ************************/
        self.view.backgroundColor = [UIColor redColor];
        //设置当前控制器导航条的内容
        //title(文本)/titleView(设置一个视图)
        self.navigationItem.title = @"导航条";
    
    //    UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    //    greenView.backgroundColor = [UIColor greenColor];
    //    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"所有电话",@"未接电话"]];
    //    CGRect frame = segment.frame;
    //    frame.origin.x =greenView.frame.origin.x + greenView.frame.size.width;
    //    frame.origin.y = (40 - frame.size.height) * 0.5;
    //    [segment addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    //    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, segment.frame.size.width + greenView.frame.size.width, 40)];
    //    [titleView addSubview:greenView];
    //    [titleView addSubview:segment];
        //设置中间视图
        //self.navigationItem.titleView = titleView;
         /******************** 左边  ************************/
        //leftBarButtonItem /leftBarButtonItems
       UIBarButtonItem *item1= [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:nil action:nil];
        UIBarButtonItem *item2= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
        //创建一个不受影响的图片对象
        UIImage *image = [[UIImage imageNamed:@"timg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:nil action:nil];
        self.navigationItem.leftBarButtonItems = @[item2,item3];
        /***************************右边*****************************/
        UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(gotoBVC:)];
        UIBarButtonItem *item5 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
        self.navigationItem.rightBarButtonItems = @[item4,item5];
        /*以上部分 是设置 navigationItem, navigationItem的设置在当前的控制器生效
         下面的部分 是设置 navigationBar, navigationBar是导航控制器上得到航条,一旦设置,导航控制器下地所有控制器全部会改变*/
        //设置导航条样式
        UINavigationBar *naviBar = self.navigationController.navigationBar;
        naviBar.barStyle = UIBarStyleBlack;
        //设置导航上图片不透明 YES为半透明 NO为不透明 默认为YES
        naviBar.translucent = NO;
        //修改 两边 按键的 颜色
        naviBar.tintColor = [UIColor greenColor];
        //设置导航条背景
        naviBar.barTintColor = [UIColor yellowColor];
        //设置导航条上得背景图片
        //[naviBar setBackgroundImage:[UIImage imageNamed:@"psc-1"]forBarMetrics:UIBarMetricsDefault];
        [naviBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
        //设置 导航条下 阴影图片
        [naviBar setShadowImage:[[UIImage alloc]init]];
        //设置导航控制器下面的 toolBar
        //toolBar默认是隐藏的 如果需要使用 必须设置显示出来
        self.navigationController.toolbarHidden = NO;
        self.navigationController.toolbar.barStyle = UIBarStyleBlack;
        self.navigationController.toolbar.tintColor = [UIColor greenColor];
        UIBarButtonItem *toolBarItem1 = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:nil action:nil];
        UIBarButtonItem *toolBarItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:nil action:nil];
         UIBarButtonItem *toolBarItem3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];
        //创建木棍特效(UIBarButtonItem样式)
        UIBarButtonItem *fixItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        fixItem1.width = 20;
         UIBarButtonItem *fixItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        fixItem1.width = 50;
        //创建弹簧特效(UIBarButtonItem样式)
        UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        self.toolbarItems = @[fixItem1,toolBarItem1,fixItem2,toolBarItem2,flexibleItem,toolBarItem3,fixItem1];  
    }
    -(void)valueChanged:(UISegmentedControl*)sender{
        switch (sender.selectedSegmentIndex) {
            case 0:
                NSLog(@"查看所有通话");
                break;
            case 1:
                NSLog(@"查看未接通话");
                break;
        }
    }
    -(void)gotoBVC:(id)sender{
        BViewController *bvc = [[BViewController alloc]init];
        [self.navigationController pushViewController:bvc animated:YES];
    }
    

    NSTimer定时器

    设定一定时间后,自动调用某方法

    • 使用:使用工厂方法schedule....创建定时器,并且使用此方法创建完毕后,定时器会立即启动
    • 使用invalidate方法销毁定时器

    MyViewController.m

    @interface MyViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *imageView1;
    @property (weak, nonatomic) IBOutlet UIImageView *imageView2;
    @property(nonatomic,strong)NSTimer *timer1;
    @end
    
    @implementation MyViewController
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
    //    [NSTimer scheduledTimerWithTimeInterval:0.5 repeats:YES block:^(NSTimer * _Nonnull timer) {
    //        NSLog(@"定时器到时了");
    //    }];
        //[NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)#> invocation:<#(nonnull NSInvocation *)#> repeats:<#(BOOL)#>]
        //使用 scheduled 开头的工厂方法,会创建定时器对象,并把自己添加到当前的事件循环中
        self.timer1 = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(run:) userInfo:nil repeats:YES];
        NSTimer *timer = [NSTimer timerWithTimeInterval:0.05 target:self selector:@selector(run2:) userInfo:nil repeats:YES];
        //将定时器添加到 当前循环事件中 定时器才开始运行
        [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
    }
    -(void)run:(NSTimer*)timer
    {
         NSLog(@"定时器到时了");
        CGPoint center = self.imageView1.center;
        center.y += 5;
        self.imageView1.center = center;
        if(self.imageView1.center.y >= 450)
        {//销毁定时器
            [self.timer1 invalidate];
        }
    }
    -(void)run2:(NSTimer*)timer
    {
        NSLog(@"定时器到时了");
        CGPoint center = self.imageView2.center;
        center.y += 5;
        self.imageView2.center = center;
    }
    

    相关文章

      网友评论

          本文标题:ios(常用控件)UIStepper、UISlider、UISw

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