//self.view.layer.contents=(id)[UIImage imageNamed:@"bg.png"].CGImage;
//創(chuàng)建圖像顯示圖層
_layer=[[CALayeralloc]init];
_layer.bounds=CGRectMake(0,0,87,32);
_layer.position=CGPointMake(160,284);
[self.view.layeraddSublayer:_layer];
//由于魚(yú)的圖片在循環(huán)中會(huì)不斷創(chuàng)建,而10張魚(yú)的照片相對(duì)都很小
//與其在循環(huán)中不斷創(chuàng)建UIImage不如直接將10張圖片緩存起來(lái)
_imageArray=[NSMutableArrayarray];
for(inti=0; i<10; ++i) {
NSString*imageName=[NSStringstringWithFormat:@"fish%i.png",i];
UIImage*image=[UIImageimageNamed:imageName];
[_imageArrayaddObject:image];
}
//定義時(shí)鐘對(duì)象
CADisplayLink*displayLink=[CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(step)];
[displayLinkaddToRunLoop:[NSRunLoopmainRunLoop]forMode:NSDefaultRunLoopMode];//添加時(shí)鐘對(duì)象到主運(yùn)行循環(huán)
}
#pragma mark -每次屏幕刷新就會(huì)執(zhí)行一次此方法(每秒接近60次)
- (void)step {
//定義一個(gè)變量記錄執(zhí)行次數(shù)
staticints=0;
NSLog(@"s=== %d", s);
NSLog(@"_index=== %d",_index);
if(++s%10==0) {
UIImage*image=_imageArray[_index];
_layer.contents=(id)image.CGImage;//更新圖片
_index=(_index+1)%IMAGE_COUNT;
}
}