UIView

1、UIView是所有界面UI类控件的父类。UIView类的对象负责屏幕上一个矩形区域的显示和行为动作。

UIView类(视图类)负责管理屏幕上的一块矩形区域,包括这个区域内的显示样式,比如背景颜色、大小、以及行为动作,例如监测用户点击等触碰事件。总体来讲,视图类的主要作用有以下3个方面。

样式显示与动画:负责自身矩形区域内样式的显示,以及某些属性(大小、位置、角度)变化时的动画过渡效果。
布局与子视图管理:管理子视图。
事件处理:接收并响应用户的触摸事件。

UIView与UIViewController紧密协作,UIViewController负责UIView的加载与卸载。

UIView继承自UIResponder,因此UIView可以响应用户交互。另外,一些常用控件都继承自UIView。需要特别说明的是,窗口(UIWindow)也是继承自UIView,窗口可以认为是一个特殊的View。

// 最常使用的UIView属性
@property(nonatomic) CGrect    frame;
@property(nonatomic) CGrect    bounds;
@property(nonatomic) CGPoint   center;
@property(nonatomic) CGFloat   alpha;
@property(nonatomic) CGAffineTransform    transform;
@property(nullable, nonatomic, copy)  UIColor  *backgroundColor;
@property(nonatomic, getter=isHidden) BOOL     hidden;

// UIView管理子视图的常用属性和方法
@property(nullable, nonatomic, readonly) UIView    *superview;
@property(nullable, nonatomic, copy)     NSArray   *subviews;
@property(nullable, nonatomic, readonly) UIWindow  *window;
-(void)removeFromSuperview;
-(void)addSubview:(UIView *)view;
-(void)bringSubviewToFront:(UIView *)view;
// UIView有关动画的属性和方法
+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;

2、视图UIView的外观

1.基本样式:背景颜色,透明度及是否隐藏
// 背景颜色属性BackgroundColor
@property(nullable, nonatomic, copy)  UIColor  *backgroundColor;
// 透明度alpha
@property(nonatomic)  CGFloat    alpha;
// 是否隐藏
@property(nonatomic, getter=isHidden) BOOL hidden;
2.位置与大小:Frame、Bounds、Center
@property(nonatomic) CGRect    frame;
@property(nonatomic) CGRect    bounds;
@property(nonatomic) CGPoint   center;

Frame:视图在其父视图坐标系中的位置和大小。建议在控件初始化之后,紧接着就去设置Frame,设置完成后,假如涉及修改控件的位置、大小等,就不要再去修改Frame了。
Bounds:视图在其自身的坐标系中的位置和大小。Bounds属性中,视图的bounds.origin始终是(0,0),因此bounds属性最核心的作用是设置视图的大小,即bounds.size,当需要去修改视图大小的时候,可以修改bounds.size。
Center:视图中心点在父视图坐标系中的坐标,当需要修改视图对象的位置时,可以修改Center属性。

-(void)viewDidLoad {
    [super viewDidLoad];
    // 添加yellowView到控制器视图self.view
    UIView *yellowView = [[UIView alloc] init];
    yellowView.frame = CGRectMake(100, 100, 200, 200);
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view.addSubview:yellowView];
}

3、视图UIView的形变

1.位移
// 当需要修改视图对象的位置时(上移、下移、左移、右移),可以通过修改视图对象的center属性。
-(IBAction)move:(id)sender {
    CGPoint point = self.mvView.center;
    point.x -= 10;
    self.mvView.center = point;
    // 注意:不能直接修改结构体变量的值,即self.myView.center.x -=10是不合法的。
}
2.放大、缩小
// 第一种方法可以修改视图对象的bounds.size属性;第二种方法是直接修改视图对象的transform属性,即让视图对象进行一次形变操作。
CG_EXTERN CGAffineTransform CGAffineTransformScale(CGAffineTransform t, CGFloat sx, CGFloat sy);  // sx以及sy是在X轴以及Y轴两个方向上放大的比例。
-(IBAction)scale:(id)sender {
    self.myView.transform=CGAffineTransformScale(self.myView.transform, 1.1, 1.1);
}
3.旋转
// 通过修改视图对象的transform属性,同样可以实现视图的顺时针旋转以及逆时针旋转
CG_EXTERN CGAffineTransform CGAffineTransformRotate(CGAffineTransform t, CGFloat angle);
CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
-(IBAction)rotate:(id)sender {
    self.myView.transform=CGAffineTransformRotate(self.myView.transform, M_PI_4);
}
4.重置transform属性
// 当需要重置transform属性时,可以使用CGAffineTransformIdentity命令,
// 但需要注意的是:如果之前对视图对象的frame、center、bounds属性进行了修改,若需要完全重置一个视图的样式,
// 除了重置transform属性之外,还需要重置frame、center、bounds属性,因此在对视图进行形变操作之前,有必要保存视图的原始状态以便恢复。
-(IBAction)reset:(id)sender {
    self.myView.transform=CGAffineTransformIdentity;
}

4、视图UIView的层次关系

// 有关视图层次关系的常用属性
@property(nullable, nonatomic, readonly)  UIView *superView; // 父视图
@property(nullable, readonly, copy)  NSArray *subViews;     // 所有的子视图
@property(nullable, nonatomic, readonly)  UIWindow *window;  // 视图所在的window
// 有关视图层次关系控制的常用方法
-(void)addSubview:(UIView *)view;           // 添加子视图
-(void)bringSubviewToFront:(UIView *)view;  // 把子视图移到最前显示
-(void)sendSubviewToBack:(UIView *)view;    // 把子视图移到最后显示
-(void)removeFromSuperview;                 // 从父视图中移除

