CAAnimation

CAAnimation_第1张图片
//设置背景

//self.view.layer.contents=(id)[UIImage imageNamed:@"bg.png"].CGImage;

//创建图像显示图层

_layer=[[CALayeralloc]init];

_layer.bounds=CGRectMake(0,0,87,32);

_layer.position=CGPointMake(160,284);

[self.view.layeraddSublayer:_layer];

//由于鱼的图片在循环中会不断创建,而10张鱼的照片相对都很小

//与其在循环中不断创建UIImage不如直接将10张图片缓存起来

_imageArray=[NSMutableArrayarray];

for(inti=0; i<10; ++i) {

NSString*imageName=[NSStringstringWithFormat:@"fish%i.png",i];

UIImage*image=[UIImageimageNamed:imageName];

[_imageArrayaddObject:image];

}

//定义时钟对象

CADisplayLink*displayLink=[CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(step)];

[displayLinkaddToRunLoop:[NSRunLoopmainRunLoop]forMode:NSDefaultRunLoopMode];//添加时钟对象到主运行循环

}

#pragma mark -每次屏幕刷新就会执行一次此方法(每秒接近60次)

- (void)step {

//定义一个变量记录执行次数

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;

}

}

你可能感兴趣的:(CAAnimation)