iOS UI学习之路02 - UIWindow、UIView详解
UIKit框架结构
构建和管理你的用户界面
捕获触摸和基于移动的事件
呈现文字和web内容
创建定制用户界面元素
UIView(视图)框架的结构图
Window和View的关系
UIWindow
重要知识
UIWindow的主要作用:a.作为UIWindow的最顶层容器,包含应用显示所有的UIView;b.传递触摸消息和键盘事件给UIView;
UIWindow的创建
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
self.window.backgroundColor = [UIColor redColor];
//设置根视图
ViewController *view1 = [[ViewController alloc] init];
self.window.rootViewController = view1;
self.window.windowLevel = UIWindowLevelNOrmal;
return YES;
}
常用属性
@property(NOnatomic) UIWindowLevel windowLevel; //窗口显示级别
@property(nullable, NOnatomic,strong) UIViewController *rootViewController; //根视图控制器
@property(NOnatomic,readonly,getter=isKeyWindow) BOOL keyWindow; //判断当前窗口是否为主窗口
常用方法
- (void)makeKeyWindow; //让当前UIWindow变成keyWindow(主窗口)
- (void)makeKeyAndVisible; //让当前UIWindow变成keyWindow,并显示出来。只有主窗口显示,子视图才会显示。
UIView(视图)
重要知识
主要功能:管理矩形区域里的内容、处理矩形区域中的事件、子视图的管理、实现动画UIView的子类也具有这些功能
一个视图只能有一个父视图,但可以有很多子视图
UIKit坐标系统
UIKit的默认坐标系统是把左上角设置为原点,横向向右横坐标递增,竖向向下纵坐标递增
视图的位置和大小通过frame属性来表达,UIView类有一个属性frame,类型为CGRect,是一个结构体。
struct CGRect{
CGPoint origin;
CGSize size;
}
struct CGPoint{
CGFloat x;
CGFloat y;
}
struct CGSize{
CGFloat width;
CGFloat height;
}
常用系统内部函数
CGPointMake(x, y) 返回CGPoint类型值
CGSizeMake(width, height) 返回CGSize类型
CGRectMake(x, y, width,height) 返回CGRect类型值
NSStringFromCGSize() 将CGSize结构体转化为字符串
NSStringFromCGPoint() 将CGPoint结构体转化为字符串
NSStringFromCGRect() 将CGRect结构体转化为字符串
UIVIew的创建
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
常用属性
@property(NOnatomic) CGRect frame; // 该view在父view坐标系统中的位置和大小,参照物是父视图的坐标系
@property(NOnatomic) CGRect bounds; // 该view在本坐标系中的位置和大小,改变这个值,可以设置本坐标系统的原点坐标和大小,改变后,会影响子view的起始位置,而不会影响自身
@property(NOnatomic) CGPoint center; // 该view的中心点在父view坐标系统中的位置和大小
@property(NOnatomic) CGAffineTransform transform; // 坐标系统
@property(NOnatomic,readonly) UIView *superview; // 父视图
@property(NOnatomic,readonly,copy) NSArray *subviews; // 所有子视图
@property(NOnatomic,readonly) UIWindow *window // 视图所在的窗⼝
@property(NOnatomic,copy) UIColor backgroundColor; // 背景颜色
@property(NOnatomic) CGFloat alpha; // 透明度 0~1
@property(NOnatomic,getter=isHidden) BOOL hidden; // 是否隐藏
常用方法
- (void)addSubview:(UIView *)view; // 添加一个子视图
- (void)bringSubviewToFront:(UIView *)view; // 将视图放到最上层
- (void)sendSubviewToBack:(UIView *)view; // 将视图放到最下层
- (void)removeFromSuperview; // 将视图从父视图中移除
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; //指定的层次位置插⼊入一个视图
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2; // 交换两个视图的层次位置
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; // 将一个视图放到一个视图下
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; // 将一个视图放到一个视图上
CGAffineTransForm(坐标系统变换类)
平移坐标系统
CGAffineTransform translation = CGAffineTransformMakeTranslation(0, 100); //平移坐标系统
CGAffineTransform translate = CGAffineTransformTranslate(self.myView.transform, 100,0); //基于已变换的坐标系再次平移
缩放坐标系统
CGAffineTransform scale = CGAffineTransformMakeScale(1 , 2); //缩放坐标系统 缩放 0~1 放大 > 1
CGAffineTransform scale = CGAffineTransformScale(self.myView.transform, 0.2, 0.2); //基于已变换的坐标系再次缩放坐标系统
旋转坐标系统
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI_4);//旋转坐标系统 按照弧度制PI作为参数
CGAffineTransform rotation = CGAffineTransformRotate( self.myView.transform, M_PI_4); //基于已变换的坐标系再次旋转坐标系统
连接仿射
CGAffineTransform transform1 = CGAffineTransformConcat(translation, rotation);//连接两种仿射方式
恢复到原始状态
CG_EXTERN const CGAffineTransform CGAffineTransformIdentity; //原始状态常量
UIView基本动画
// 1.基本动画
[UIView animateWithDuration:1 animations:^{
CGAffineTransform transform = CGAffineTransformTranslate(self.myView.transform, 0, -200);
_myView.transform = transform;
}];
// 2.动画完成后处理事情
[UIView animateWithDuration:1 animations:^{
_myView.center = self.view.center;
} completion:^(BOOL finish){
CGAffineTransform transform1 = CGAffineTransformRotate(self.myView.transform, M_PI_4);
_myView.transform = transform1;
}];
// 3.连续动画
[UIView animateWithDuration:1 delay:0 options:UIViewAnimatioNOptionCurveEaseIn animations:^{
_myView.center = self.view.center;
} completion:^(BOOL finished){
[UIView animateWithDuration:1 animations:^{
CGAffineTransform transform1 = CGAffineTransformRotate(self.myView.transform, M_PI_4);
_myView.transform = transform1;
}];
}];