IOS 开发(四) — UIKIT 组件学习

前言

UIKit 提供了构建App,最核心的模块 ------ 主要处理提供视图展示内容,以及和用户的交互 基于 MVC 的设计模式 ------ 系统封装视图和逻辑,开发者提供数据驱动 交互 ------ 通过 Delegate 方式活着 Target-Action 方式

一、 UILabel

展示一行或者多行文字的视图

  • text: @"IOS开发"
  • font: UIFont (大小、粗体、斜体)
  • textColor: 文字颜色
  • textAlignment: 对其方式 (居中、左、右对齐)
  • numberOfLines: 最大展示行数
  • lineBreakMode: NSLineBreakByClipping 截断、NSLineBreakByTruncatingMiddle 中间省略号 ...
  • -(void) sizeToFit 根据内容长度自动调整大小

1-1、创建一个新的 视图

替换掉 ViewController 中的系统的UITableViewCell, 然后去掉系统默认的文字和图标

创建自己的UILabel

objc 复制代码
#import "GTNormalTableViewCell.h"

@interface  GTNormalTableViewCell()

// 设置 Label 属性
@property(nonatomic,strong,readwrite) UILabel *titleLabel;
@property(nonatomic,strong,readwrite) UILabel *sourceLabel;
@property(nonatomic,strong,readwrite) UILabel *commentLabel;
@property(nonatomic,strong,readwrite) UILabel *timeLabel;

@end

@implementation GTNormalTableViewCell


// 重写初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self.contentView addSubview:({
            self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, 300, 50)];
//            self.titleLabel.backgroundColor = [UIColor redColor];
            self.titleLabel.font = [UIFont systemFontOfSize:16];
            self.titleLabel.textColor = [UIColor blackColor];
            self.titleLabel;
        })];
        
        [self.contentView addSubview:({
            self.sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 50, 20)];
//            self.sourceLabel.backgroundColor = [UIColor redColor];
            self.sourceLabel.font = [UIFont systemFontOfSize:12];
            self.sourceLabel.textColor = [UIColor grayColor];
            self.sourceLabel;
        })];
        
        [self.contentView addSubview:({
            self.commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 80, 50, 20)];
//            self.commentLabel.backgroundColor = [UIColor redColor];
            self.commentLabel.font = [UIFont systemFontOfSize:12];
            self.commentLabel.textColor = [UIColor grayColor];
            self.commentLabel;
        })];
        
        [self.contentView addSubview:({
            self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 80, 50, 20)];
//            self.timeLabel.backgroundColor = [UIColor redColor];
            self.timeLabel.font = [UIFont systemFontOfSize:12];
            self.timeLabel.textColor = [UIColor grayColor];
            self.timeLabel;
        })];
    }
    return self;
};

1-2、创建一个方法暴露出去,创建 tableViewCell 的时候进行调用

GTNormalTableViewCell.m

objc 复制代码
// 创建一个方法  在.h 中暴露
-(void) layoutTableViewCell{
  self.titleLabel.text = @"极客时间IOS开发";
  self.sourceLabel.text=@"极客时间";
  [self.sourceLabel sizeToFit];
  
  self.commentLabel.text=@"1888评论";
  [self.commentLabel sizeToFit];
  self.commentLabel.frame = CGRectMake(self.sourceLabel.frame.origin.x + self.sourceLabel.frame.size.width + 15, self.commentLabel.frame.origin.y , self.commentLabel.frame.size.width,self.commentLabel.frame.size.height);
  
  self.timeLabel.text=@"三分钟前";
  [self.timeLabel sizeToFit];
  
  self.timeLabel.frame = CGRectMake(self.commentLabel.frame.origin.x + self.commentLabel.frame.size.width + 15, self.timeLabel.frame.origin.y , self.timeLabel.frame.size.width,self.timeLabel.frame.size.height);
}

GTNormalTableViewCell.h

objc 复制代码
-(void) layoutTableViewCell;

1-3、每次创建的时候进行设置

