zoukankan      html  css  js  c++  java
  • UI常用控件总结(一)

    UIView

        //设置view
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 100)];
        //设置背景色
        view.backgroundColor = [UIColor lightGrayColor];
        //设置坐标跟大小
        view.frame = CGRectMake(0, 0, kWidth-20, 100);
        //设置大小(不管坐标是多少,位置永远固定不变,但该位置是它所特有的位置,并不是之前的位置)
        view.bounds = CGRectMake(0, 0, 200, 200);
        //超过边界隐藏,默认是不隐藏(隐藏谁即是谁的属性)
        view.clipsToBounds = YES;
        //获取frame  bounds
        CGRect frame = view.frame;
        CGRect bounds = view.bounds;
        NSLog(@"%@",NSStringFromCGRect(frame));
        NSLog(@"%@",NSStringFromCGRect(bounds));
        //设置tag值(只要继承UIView均可设置tag值)
        view.tag = 101;
        //设置圆角
        view.layer.cornerRadius = 10;
        //设置阴影颜色(不要忘掉.CGColor)
        view.layer.shadowColor = [UIColor blackColor].CGColor;
        //设置阴影的不透明度(0~1)
        view.layer.shadowOpacity = 1;
        //设置阴影的圆角
        view.layer.shadowRadius = 5;
        //设置阴影的大小(默认是(0,-3))
        view.layer.shadowOffset = CGSizeMake(5, 10);
        //设置边界
        view.layer.borderColor = [UIColor redColor].CGColor;
        //设置边界宽度
        view.layer.borderWidth = 2; 
        //添加
        [self.view addSubview:view];
    • pragma mark 点击空白处进行的事件
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
        //获取所有子视图(返回值是一个数组)
        NSArray *subViewsArray = [self.view subviews];
        NSLog(@"%@",subViewsArray);
        //取出所有子视图的最上面一个
        UIView *lastView = [subViewsArray lastObject];
        NSLog(@"%@",NSStringFromCGRect(lastView.frame));
        //将最上面一个移动到最后(即数组的第0个,谁是父视图谁调用)
        [self.view sendSubviewToBack:lastView];
        NSArray *array = [self.view subviews];
        NSLog(@"%@",array);
        //根据tag值调取视图(父视图调用)
        UIView *tagView = [self.view viewWithTag:101];
        //将视图隐藏的三种方法
        //1.改变透明度
        tagView.alpha = 0.5;
        //2.从父视图移除
        [self.view removeFromSuperview];
        //3.隐藏
        tagView.hidden = !tagView.hidden;
    
        //计时器的开启
        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(change) userInfo:nil repeats:YES];
    
    }

    UIButton

       //设置button
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 100, 50)];
        //设置按钮的圆角
        button.layer.cornerRadius = 0;
        //居中
        button.center = self.view.center;
        //设置颜色
        button.backgroundColor = [UIColor whiteColor];
        //设置图标(只有添加了图标或背景图片,才会有点击效果,并且该方法是以图标形式添加的)
        [button setImage:[UIImage imageNamed:@"channel_friend_love"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"hongxin"] forState:UIControlStateSelected];
        //设置图标(该方法添加的是铺满的背景图)
        [button setBackgroundImage:[UIImage imageNamed:@"bthui2"] forState:UIControlStateNormal];
        //设置标题
        [button setTitle:@"UIControlStateNormal" forState:UIControlStateNormal];
        [button setTitle:@"UIControlStateSelected" forState:UIControlStateSelected];
        [button setTitle:@"UIControlStateDisabled" forState:UIControlStateDisabled];
        //设置字体的大小
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        //设置标题颜色
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //标题水平对齐(对齐方式以此类推)
        [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
        //移动图片(CGFloat top, left, bottom, right  在原有基础上增加或减少的相对于边缘的距离)
        [button setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 0, 0)];
        //移动标题(CGFloat top, left, bottom, right  在原有基础上增加或减少的相对于边缘的距离)
        [button setTitleEdgeInsets:UIEdgeInsetsMake(10, -10, 0, 0)];
        //监视点击事件
    //    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        //添加
        [self.view addSubview:button];
    • pragma mark 点击事件,控制器切换

    -(void)click{
      //初始化控制器
      MyViewController *mVC = [MyViewController new];
      //控制器切换效果
      [mVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
      [self presentViewController:mVC animated:YES completion:^{
    
      }];
    }
    • pragma mark 点击空白处,如果定时器对象为空,则重新启动定时器,如果定时器对象不为空,则让定时器对象失效(invalidate方法)
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
        if (_timer == nil) {
            _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(change) userInfo:nil repeats:YES];
        }else{
            [_timer invalidate];
            _timer = nil;
        }
    }

    UISlider

      //设置slider(高度不起作用)
        UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
        //居中
        slider.center = self.view.center;
        //slider的默认范围是0~1,可通过value值来改变范围
        //设置最小值
        slider.minimumValue = 0;
        //设置最大值
        slider.maximumValue = 10;
        //设置当前值
        slider.value = 5;
        //设置圆点的颜色
        [slider setThumbTintColor:[UIColor redColor]];
        //设置圆点的图片
        [slider setThumbImage:[UIImage imageNamed:@"hongxin"] forState:UIControlStateNormal];
        //设置已跑轨道的颜色(同样也可设置图片)
        [slider setMinimumTrackTintColor:[UIColor blueColor]];
        //设置未跑轨道的颜色(同样也可设置图片)
        [slider setMaximumTrackTintColor:[UIColor redColor]];
        //添加
        [self.view addSubview:slider];
        //开始计时器,让进度条动起来
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(move) userInfo:nil repeats:YES];
    • pragma mark 改变进度值
    -(void)move{
    
        _slider.value = _slider.value++;
    
    }

     

     
  • 相关阅读:
    chrome headless+selenium+python+(ubuntu 16.04/centos7) 下的实现
    selenium + phantomJS 常用方法总结
    Rabbitmq consumer端超时报错
    python 守护进程
    如何在GitBook中使用Git管理
    Java#Spring框架下注解解析
    Linux 之Ubuntu在VM中安装(桌面版)
    Linux----Ubuntu虚拟机(VMWare)学习
    Python tuple元组学习
    Python 编解码
  • 原文地址:https://www.cnblogs.com/my-garden/p/5569333.html
Copyright © 2011-2022 走看看