慕课IOS界面基础

  • MVC 主要优点:低耦合性 (便于修改开发)高重用性(多个view共用一个model)低开发成本 高可维护性

UIWindow

实例化

_window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen]bounds]];
_window,backgroundColor=[UIColor redColor];
[_window makeKeyVisible];
_window.windowLevel=UIWindowLevelNormal 设置window级别 可以显示多个window
  • 主要用途
  • window相当一个容器,里面有很多控件
  • window可以传递触摸消息到控件
  • 协同控件做响应

UIView(属性和方法子类会继承)

  • UI的基类 (不是所有UI开头的类都继承于UIView)
  • UIView属性
  • UIView方法
  • UIView自适应(屏幕适配 )
UIView *view=[[UIView alloc]init];
位置大小
view.frame=CGRectMake(x,y,w,h );
view.frame=CGRectMake(10,30,355,627);  
背景颜色
view.backgroundColor=[UIColor redColor];  默认和屏幕颜色一样
将视图加入到父视图中
[self.view addSubview:view];

左上角为屏幕原点 控件左上角距离屏幕上端距离为y距离屏幕左边为x
w h为控件宽高

  • 屏幕大小
    3GS 屏幕3.5寸 分辨率 320480 图片@1x
    4/4s 3.5寸 320
    480 @2x 图片640960
    5/5c/5s 4.0寸 320
    568@2x 6401136
    6 4.7寸 375
    667 @2x 7501344
    6plus 5.5寸 414
    736 @3x 1242*2208

常用属性

打印屏幕大小 只能调用bounds

NSLog(@"w:%f h%f",[[UIScreen mainScreen]bounds].size.width,[[UIScreen mainScreen]bounds].size.height);  取屏幕宽度
状态栏高度为20px 设置控件frame时需要减去20px

frame bounds区别

NSLog(@"frame-x:%f y%f w:%f h:%f",view.frame.origin.x,view.frame.origin.y,view.frame.size.weight,view.frame.size.height);
边框大小 x,y永远为0
NSLog(@"bounds-x:%f y%f w:%f h:%f",view.frame.origin.x,view.frame.origin.y,view.frame.size.weight,view.frame.size.height);

center 中心点

NSLog(@"center-x:%f y:%f",view.center.x,view.center.y);

父视图

获取父视图

UIView *superView=view.superView;
superView.backgroundColor=[UIColor greenColor];
坐标是根据父视图的位置来设置的 不会跨层
UIView*view2=[[UIView alloc]init];
view2.frame=CGRectMake(10,100,300,30);
view2.backgroundColor=[UIColor blackColor];
view2.tag=2; 唯一标识 区分视图
[view addSubview:view2];

UIView *view3=[[UIView alloc]init];
view3.frame=CGRectMake(20,50,100,100);
view3.backgroundColor=[UIColor purpleColor];
view3.tag=3;
[view addSubview:view3];

获取子视图 遍历(返回值是数组)

NSArray *subViewsArray=view.subViews;
for(UIView *view in subViewsArray)
{
		if(view.tag==2)
			view.backgroundColor=[UIColor whiteColor];
}

通过tag值得到对应子视图

UIView *subView=[view viewWithTag:3];
subView.backgroundColor=[UIColor orangeColor];

层级的处理
同一个父视图中先加入view会被盖在下面
子视图层级跟随父视图 如果父视图层级低于其他同级视图则父视图的子视图也会被盖住
但子视图和其他视图中子视图无关

UIView *view4=[[UIView alloc]init];
view4.frame=CGRectMake(10,100,300,300);
view4.backgroundColor=[UIColor yellowColor];
[self.view addSubView:view4]; 

交换两个层的视图
交换两个层的视图时必须填写正确层数

[view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

插入一个视图到指定层

UIView *view5=[[UIView alloc]init];
view5.frame=CGRectMake(7,80,200,200);
view5.backgroundColor=[UIColor blackColor];
view insertSubview:view5 atIndex:5];
[view insertSubview:view5 aboveSubview:view3];
[view1 insertSubview:viwe5 belowSubview:view2];

将一个放入最顶层或最底层
当层交换了之后对应的子视图的数组下标也会进行改变

[view bringSubviewToFront:view3]; 
[view sendSubviewToBack:view3]; 

自适应

UIView *backView=[[UIView alloc]init];
backView.frame=CGRectMake([UIScreen mainScreen].bounds.size.width/2-25,400,50,50);
backView.backgroundColor=[UIColor orangeColor];
准许子视图自适应
backView.autoresizesSubviews=YES;
backView.tag=1001;
 [self.view addSubView:backView];

