UI基础

  • UILable
    • numberofLines属性设置文字的行数,0为自动计算
    • 当文字行数超过宽度时会自动换行 超过高度时会显示省略号
  • UIButton
    • 该对象初始化需要使用静态方法UIButton* btn = [UIButton buttonWidthType:$BtnType];
    • 设置title需要用setTitle forState 例:[btn setTitle:@"click me" forState:UIControlStateNormal];
    • 设置圆角
    //设置超出图形范围剪裁
    btn.clipToBounds = YES;
    //设置图形布局的角半径为10;
    btn.layer.cornerRadius = 10;
    
    • 绑定事件函数需要用到选择器@selector()
    //例子:
     [btn addTarget:self action:@selector(sayHey:) forControlEvents:UIControlEventTouchDown];
     - (void) sayHey:(UIButton*)btn{
      NSLog(@"%@",btn);
      NSLog(@"hey");
      }
    
    • 定时器
    //创建定时器
    [NSTimer scheduledTimerWithTimeInterval:2 repeats:NO block:^(NSTimer * _Nonnull timer) {
          NSLog(@"begin");
      }];
    //也可以用@selector方式去创建 创建完即运行 不需要手动调用
    //`注`: NSTimeInterval(第一个参数)是一个以秒为单位的间隔.
    
    • UISwitch
      • 使用addTarget来绑定函数 用isOn来判断控件状态
   UISwitch* sw = [[UISwitch alloc] initWithFrame:CGRectMake(30, 50, 200, 100)];
  [sw addTarget:self action:@selector(pressTarget:) forControlEvents:UIControlEventValueChanged];
  - (void) pressTarget:(UISwitch*)sw{

   NSLog(@"%i",sw.isOn);
   }
   ```
 * UISlider(滑块)
 ```objectivec
 UISlider* us = [[UISlider alloc] initWithFrame:CGRectMake(30, 50, 200, 200)];
  us.value = 20;
  us.maximumValue = 200;
  us.minimumValue = 10;
  us.tintColor = [UIColor greenColor];
  //圆圈样式
  us.thumbTintColor = [UIColor purpleColor];
  [us addTarget:self action:@selector(pressTarget:) forControlEvents:UIControlEventValueChanged];
  [view addSubview:us];
  - (void) pressTarget:(UISlider*)us{

   NSLog(@"%.0f",us.value);
}
  • 步进器(UIStepper)
- (instancetype)init{
 mainPage* view = [super init];
 UIStepper* step = [[UIStepper alloc] initWithFrame:CGRectMake(30, 50, 200, 200)];
 step.minimumValue = 10;
 step.maximumValue = 100;
 step.stepValue = 10;
 step.value = 8;
 UILabel* ul = [[UILabel alloc] initWithFrame:CGRectMake(80, 50, 200, 200)];
 ul.text = [NSString stringWithFormat:@"%i",8];
 ul.tag = 200;
 [step addTarget:self action:@selector(pressTarget:) forControlEvents:UIControlEventValueChanged];
 [view addSubview:step];
 [view addSubview:ul];
 return view;
 }
 - (void) pressTarget:(UIStepper*)us{
 UILabel* ul = [self viewWithTag:200];
 ul.text =[NSString stringWithFormat:@"%.0f",us.value];
 }
  • UISegementControl(分栏控制器)
- (instancetype)init{
 mainPage* view = [super init];
 UISegmentedControl* usc = [[UISegmentedControl alloc] initWithFrame:CGRectMake(30, 50, 300,50)];
 [usc insertSegmentWithTitle:@"first" atIndex:0 animated:YES];
 [usc insertSegmentWithTitle:@"second" atIndex:1 animated:YES];
 [usc addTarget:self action:@selector(pressTarget:) forControlEvents:UIControlEventValueChanged];
 [view addSubview:usc];
 return view;
 }
 - (void) pressTarget:(UISegmentedControl*)usc{
 NSLog(@"%li",usc.selectedSegmentIndex);
 }
  • 等待指示器(loading效果)
UIActivityIndicatorView* ac =  [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(30, 120, 300,50)];
//必须设置一种风格样式
ac.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[ac startAnimating];
//[ac stopAnimating]; 关闭加载动画
  • UITextFiled

//textfiled样式
tx.borderStyle = UITextBorderStyleRoundedRect;
tx.keyboardType = UIKeyboardTypeNumberPad;
//    开启密码保护
tx.secureTextEntry = YES;
//回收键盘
//viewController.m 点击空白处回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
 UITextField* tx = [self.view viewWithTag:201];
 [tx resignFirstResponder];
}

