- 很多时候我们都把UIImageView当成显示图片的容器,其实通过它,还可以轻松的创建帧动画效果,我们先一步步的通过代码来认识UIImageView控件.
- (void)viewDidLoad {
[super viewDidLoad];
// 创建UIImageView
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
// 设置显示的图片
imageview.image = [UIImage imageNamed:@"hyf"];
// 添加到view上
[self.view addSubview:imageview];
}
- 其实UIImageView还有下面两种初始化方法,但是可能是相同的!
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hyf"]];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hyf"] highlightedImage:[UIImage imageNamed:@"hyf"]];
- 这边我们就接着说UIImageView的帧动画,其实就是引入一组连续的图片,一帧一帧的播放,我们就以拳王来做演示,直接上代码
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) UIImageView *imagView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建开始按钮
UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
startBtn.frame = CGRectMake(0, 0, 50, 50);
[startBtn setTitle:@"开始" forState:UIControlStateNormal];
[self.view addSubview:startBtn];
// 创建imagView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 300, 220)];
imageView.image = [UIImage imageNamed:@"dazhao_1"];
_imagView = imageView;
[self.view addSubview:imageView];
// 给按钮创建触发方法
[startBtn addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchDown];
}
// 播放动画的方法
- (void)play
{
// 1.创建图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 1; i <= 87; i ++) {
// 获取图片名
NSString *imageName = [NSString stringWithFormat:@"dazhao_%d",i];
UIImage *image = [UIImage imageNamed:imageName];
[images addObject:image];
}
// 设置动画图片
self.imagView.animationImages = images;
// 设置动画的次数
self.imagView.animationRepeatCount = 2;
// 设置动画的时间
self.imagView.animationDuration = 87 * 0.05;
// 开启动画
[self.imagView startAnimating];
}
-
效果如下:
上面就是UIImageView控件的一些简单运用了,有什么问题或者不明白的地方,大家可以评论,留言!谢谢...