IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> iOS开发之UI开发2(xib、storyboard、字典转模型、MVC) -> 正文阅读

[开发测试]iOS开发之UI开发2(xib、storyboard、字典转模型、MVC)

添加控件

获取控制器管理的view的宽度

CGFloat viewWidth = self.view.frame.size.width;

控件的最大x和y值(x值+宽度、y值+高度)

获取其他控件的最大y值,就是下面控件的y值
CGFloat btnY = CGRectGetMaxY(lblName.frame);

view的嵌套

创建View
UIView *appView = [[UIView alloc]init];
appView.backgroundColor = [UIColor blueColor];
appView.frame = CGRectMake(10,10,100,100);
[self.view addSubview:appView];

View里添加image
UIImageView *imgViewIcon = [[UIImageView alloc]init];
imgViewIcon.backgroundColor = [UIColor yellowColor];
imgViewIcon.frame = CGRectMake(10,10,10,10);
[appView addSubview:imgViewIcon];


View里添加label
UILabel *lblName = [[UILabel alloc]init];
lblName.backgroundColor = [UIColor redColor];
lblName.frame = CGRectMake(10,20,10,10);
[appView addSubview:lblName];
标题
lblName.text = appDitt[@"name"];
字体大小
lblName.font = [UIFont systemFontOfSize:12];
居中
lblName.textAlignment = NSTextAlignmentCenter;


View里添加UIButton
UIButton *btnDownload = [[UIButton alloc]init];
btnDownload.backgroundColor = [UIColor greenColor];
btnDownload.frame = CGRectMake(10,30,10,10);
[appView addSubview:btnDownload];

[btnDownload setTitle:@"下载" forState:UIControlStateNormal];
[btnDownload setTitle:@"已安装" forState:UIControlStateDisabled];

[btnDownload setBackgroundImage:[UIImage imageNamed:@"buttongreen"]forState:UIControlStateNormal];
[btnDownload setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"]forState:UIControlStateDisabled];
字体大小
btnDownload.titleLabel.font = [UIFont systemFontOfSize:14];
单击事件
[btnDownload addTarget:self action:@selector(btnDownloadClick) forControlEvents:UIControlEventTouchUpInside];

xib

xib是轻量级界面描述文件,storyboard是重量级界面描述文件
storyboard还可以描述多个界面,以及不同界面之间的跳转关系,两个最终都是创建代码

  • 创建 -> IOS -> User Interface -> Empty文件
  • 通过动态加载xib文件创建里面的view

1.1找到根目录
NSBundle *rootBundle = [NSBundle mainBundle];
1.2找到应用根目录下的xib(nib)文件,不需要后缀
UIView *appView = [[rootBundle loadNibNamed:@"CZAppView" owner:nil options:nil] lastObject];//返回数组存储控件,需要first或者last取出要的控件
1.3设置appView的frame属性
appView.frame = CGRectMake(appX,appY,appW,appH);
1.4appview添加到self.view中
[self.view addSubview:appView];

1.5设置appview的子控件数据(方法一:耦合性高)
UIImageView *imgViewIcon = (UIImageView *)[appView viewWithTag:1000];
imgViewIcon.image = [UIImage imageNamed:appModel.icon];

UILable *lblName = (UILable *)[appView viewWithTag:2000];
UILable.text = appModel.name;

1.5设置appview的子控件数据(方法二)
xib文件中 CustomClass 选择一个自己创建的一个继承自UIView的类
xib中控件可以拖拽到该类进行绑定

imgViewIcon.imgViewIcon.image = [UIImage imageNamed:appModel.icon];
UILable.lblName.text = appModel.name;

1.5设置appview的子控件数据(方法三)
在方法二的基础之上,将其image和text封装
再加一个model属性,重写set方法,将model数据解析
appView.model = appModel;
该部分也可以封装进appView里面
+(instancetype)appView{
NSBundle *rootBundle = [NSBundle mainBundle];
return [[rootBundle loadNibNamed:@"CZAppView" owner:nil options:nil] lastObject];//返回数组存储控件,需要first或者last取出要的控件
}

shift + option + command + 左/右 折叠/展开代码

字典转模型

@interface CZApp:NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)appWithDict:(NSDictionary *)dict;
@end

@implementation CZApp
-(instancetype)initWithDict:(NSDictionary *)dict
{
if(self=[super init])
{self.name=dict[@"name"];self.icon=dict[@"icon"];}
return self;
}

+(instancetype)appWithDict:(NSDictionary *)dict
{return [[self alloc] initWithDict:dict];}
@end

主函数
NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrayModels = [NSMutableArray array];
方法一
for(NSDictionary *dict in arrayDict){
   CZApp *model = [[CZApp alloc] init];
   model.name = dict[@"name"];
   model.icon = dict[@"icon"];
   [arrayModels addObject:model];
}

方法二
for(NSDictionary *dict in arrayDict){
   //CZApp *model = [[CZApp alloc] initWithDict:dict];这个也可以
   CZApp *model = [CZApp appWithDict:dict];

   [arrayModels addObject:model];
}

label浮现和消失

UILabel *lblMsg = [[UILabel alloc] init];
lblMsg.text = @"正在下载...";
lblMsg.backgroundColor = [UIColor blackColor];
lblMsg.frame = CGRectMake(10,10,10,10);
lblMsg.textColor = [UIColor redColor];

居中
lblMsg.textAlignment = NSTextAlignmentCenter;
lblMsg.font = [UIFont boldSystemFontOfSize:17];

透明度,后面通过动画减少透明度
lblMsg.alpha = 0;

设置四个角的半径
lblMsg.layer.cornerRadius = 10;
把多余的角去掉
lblMsg.layer.masksToBounds = YES;
动画
[UIView animateWithDuration:2.0 animations:^{
lblMsg.alpha = 0.6;
} completion:^(BOOL finsished){
     if(finished){//执行完毕
     //开始新的动画 延迟1秒后执行     
     [UIView animateWithDuration:1.5 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
lblMsg.alpha = 0;
     } completion:^(BOOL finsished){
     if(finished){[lblMsg removeFromSuperview];}
     }]
     }
}]




[self.superview addSubview:lblMsg];

MVC

目录可以建立四个文件夹group:models、views、controllers、others

  • controllers:viewControllers
  • models:模型类
  • view:mainStoryboard,xib,具体的view
  • others:AppDelegate程序代理
  • supporting files:images.xcassets

改变状态栏的文字颜色

重写方法
-(UIStatusBarStyle)preferredStatusBarStyle
{
   return UIStatusBarStyleLightContent//改为白色
}
隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
   return YES;
}

控件显示至最上层

[self.view bringSubviewToFront:self.btnIcon];

清除view所有控件

每个子控件分别调用removeFromSuperview方法,内部执行循环
[self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

延迟

[self performSelector:@selector(方法) withObject:nil afterDelay:0.5];

设置图标和启动图片

同一张图片要做很多张:1.iphone屏幕大小和分辨率不同 2.不同地方显示尺寸不同

在这里插入图片描述

  • 开发使用的是点,ios系统自动转换为对应的像素,自动找图片
  • @2x 视网膜屏幕,在原来点坐标的大小上乘于2,@3x同理

设置图标

  • 选中images.xcassets -> AppIcon拖进图标

启动图片

  • 选择项目 -> App Icon and Launch Images -> Launch Images Source -> Launchimage Launch Screen File删除内容
  • 选中images.xcassets -> Launchimage拖进图标
  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-06 10:07:54  更:2021-08-06 10:08:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/4 11:04:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码