您的位置:首页 > 移动开发 > IOS开发

史上最简单的iOS教程(二)

2018-03-05 18:40 183 查看

本节目录

UILabel

UIimage

UIimage contentMode属性

UIimage小语法点

UIimage initWithImage:方法

UIImageView的frame设置

UIimage 修改frame的3种方式

抽取重复代码

UIImageView的帧动画

延迟做一些事情

音频文件的简单播放

监听按钮点击

UIControl

UIAlertView

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 = @"opooc opooc opooc ";

// 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"
*/


UIimage

// 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(这个地方用的是bound,如果是用frame也可以,不过应该对应的是self.view,导致每次调整imageView的时候 ,还需要修改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];


UIimage contentMode属性

带有scale单词的:图片有可能会拉伸

UIViewContentModeScaleToFill

将图片拉伸至填充整个imageView

图片显示的尺寸跟imageView的尺寸是一样的

带有aspect单词的:保持图片原来的宽高比

UIViewContentModeScaleAspectFit

保证刚好能看到图片的全部

UIViewContentModeScaleAspectFill

拉伸至图片的宽度或者高度跟imageView一样

没有scale单词的:图片绝对不会被拉伸,保持图片的原尺寸

UIViewContentModeCenter

UIViewContentModeTop

UIViewContentModeBottom

UIViewContentModeLeft

UIViewContentModeRight

UIViewContentModeTopLeft

UIViewContentModeTopRight

UIViewContentModeBottomLeft

UIViewContentModeBottomRight

UIimage小语法点

不能直接修改:OC对象的结构体属性的成员

下面的写法是错误的

imageView.frame.size = imageView.image.size;


正确写法

CGRect tempFrame = imageView.frame;
tempFrame.size = imageView.image.size;
4000

imageView.frame = tempFrame;


UIimage initWithImage:方法

利用这个方法创建出来的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];


UIimage 修改frame的3种方式

直接使用CGRectMake函数

imageView.frame = CGRectMake(100, 100, 200, 200);


利用临时结构体变量

CGRect tempFrame = imageView.frame;
tempFrame.origin.x = 100;
tempFrame.origin.y = 100;
tempFrame.size.width = 200;
tempFrame.size.height = 200;
imageView.frame = tempFrame;


使用大括号{}形式

imageView.frame = (CGRect){{100, 100}, {200, 200}};


抽取重复代码

- 将相同代码放到一个新的方法中
- 不用的东西就变成方法的参数


图片的加载方式

有缓存

objc

UIImage *image = [UIImage imageNamed:@"图片名"];


使用场合:图片比较小、使用频率较高

建议把需要缓存的图片直接放到Images.xcassets

无缓存

objc

NSString *file = [[NSBundle mainBundle] pathForResource:@"图片名" ofType:@"图片的扩展名"];

UIImage *image = [UIImage imageWithContentsOfFile:@"图片文件的全路径"];


使用场合:图片比较大、使用频率较小

不需要缓存的图片不能放在Images.xcassets

放在Images.xcassets里面的图片,只能通过图片名去加载图片

UIImageView的帧动画

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];


延迟做一些事情

[abc performSelector:@selector(stand:) withObject:@"123" afterDelay:10];
// 10s后自动调用abc的stand:方法,并且传递@"123"参数


音频文件的简单播放

// 创建一个音频文件的URL(URL就是文件路径对象)
NSURL *url = [[NSBundle mainBundle] URLForResource:@"音频文件名" withExtension:@"音频文件的扩展名"];
// 创建播放器
self.player = [AVPlayer playerWithURL:url];
// 播放
[self.player play];


或者

// 创建一个音频文件的URL(URL就是文件路径对象)
NSString* str = [[NSBundle mainBundle]pathForResource:@"音频文件名" ofType:@"音频文件的扩展名"];

NSURL *url = [NSURL fileURLWithPath:str];
// 创建播放器
self.player = [AVPlayer playerWithURL:url];
// 播放
[self.player play];


监听按钮点击

[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];


UIControl

凡是继承自UIControl的控件,都可以通过addTarget:…方法来监听事件

UIAlertView

// 创建对象
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"输入有误" message:info delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];

// 显示
[alertView show];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息