contentMode属性
-
带有scale单词的:图片有可能会拉伸
- UIViewContentModeScaleToFill
- 将图片拉伸至填充整个imageView
- 图片显示的尺寸跟imageView的尺寸是一样的
- 带有aspect单词的:保持图片原来的宽高比
- UIViewContentModeScaleAspectFit
- 保证刚好能看到图片的全部
- UIViewContentModeScaleAspectFill
- 拉伸至图片的宽度或者高度跟imageView一样
- UIViewContentModeScaleAspectFit
- UIViewContentModeScaleToFill
-
没有scale单词的:图片绝对不会被拉伸,保持图片的原尺寸
- UIViewContentModeCenter
- UIViewContentModeTop
- UIViewContentModeBottom
- UIViewContentModeLeft
- UIViewContentModeRight
- UIViewContentModeTopLeft
- UIViewContentModeTopRight
- UIViewContentModeBottomLeft
- UIViewContentModeBottomRight
小语法点
- 不能直接修改:OC对象的结构体属性的成员
- 下面的写法是错误的
imageView.frame.size = imageView.image.size;
- 正确写法
CGRect tempFrame = imageView.frame;
tempFrame.size = imageView.image.size;
imageView.frame = tempFrame;
-设置imageView的尺寸与父容器的尺寸一致
imageView.frame = self.view.bounds
-利用UIToolBar设置毛玻璃效果
- 创建UIToolBar对象
UIToolbar *toolBar = [[UIToolbar alloc]init]; - 设置toolBar的frame
toolBar.frame = self.view.bounds; - 设置毛玻璃的样式
toolBar.barStyle = UIBarStyleBlack;
toolBar.alpha = 0.98; - 加到imageView中
[imageView addSubview:toolBar];
initWithImage:方法
- 利用这个方法创建出来的imageView的尺寸和传入的图片尺寸一样
修改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}};
-
抽取重复代码
- 将相同代码放到一个新的方法中
- 不用的东西就变成方法的参数
-
图片的加载方式
- 有缓存
UIImage *image = [UIImage imageNamed:@"图片名"];
- 使用场合:图片比较小、使用频率较高 - 建议把需要缓存的图片直接放到Images.xcassets
- 无缓存
NSString *file = [[NSBundle mainBundle] pathForResource:@"图片名" ofType:@"图片的扩展名"]; UIImage *image = [UIImage imageWithContentsOfFile:@"图片文件的全路径"];
- 使用场合:图片比较大、使用频率较小 - 不需要缓存的图片不能放在Images.xcassets
- 放在Images.xcassets里面的图片,只能通过图片名去加载图片
-
延迟做一些事情
[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];
例子
在开始写代码的时候,一定要先分析清楚整个实现思路,把整个过程拆解成一步一步
1.动画核心代码
//1.遍历每一张图片
//1.1加载所有图片
NSMutableArray<UIImage *>*imageArr = [NSMutableArray array];
for(int i=0;i<20;i++){
//1.2获取图片名称
NSString *imageName = [NSString stringWithFormat:@"%d",i++];
//1.3创建UIImage对象
UIImage *image = [UIImage imageNamed:imageName];
//1.4把image对象加入数组
[imageArr addObeject:image];
//2.设置动画图片
self.imageView.animationImages = imageArr;
//3.设置动画图片的播放次数
self.imageView.animationRepeatCont = 0;
//4.设置播放时长
self.imageView.animationDuration = 1.0;
//5.开始动画
[self.imageView startAnimation];
}
2.拳皇动画
#import "ViewController.h"
@interface ViewController()
@propety(weak,nonatomic)IBOutlet UIImageView *imageView;
@propety(strong,nonatomic)NSArray *standImages;
@propety(strong,nonatomic)NSArray *smallImages;
@propety(strong,nonatomic)NSArray *bigImages;
@implementation ViewController
//把三类图片加载方式封装到loadAllImagesWithimagePrefix中,然后调用这个方法来加载这三类动画
-(void)viewDidLoad{
self.standImage = [self loadAllImagesWithimagePrefix:@"stand" count:10];
self.smallImage = [self loadAllImagesWithimagePrefix:@"stand" count:10];
self.bigImage = [self loadAllImagesWithimagePrefix:@"stand" count:10];
}
/*
加载所有图片
imagePrefix 名称前缀
count 图片的总个数
*/
-(NSArray *)loadAllImagesWithPrefix:(NSString *)imagePrefix count:(int)count{
NSMutableArray<UIImage *>images = [NSMutableArray array];
for(int i=0;i<count;i++){
NSString *imageName = [NNString stringWithFormat:@"@_%d",imagePrefix:i+1];
}
return images;
}
- (IBAction)stand {
self.imageView.animationImages = self.standImages;
self.imageView.animationRepeatCount = 0;
self.imageView.animationDuration=0.6;
[self.imageView startAnimating];
}
- (IBAction)smallZhao {
self.imageView.animationImages = self.smallImages;
self.imageView.animationRepeatCount = 2;
self.imageView.animationDuration=1.5;
[self.imageView startAnimating];
}
- (IBAction)bigZhao {
self.imageView.animationImages = self.bigImages;
self.imageView.animationRepeatCount = 1;
self.imageView.animationDuration=2.5;
[self.imageView startAnimating];
}
@end
网友评论