// 设置层次关系
-(IBAction)changeLayer:(id)sender {
    // 从subviews中取出视图
    UIView *lastView = (UIView *)[self.view.subviews lastObject];
    [self.view sendSubviewToBack:lastView];
}

5、视图UIView动画

1. UIView支持动画的属性
@property(nonatomic)  CGRect  frame;
@property(nonatomic)  CGRect  bounds;
@property(nonatomic)  CGPoint center;
@property(nonatomic)  CGFloat alpha;
@property(nonatomic)  CGAffineTransform  transform;
@property(nullable, nonatomic, copy)  UIColor *backgroundColor;
2.常用动画播放的方法
+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;
exp:
-(IBAction)playAnimation:(id)sender {
    [UIView animateWithDuration:1.0 animations:^{
        self.myView.backgroundColor = [UIColor redColor];
        self.myView.transform = CGAffineTransformScale(self.myView.transform, 1.2, 1.2);
    } completion:^(BOOL finished){
        [UIView animateWithDuration:1.2 animations:^{
            self.myView.backgroundColor = [UIColor greenColor];
            self.myView.transform = CGAffineTransformIdentity;
        }]
    }];
}

6、响应用户交互事件

1.与用户交互事件相关的属性
// 是否支持用户交互
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
// 是否支持多点触控
@property(nonatomic, getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled;
2.添加手势
// 获取视图上所有手势
@property(nullable, nonatomic, copy) NSArray *gestureRecongnizers;
// 添加手势
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer;
// 移除手势
-(void)removeGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer;

-(void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.myView];
    // 添加长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction)];
    [self.myView addGestureRecognizer:longPress];
    // 添加点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.myView addGestureRecognizer:tap];
}
// 长按触发动作
-(void)longPressAction {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"长按" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
}
// 点击触发动作
-(void)tapAction {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"点击" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
}
3.自定义视图类实现touches系列方法
// 在视图管理范围内开始点击时调用
-(void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event;
// 在视图管理区域中移动时反复调用
-(void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event;
// 在视图管理范围内结束点击时调用
-(void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event;

7、内容模式contentMode

视图的contentMode属性决定了边界变化和缩放操作作用到视图上产生的效果。视图在屏幕上显示后,渲染后的内容会被缓存在视图的图层(layer)上,当视图的大小发生变化时,UIKit并不强制对视图进行重画,而是根据其contentMode属性决定如何显示缓存内容。由于这种机制的存在,当修改视图的大小时,可以提升性能。

contentMode经常用于图像视图UIImageview。当希望在App中调整控件的尺寸时,务必优先考虑使用contentMode,这样做可以在视图的外观发生形变时,避免编写定制的描画代码。这是因为:每个图像视图的关联图像都由CoreAnimation来缓存,因此不需要编写代码就可以支持动画,即不需要再次调用drawRect:方法,从而大大提高了性能。
在开发过程中,当发生以下两种情况时,会使用到contentMode:
改变frame或bounds中的高度或宽度。
修改transform属性。
默认情况下,contentMode的值被设置为UIViewContentModelScaleToFill,意味着视图内容(一般情况下是一张图片)总是填充整个视图划定的矩形区域,有可能这张图片会被拉伸。

@property(nonatomic) UIViewContentMode contentMode;

8、图片拉伸

除了通过Xcode的Slicing功能之外,UIImage还提供了resizableImageWithCapInsets:方法

// 需要传入一个UIEdgeInsets类型的参数capInsets,通过该参数可以设置在上、下、左、右四个方向需要保留的图像大小范围。
-(UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
-(void)viewDidLoad {
    [super viewDidLoad];
    UIIamge *image = [UIImage imageNamed:@"iamge"];
    [image resiableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15)];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:iamge];
    imageView.frame = CGRectMake(30, 100, 250, 150);
    [self.view addSubview:imageView];
}

9、使用代码创建自定义UIView

// 在.m文件中,实现initWithFrame:方法。在该方法中,对视图的属性进行设置,并创建、添加子视图或者添加手势对象。
-(instancetype)initWithFrame:(CGRect)frame
{
    NSLog(@"%s", __func__);
    self = [super initWithFrame:frame];
    if (self) {
        // 定制View
        self.backroundColor = [UIColor blueColor];
        self.alpha = 0.5;
        self.userInteractionEnabled = YES;
        self.mutipleTouchEnabled = YES;
        self.clipsToBounds = YES;  //  控制子视图不能超出父视图的范围
        [self subView] // 添加子视图
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpressAction)];
        [self addGestureRecognizer:longPress];
    }
    return self;
}
// 添加子视图属性,并进行懒加载。
// 子视图懒加载
-(UIView *)subView {
    if (-subView == nil) {
        UIView *view = [[UIView alloc] init];    
        view.frame = CGRectMake(0, 0, 50, 50);
        view.backgroundColor = [UIColor redColor];
        [self addSubview:view];
        _subView=view;
    }
    return _subView;
}
// 当需要对视图类的外观做自定义绘图的时候,实现drawRect:方法
-(void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect myFrame = self.bounds;
    CGContextSetLineWidth(context, 10);
    CGRectInset(myFrame, 5, 5);
    [[UIColor greenColor] set];
    UIRectFrame(myFrame);
}
// 如果视图需要响应用户交互,如点击等,可以为视图添加手势或者实现UIResponder类的touches系列方法。

10、XIB创建自定义UIView

 

11、控件改变坐标系(convertRect:)

// 坐标系转换的方法
-(CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
-(CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
-(CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
-(CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
-(void)viewDidLoad {
    [supre viewDidLoad];
    CGRect newFrame = [self.gratView convertRect:self.imageView.frame toView:self.view];
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值