CGSize size = [UIScreen mainScreen].bounds.size;
//演示UIImageView的使用, 主要显示图像(png,jpeg)
//<1>UIImageView 基本使用
UIImageView *back = [[UIImageView alloc] initWithFrame:self.view.bounds];
//注意细节:
//不能显示gif, 使用第三方库显示
//png可以省略后缀名, jpg一定需要加后缀名
back.image = [UIImage imageNamed:@"back2.jpg"];
// imageNamed图片做缓存, 如果图片太大
//back.image = [UIImage alloc] initWithData:<#(NSData *)#>;
NSLog(@"w=%f h=%f",back.image.size.width,back.image.size.height);
[self.view addSubview:back];
//<2>UIImageView 如何实现动画
_bird = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 120, 96)];
_bird.image = [UIImage imageNamed:@"DOVE 1.png"];
[self.view addSubview:_bird];
//设置动画
//bird.animationDuration
//bird.animationImages
//bird.animationRepeatCount
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i=1; i<=18; i++) {
NSString *name = [NSString stringWithFormat:@"DOVE %d.png",i];
UIImage *image = [UIImage imageNamed:name];
[images addObject:image];
}
_bird.animationImages = images;
//控制动画速度
_bird.animationDuration = 18 * 1.0/30;
[_bird startAnimating];
//定时器, 很多操作每隔一段时间执行
//参数1: 每次执行的间隔
//参数2和3: 指定一个每次执行方法
//参数4: 传入用户数据
//参数5: 是否重复
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(dealLoop:) userInfo:nil repeats:YES];
}
-(void)dealLoop:(NSTimer *)t
{
double xSpeed = 10;
CGPoint center = _bird.center;
center.x += xSpeed;
if(center.x > self.view.frame.size.width + _bird.frame.size.width/2)
{
center.x = - _bird.frame.size.width / 2;
}
_bird.center = center;
//让敌机再次出现
}