UIView*topView=[[UIView alloc]init];
topView.frame=CGRectMake(10,10,30,30);
topView.backgroundColor=[UIColor greenColor];
设置子视图的自适应方式
topView.autoresizeingMask=UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | | UIViewAutoresizingFlexibleWidthMargin;
[backView addSubview:topView];   

UILabel

  • 文本标签
  • UIColor颜色类
    继承自NSObject 不是框架和视图
    实例化
    文本标签 默认透明色
UILabel *label=[[UILabel alloc]init];
label.frame=CGRectMake(10,100,100,30);
label.backgroundColor=[UIColor yellowColor];
 - 文本
label.text=@"xxx";
 - 文字布局模式
label.textAlignment=NSTextAlignmentCenter;
 - 文字颜色
cleanColor 透明色
label.textColor=[UIColor clearColor]; 
label.textColor=[UIColor colorWithRed: (CGFloat) green:  (CGFloat)blue:  (CGFloat)alpha: (CGFloat)]; alpha为透明度 CGFloat范围为0-1 颜色范围0-255 换算成比例 
label.textColor=[UIColor colorWithRed: 0.1 green: 0.8blue: 0.2 alpha: 1]; 
label.alpha=0.5;
[self.view addSubview:label];

配色软件sip 选择颜色后在代码部分直接command+v

  • 字体设置
label.front=[UIFont systemFontOfSize:25];
  • 字体加粗或者倾斜
    设置同一个属性 会覆盖掉之前设置的
label.font=[UIFont boldSystemFontOfSize:25]; 
label.font=[UIFont italicSystemFontOfSize:25];加粗被倾斜覆盖
for(NSString *name in [UIFont familyNames])  遍历字体
{
	NSLog(@"@",name);
}
label.font=[UIFont fontWithName:@"Bodoni 72" size:25];

 - 设置阴影
 label.shadowColor=[UIColor redColor];
label.shadowOffset=CGRectMake(5,5);  向右下偏移
label.shadowOffset=CGRectMake( -5,-5); 向左下偏移 
  • 换行
  1. label要有足够大小
  2. 设置换行模式 (只对英文有效 )
  3. 设置显示行数(0或-1表示不限制行数)
label.lineBreakMode=NSLineBreakByCharWrapping; 按字符换行
label.lineBreakMode=NSLineBreakByWo rdWrapping; 按单词换行
label.numberOfLines=0;
[self.view addSubview:label];
  • 根据字符串大小计算label大小
CGSize size=[label.text sizeWithFont:label.font constrainedToSize:CGRectMake(355,10000) lineBreakMode:NSLIneBreakByCharWrapping]
label.frame=CGRectMake(laebl.frame.origin.x ,label.frame.origin.y,label.frame.sie.width,label.frame.size.height);

UIImage&UIImageView

  • UIImage (不是继承自UIView 不能直接放到视图上)
  • UIImage载体
  • UIImageView

加载图片

图片名重复需要写路径

NSString *path=[[NSBundle mainBundle]resourcePath];工程目录路径
NSString *imagePath=[NSString stringWithFormat: @"%a/1.png",path];
 UIImage *image=[[UIImage alloc]initWithConetentOfFile:imagePath]; 加载效率低 可释放内存  加载精致图片
image=[[UIImage alloc]initWithData:(NSData*)
UIImage*image1=[UIImage imagedName:@"1.jpg"]; 会被加入内存中 效率高 占用内存 加载表情

图片不能直接显示 需要载体( UIImageView Button等)
图片显示在图片上的大小由载体控制

UIImageView*imageView=[[UIImageView alloc]initWithImage:image];
imageView.frame=CGRectMake(10,100,355,400);
imageView.frame=CGRectMake(10,100,image.size.width,image.size.height);原图大小
imageView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:imageView];

内容模式

默认效果 UIViewContentModeScaleToFill 拉伸充满整个载体
UIViewContentModeScaleAspectFill 拉伸不改变比例 充满最大的边
UIViewContentModeScaleAspectFit 拉伸不改变比例 充满小的一边

imageView.contentMode=UIViewContentModeCenter;

UIImageView动画 (播放序列图)

序列图连续播放为动画
多行注释#if 0 #endif

NSMutableArray *imageArray=[[NSMutableArray alloc]init];
for(int i=1;i<=13;i++)
{
	UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@“png%d.png”,i ]]; 图片名是变量
	[imageArray addObject:image];
}
	UIImageView*imageView=[[UIImageView alloc]init];
	imageView.frame=CGRectMake((375-407)/2,100,407,217);
	[self.view addSubview:imageView]; 

设置动画数组

imageView.animationImages=imageArray;

谁播放周期时间(秒)

imageView.animationDuration=2;

执行次数

imageView.animationRepeatCount=0;

开始/停止播放动画

[imageView startAnimating];
[imageView stopAnimating];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值