CAAnimation

//設(shè)置背景

//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];

//由于魚的圖片在循環(huán)中會(huì)不斷創(chuàng)建,而10張魚的照片相對(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;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容