//监听事件(需要实现UITextFieldDelegate协议)
@interface mainPage : UIView
//给控件指定代理类
tx.delegate = self;
//选择需要监听的事件进行实现
- (void)textFieldDidBeginEditing:(UITextField *)textField{
 NSLog(@"%@",@"开始编辑");
}
  • UIScrollView 滚动视图

 mainPage* view = [super init];
 UIScrollView* usv = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 //设置内容画布大小
 usv.contentSize = CGSizeMake(320*5, 570);
 //创建子视图加入到滚动视图中
 UIView* uv = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 570)];
 uv.backgroundColor = [UIColor greenColor];
 [usv addSubview:uv];
 [view addSubview:usv];

  • UIImage
UIImage* imgData = [UIImage imageNamed:@"test1.jpg"];
 UIImageView* img = [[UIImageView alloc] initWithImage:imgData];
  • UIGestureRecognizer(手势基础事件)
//开启事件响应默认是NO
img.userInteractionEnabled = YES;
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAct:)];
//    几次点击时触发
gesture.numberOfTapsRequired = 1;
//    需要几个手指点击触发
gesture.numberOfTouchesRequired = 1;
//    给视图添加响应事件
[img addGestureRecognizer:gesture];
- (void)tapAct:(UITapGestureRecognizer *)gestrue{
 UIView* image = gestrue.view;
 //开启动画
 [UIView beginAnimations:nil context:nil];
//    获取手势在视图中的位置
 CGPoint point = [gestrue locationInView:image];
 image.frame = CGRectMake(point.x, point.y, image.frame.size.width, image.frame.size.height);
 [UIView commitAnimations];
 NSLog(@"%@",NSStringFromCGPoint(point));
}
//注一个视图可以添加多个响应事件 但是一个响应事件不能给多个视图使用
  • UIGestureRecognizer(手势高级事件)
    • UIRotationGestureRecognizer UIPinchGestureRecognizer
   - (instancetype)init{
    mainPage* view = [super init];
    UIImage* imgData = [UIImage imageNamed:@"test1.jpg"];
    UIImageView* img = [[UIImageView alloc] initWithImage:imgData];
    img.frame = CGRectMake(0, 0, 300, 500);
    [view addSubview:img];
    img.userInteractionEnabled = YES;
    UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotaAct:)];

    UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAct:)];
    [img addGestureRecognizer:rotation];
    [img addGestureRecognizer:pinch];
    return view;
}
- (void)pinchAct:(UIPinchGestureRecognizer *)gestrue{
    UIImageView* curView = (UIImageView*)gestrue.view;
//    在原来的矩阵基础上进行缩放大小的矩阵变换 参数2是x方向缩放比例 参数2是Y方向缩放比例
    curView.transform = CGAffineTransformScale(curView.transform, gestrue.scale, gestrue.scale);
    //做完一次变换后必须归为 不然会进行累加 缩放效果太大
    gestrue.scale = 1;
    NSLog(@"pinch");
}
- (void)rotaAct:(UIRotationGestureRecognizer *)gestrue{
    UIImageView* curView = (UIImageView*)gestrue.view;
    //    在原来的矩阵基础上进行缩放大小的矩阵变换
    curView.transform = CGAffineTransformRotate(curView.transform, gestrue.rotation);
    //做完一次变换后必须归为 不然会进行累加 缩放效果太大
    gestrue.rotation = 0;
    NSLog(@"rotate");
}

注:如果要实现多个手势事件同时响应的话需要实现代理
然后给手势对象的代理设置为实现了代理的类 然后实现shouldRecognizeSimultaneouslyWithGestureRecognizer方法
返回YES 即允许同时响应多个手势事件

  • 拓展手势
    • UIPanGestureRecognizer 平移手势
    • UILongPressGestureRecognizer 长按手势
    • UISwipeGestureRecognizer 滑动手势
  • xib使用
//AppDelegate.m                                                 //nibName要对应xib名称    mainBundle代表是根目录
self.window.rootViewController = [[ViewController alloc] initWithNibName:@"VCxib" bundle:[NSBundle mainBundle]];
  • 自动布局 autoresizingMask
//注意 如果视图要紧贴容器右侧即设置左边距为自动布局 其他也是
self.con.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
//注意 自动布局可以设置多个属性
self.con.autoresizingMask = UIViewAutoresizingFlexibleWidth;

你可能感兴趣的:(UI基础)