iOS学习day6
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS学习day6相关的知识,希望对你有一定的参考价值。
1、计时器的使用
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(flyAction) userInfo:nil repeats:YES];
2、随机数的使用
arc4random()
3、UIWindow部分代码(xib)
//UIScreen
CGRect rect = [UIScreen mainScreen].bounds;
//创建UIWindow
self.window = [[UIWindow alloc] initWithFrame:rect];
//设置背景颜色
self.window.backgroundColor = [UIColor blackColor];
//设置keyWindow,并使其可见
[self.window makeKeyAndVisible];
//添加主控制器
ViewController * vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
4、UIView部分代码
//将v置为最前端
[self.view bringSubviewToFront:v];
//将v置为最后端
[self.view sendSubviewToBack:v];
//从父视图删除子视图
[v removeFromSuperview];
5、计算Label的高度(核心代码)
CGRect rect = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
6、自适应(核心代码)
(1)sizeToFit
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 17, 0)];
label.text = string;
label.numberOfLines = 0;
//使用sizeToFit一定要设置宽度
[label sizeToFit];
(2)sizeThatFits:
UILabel * label = [[UILabel alloc] init];
label.text = string;
label.numberOfLines = 0;
label.backgroundColor = [UIColor greenColor];
CGSize size = [label sizeThatFits:CGSizeMake(self.view.frame.size.width - 40, MAXFLOAT)];
label.frame = CGRectMake(20, 50, size.width, size.height);
[self.view addSubview:label];
7、UIImage部分代码
(1)本目录下的位置
//Resource:文件名
//ofType:文件名后缀
//inDirectory:Bundle下的目录
NSString * path =[[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist" inDirectory:@"image"];
(2)简易拉伸
UIImage* image = [UIImage imageNamed:@"backgroundImage”];//有缓存
UIImageView* imageView =[[UIImageView alloc]initWithImage:image];
UIEdgeInsets inset = UIEdgeInsetsMake(6 , 6, 6, 6);
image = [image resizableImageWithCapInsets:inset resizingMode:UIImageResizingModeStretch];
imageView.image = image;
imageView.frame = CGRectMake(20, 20, self.view.frame.size.width-40, 30);
[self.view addSubview:imageView];
(3)圆切
imageView.layer.cornerRadius = 10
//隐藏圆外的东西
imageView.layer.masksToBounds = YES;、//隐藏
imageView.clipsToBounds = YES;//切掉
//设置边线宽度
imageView.layer.borderWidth = 4 ;
//设置边线颜色
imageView.layer.borderColor = [UIColor blackColor].CGColor;
//设置阴影颜色
imageView.layer.shadowColor = [UIColor greenColor].CGColor;
//设置阴影大小
imageView.layer.shadowOffset = CGSizeMake(2, 2);
//设置不透明度
imageView.layer.shadowOpacity = 0.3;
(4)动画
NSMutableArray * array = [NSMutableArray array];
for (int i = 1; i < 6; i++) {
NSString * string = [NSString stringWithFormat:@"hehua0%d",i];
UIImage * image = [UIImage imageNamed:string];
[array addObject:image];
}
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
//设置动画图片
imageView.animationImages = array;
//动画时间
imageView.animationDuration = 1;
//动画播放重复次数,值为0时,无限循环
imageView.animationRepeatCount = 0;
//开始动画
[imageView startAnimating];
[self.view addSubview:imageView];
8、UIButton部分代码
(1)Button实现图上字下(核心代码)
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.imageView.contentMode = UIViewContentModeBottom;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont systemFontOfSize:20];
}
return self;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
CGFloat ponitX = 0;
CGFloat ponitY = 0;
CGFloat width = contentRect.size.width;
CGFloat height = contentRect.size.height * kScale;
return CGRectMake(ponitX, ponitY, width, height);
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
CGFloat ponitX = 0;
CGFloat ponitY = contentRect.size.height * kScale;
CGFloat width = contentRect.size.width;
CGFloat height = contentRect.size.height * (1 - kScale);
return CGRectMake(ponitX, ponitY, width, height);
}
9、UITextField部分代码
(1)第一响应(键盘弹入弹出)
//进行第一响应
[self.view addSubview:textField];
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//取消第一响应
[self.textField resignFirstResponder];
//结束编辑
[self.textField endEditing:YES];
}
以上是关于iOS学习day6的主要内容,如果未能解决你的问题,请参考以下文章