objc 复制代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      GTNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
      if(!cell){
          cell = [[GTNormalTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
      }
      // 每次的 TableView 需要布局的时候调用这个方法
      [cell layoutTableViewCell];
      return cell;
  }

二、UIImage 图片展示

2-1、 IOS 中图片数据都会被系统封装成 UIImage

调用 UIImage 对象进行视图展示,在上下文绘制

objec 复制代码
UIImageView.image = UIImage

2-2、 设置一组静态图片,成为动图

objc 复制代码
UIImageView.animationImages = @[UIImage,UIImage...]
UIImageView.animationDuration = 1
[UIImageView startAnimation]

2-3、UIViewContentMode 设置 UIImage 填充方式

objc 复制代码
// 添加属性
@property(nonatomic,strong,readwrite) UIImageView *rightImageView;
//...

// 自定义cell
[self.contentView addSubview:({
  self.rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(310, 15, 70, 70)];
  self.rightImageView.contentMode = UIViewContentModeScaleAspectFit;
  self.rightImageView;
})];

//...
// 执行方式时执行
self.rightImageView.image = [UIImage imageNamed:@"setting"];

三、 UIButton

3-1、UIButton

可以进行展示、交互、点击、拖拽、手势

objc 复制代码
// 添加属性
@property(nonatomic,strong,readwrite) UIButton *deleteButton;
//...

// 添加按钮
[self.contentView addSubview:({
    self.deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(290, 88, 30, 20)];
    [self.deleteButton setTitle:@"X" forState: UIControlStateNormal];
    [self.deleteButton setTitle:@"V" forState: UIControlStateHighlighted];
    self.deleteButton.backgroundColor = [UIColor orangeColor];
    self.deleteButton;
})];

3-2、 Target-Action 设计模式

  • 当某个事件触发时,调用对应target对象的相应方法
  • 传值限制比较多
objc 复制代码
[self.button addTarget:self
                 action:@selector(clickButton:)
          forControlEvents:UIControEventTouchUpInside];

组件内部监听事件、target执行action,缺点不利于传值,优点利于组件化

UIControlEvents

  • UIButton 通过Target-Action的模式,处理点击逻辑
  • 系统封装用户操作事件
  • 对应事件开发者实现自定义的方法
objc 复制代码
 // 添加 target action
 [self.deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
 
 
-(void) deleteButtonClick{
      NSLog(@"deleteButtonClick");
}

3-3、UIControl

  • UIControl 作为父类,继承了所有子类可能用到的事件
  • 系统级封装的可交互视图,都继承自 UIControl

3-4、UIUIGestureRecognizer 识别用户屏幕中的触摸

手势封装

  • UITapGestureReconizer
  • UIPinchGestureReconizer
  • UIRotationGestureReconizer
  • UISwipeGestureReconizer
  • UIPanGestureReconizer
  • UILongPressGestureReconizer

1、创建手势 2、设置响应处理 3、在视图中添加

objc 复制代码
// 创建一个新的 view 添加手势demo
[view addSubview:({
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    view.backgroundColor = [UIColor yellowColor];
    // 创建手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewClick)];
    // 添加到view
    [view addGestureRecognizer:tapGesture];
    view;
})];

// ------
// 执行方法
- (void)viewClick{
    NSLog(@"点击view");
}

3-5、 UIGestureRecognizerDelegate

处理复杂手势操作

  • 是否响应手势
  • 是否支持多手势
  • 多个手势冲突时如何处理
obgc 复制代码
// 将手势的 delegate 设置为self
tapGesture.delegate = self;

// 声明 UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    // 手势是否需要识别
    return NO; 
};

此时点击将不再出发 viewClick 事件

四、UIAlertView

UIAlertView is deprecated.

相关推荐
慧一居士34 分钟前
flex 布局完整功能介绍和示例演示
前端
DoraBigHead36 分钟前
小哆啦解题记——两数失踪事件
前端·算法·面试
一斤代码6 小时前
vue3 下载图片(标签内容可转图)
前端·javascript·vue
中微子6 小时前
React Router 源码深度剖析解决面试中的深层次问题
前端·react.js
光影少年6 小时前
从前端转go开发的学习路线
前端·学习·golang
中微子7 小时前
React Router 面试指南:从基础到实战
前端·react.js·前端框架
3Katrina7 小时前
深入理解 useLayoutEffect:解决 UI "闪烁"问题的利器
前端·javascript·面试
前端_学习之路8 小时前
React--Fiber 架构
前端·react.js·架构
伍哥的传说8 小时前
React 实现五子棋人机对战小游戏
前端·javascript·react.js·前端框架·node.js·ecmascript·js
qq_424409198 小时前
uniapp的app项目,某个页面长时间无操作,返回首页
前端·vue.js·uni-app