IOS开发学习笔记Day4-IOC基础一
控件的常用属性和方法
NSLog(@"view的父控件:%@----控制器的view:%@", self.greenView.superview, self.view);
NSLog(@"view的子控件%@", self.greenView.subviews);
NSLog(@"控制器的view的子控件%@", self.view.subviews);
NSLog(@"控制器的view的父控件-----%@", self.view.superview);//在viewDidAppear方法中才可以拿到
根据tag拿到对应的view
UIView *redView = [self.view viewWithTag:1];
加到控制器的view中
[self.view addSubview:sw];
移除kongjian
[self.view removeFromSuperview];
基本创建方式
// 创建UILabel对象
UILabel *label = [[UILabel alloc] init];
// 设置frame (位置和尺寸)
label.frame = CGRectMake(100, 100, 100, 60);
// 设置背景颜色
label.backgroundColor = [UIColor yellowColor];
// 改变尺寸 iOS9以后, 中心点不变,向四周延伸
self.label.bounds = CGRectMake(0, 0, 200, 120);
改变控件的frame
// 方式1
// self.label.frame = CGRectMake(200, 100, 100, 60);
// 方式2
self.label.frame = (CGRect){{100, 100}, {100, 100}};
// 方式3
// 结构体是值传递,不是地址传递
// self.label.frame.size.width += 100;
CGRect frame = self.label.frame;
// frame.origin.x -= 100; // 改变x值
// frame.origin.y += 100; // 改变y值
// frame.size.width += 50; // 改变宽度
frame.size.height += 100; // 改变高度
self.label.frame = frame;
让控件居中
// self.label.center = CGPointMake(100, 100);
// 显示在最中间
self.label.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
监听器中tag的使用
不需要设置属性方式即可监听。
switch (button.tag) {
case 3:
NSLog(@"点击了按钮1");
break;
case 4:
NSLog(@"点击了按钮2");
break;
case 5:
NSLog(@"点击了按钮3");
break;
default:
break;
}
UILabel
// 1.1 创建UILabel对象
UILabel *label = [[UILabel alloc] init];
// 1.2 设置frame
label.frame = CGRectMake(100, 100, 202, 175);
// 1.3 设置背景颜色
label.backgroundColor = [UIColor redColor];
// 1.4 设置文字
label.text = @"da shen 11期最牛逼!!!!da shen da shen da shen da shen da shen ";
// 1.5 居中
label.textAlignment = NSTextAlignmentCenter;
// 1.6 设置字体大小
label.font = [UIFont systemFontOfSize:20.f];
label.font = [UIFont boldSystemFontOfSize:25.f];
label.font = [UIFont italicSystemFontOfSize:20.f];
// 1.7 设置文字的颜色
label.textColor = [UIColor whiteColor];
// 1.8 设置阴影(默认是有值)
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(-2, 1);
// 1.9 设置行数(0:自动换行)
label.numberOfLines = 1;
// 1.10 显示模式
label.lineBreakMode = NSLineBreakByTruncatingHead;
/*
NSLineBreakByWordWrapping = 0, // 单词包裹,换行的时候会以一个单词换行
NSLineBreakByCharWrapping, // 字符包裹换行,换行的时候会以一个字符换行
NSLineBreakByClipping, // 裁剪超出的内容
NSLineBreakByTruncatingHead, // 一行中头部省略(注意:numberOfLines要为1): "...wxyz"
NSLineBreakByTruncatingTail, // 一行中尾部省略: "abcd..."
NSLineBreakByTruncatingMiddle // 一行中中间部省略: "ab...yz"
*/
UIImageView
// 1.1 创建UIImageView对象
UIImageView *imageView = [[UIImageView alloc] init];
// 1.2 设置frame
imageView.frame = CGRectMake(100, 100, 250, 200);
// 1.3 设置背景
// imageView.backgroundColor = [UIColor greenColor];
// 1.4 设置图片 (png不需要后缀)
imageView.image = [UIImage imageNamed:@"1"];
/**
UIViewContentModeRedraw, // 重新绘制 (核心绘图) drawRact
//带有Scale,标明图片有可能被拉伸或压缩
UIViewContentModeScaleToFill, // 完全的压缩或拉伸
// Aspect 比例,缩放是带有比例的
UIViewContentModeScaleAspectFit, // 宽高比不变 Fit 适应
UIViewContentModeScaleAspectFill, // 宽高比不变 Fill 填充
//不带有Scale,标明图片不可能被拉伸或压缩
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
*/
// 1.5 设置图片的内容模式
imageView.contentMode = UIViewContentModeScaleAspectFill;
// 2.0 加到控制器的view中
[self.view addSubview:imageView];
// 裁剪多余的部分
imageView.clipsToBounds = YES;
UIImageView毛玻璃练习
// 1.创建UIImageView对象
UIImageView *imageView = [[UIImageView alloc] init];
// 2. 设置尺寸
// imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
imageView.frame = self.view.bounds;
// 3. 设置背景颜色
imageView.backgroundColor = [UIColor redColor];
// 4. 设置背景图片
imageView.image = [UIImage imageNamed:@"1"];
// 5.设置图片的内容模式
imageView.contentMode = UIViewContentModeScaleAspectFill;
// 6.加毛玻璃
// 6.1 创建UIToolBar对象
UIToolbar *toolBar = [[UIToolbar alloc] init];
// 6.2 设置toolBar的frame
toolBar.frame = imageView.bounds;
// 6.3 设置毛玻璃的样式
toolBar.barStyle = UIBarStyleBlack;
toolBar.alpha = 0.98;
// 6.4 加到imageView中
[imageView addSubview:toolBar];
// 加到控制器的view中
[self.view addSubview:imageView];
UIImageView的frame设置
// 设置frame的方式
// 方式一
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"1"];
// imageView.frame = CGRectMake(100, 100, 267, 400);
// imageView.frame = (CGRect){{100, 100},{267, 400}};
*/
// 方式二
/*
UIImageView *imageView = [[UIImageView alloc] init];
// 创建一个UIImage对象
UIImage *image = [UIImage imageNamed:@"1"];
// 设置frame
imageView.frame = CGRectMake(100, 10, image.size.width, image.size.height);
// 设置图片
imageView.image = image;
*/
// 方式三
/*
// 创建一个UIImage对象
UIImage *image = [UIImage imageNamed:@"1"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 10, image.size.width, image.size.height)];
imageView.image = image;
// 方式四
// 创建一个UIimageview对象
// 注意: initWithImage 默认就有尺寸--->图片的尺寸
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
// 改变位置
// imageView.center = CGPointMake(200, 150);
imageView.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height * 0.5);
[self.view addSubview:imageView];
UIImageView练习-帧动画
#pragma mark - 开始动画
- (IBAction)startAnimation {
// 1.1 加载所有的图片
NSMutableArray<UIImage *> *imageArr = [NSMutableArray array];
for (int i=0; i<20; i++) {
// 获取图片的名称
NSString *imageName = [NSString stringWithFormat:@"%d", i+1];
// 创建UIImage对象
UIImage *image = [UIImage imageNamed:imageName];
// 加入数组
[imageArr addObject:image];
}
// 设置动画图片
self.imageView.animationImages = imageArr;
// 设置动画的播放次数
self.imageView.animationRepeatCount = 0;
// 设置播放时长
// 1秒30帧, 一张图片的时间 = 1/30 = 0.03333 20 * 0.0333
self.imageView.animationDuration = 1.0;
// 开始动画
[self.imageView startAnimating];
}
#pragma mark - 结束动画
- (IBAction)overAnimation {
[self.imageView stopAnimating];
}
第二种方式
// 1.加载所有的图片
NSMutableArray<UIImage *> *standImages = [NSMutableArray array];
for (int i=0; i<10; i++) {
// 获取所有图片的名称
NSString *imageName = [NSString stringWithFormat:@"stand_%d", i+1];
// 创建UIImage
UIImage *image = [UIImage imageNamed:imageName];
// 装入数组
[standImages addObject:image];
}
*/
// 2.设置动画图片
self.imageView.animationImages = self.standImages;
/**
* 加载所有的图片
*
* @param imagePrefix 名称前缀
* @param count 图片的总个数
*/
- (NSArray *)loadAllImagesWithimagePrefix:(NSString *)imagePrefix count:(int)count{
NSMutableArray<UIImage *> *images = [NSMutableArray array];
for (int i=0; i<count; i++) {
// 获取所有图片的名称
NSString *imageName = [NSString stringWithFormat:@"%@_%d",imagePrefix, i+1];
// 创建UIImage
UIImage *image = [UIImage imageNamed:imageName];
// 装入数组
[images addObject:image];
}
return images;
}
// 3.设置播放次数
self.imageView.animationRepeatCount = 0;
// 4.设置播放的时长
self.imageView.animationDuration = 0.6;
// 5.播放
[self.imageView startAnimating];
第三种方式
self.standImages = [self loadAllImagesWithimagePrefix:@"stand" count:10];
/**
* 加载所有的图片
* @param imagePrefix 名称前缀
* @param count 图片的总个数
*/
- (NSArray *)loadAllImagesWithimagePrefix:(NSString *)imagePrefix count:(int)count{
NSMutableArray<UIImage *> *images = [NSMutableArray array];
for (int i=0; i<count; i++) {
// 获取所有图片的名称
NSString *imageName = [NSString stringWithFormat:@"%@_%d",imagePrefix, i+1];
// 创建UIImage
UIImage *image = [UIImage imageNamed:imageName];
// 装入数组
[images addObject:image];
}
return images;
}
UIImageView加载方式
加载图片的方式:
- imageNamed
- 就算指向它的指针被销毁,该资源也不会被从内存中干掉
- 放到Assets.xcassets的图片,默认就有缓存
- 图片经常被使用
- imageWithContentsOfFile:
- 指向它的指针被销毁,该资源会被从内存中干掉
- 放到项目中的图片就不由缓存
- 不经常用,大批量的图片
-
加载Assets.xcassets这里面的图片:
1> 打包后变成Assets.car
2> 拿不到路径
3> 只能通过imageNamed:来加载图片
4> 不能通过imageWithContentsOfFile:来加载图片 -
放到项目中的图片:
1> 可以拿到路径
2> 能通过imageNamed:来加载图片
3> 也能通过imageWithContentsOfFile:来加载图片
// 方式一
// self.imageView.image = [UIImage imageNamed:@"1"];
// 方式二
// 路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
self.imageView.image = [UIImage imageWithContentsOfFile:path];
播放音频实战
- (void)viewDidLoad {
[super viewDidLoad];
// 1.加毛玻璃
UIToolbar *toolbar = [[UIToolbar alloc] init];
// 2. 设置frame
toolbar.frame = self.bgImageView.bounds;
// 3. 设置样式和透明度
toolbar.barStyle = UIBarStyleBlack;
toolbar.alpha = 0.98;
// 4.加到背景图片上
[self.bgImageView addSubview:toolbar];
// 5.创建播放器
/*
NSString *path = [[NSBundle mainBundle] pathForResource:@"mySong1.mp3" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
*/
// 资源的URL地址
NSURL *url = [[NSBundle mainBundle] URLForResource:@"mySong1.mp3" withExtension:nil];
// 创建播放器曲目
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
// 创建播放器
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
//self.player.rate = 2.0;//调节速率
}
播放逻辑控制
- (IBAction)playOrPause:(UIButton *)button {
switch (button.tag) {
case 3:
[self.player play]; // 播放
break;
case 4:
[self.player pause]; // 暂停
break;
default:
break;
}
}
切歌
- (IBAction)changeMusic:(UIButton *)button {
// 歌曲的名称
NSString *musicName = nil;
switch (button.tag) {
case 1:// 上一首
musicName = @"mySong2.mp3";
break;
case 2:// 下一首
musicName = @"mySong3.mp3";
break;
default:
break;
}
NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
[self.player replaceCurrentItemWithPlayerItem:playerItem];
// 播放
[self.player play];
}
延迟执行某方法;
[self performSelector:@selector(stand) withObject:nil afterDelay:self.imageView.animationDuration];
UIAlertView
// 创建对象
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"输入有误" message:info delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
//显示
[alertView show];