一.可以直接通過(guò)UIImageView提供的方法animationDuration進(jìn)行一組圖片的動(dòng)畫,具體代碼如下:
dispatch_async(dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT), ^{
for (int i = 0; i < 87; i++) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"Bundle"];
//拼接路徑
filePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"backImage-%d.png", i]];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
[self.dataSour addObject:image];
}
dispatch_async(dispatch_get_main_queue(), ^{
_backView.animationImages = self.dataSour;
_backView.animationDuration = self.dataSour.count / 24;
_backView.animationRepeatCount = 1;
[_backView startAnimating];
[self performSelector:@selector(animationOver) withObject:nil afterDelay:(_backView.animationDuration + 1.1)];
});
});
- (void) animationOver
{
TBLog(@"調(diào)用了");
[_backView stopAnimating];
_backView.animationImages = nil;
}
1.大家可以看一下洋丐,在這廢話不多說(shuō)邻悬,直接說(shuō)一下我的思路埃难,加載圖片的時(shí)候直接使用的是imageWithContentsOfFile方法莱预,這里好處就不多說(shuō)军熏,相信大家都懂度硝;
2.在向數(shù)組里面加載圖片的時(shí)候,我是開一條線程上炎,然后再子線程中進(jìn)行此操作恃逻,原因很簡(jiǎn)單,就是因?yàn)榇瞬僮魇呛臅r(shí)操作藕施,如果放在主線程中進(jìn)行操作寇损,那么就會(huì)卡住線程,這里面我說(shuō)一下我的使用場(chǎng)景:我的這個(gè)動(dòng)畫是放到登錄頁(yè)面的裳食,如果我點(diǎn)擊退出登錄時(shí)需要重新創(chuàng)建登錄頁(yè)矛市,此時(shí)正在進(jìn)行添加圖片到數(shù)組的耗時(shí)操作卡住主線程,造成的現(xiàn)象就是诲祸,點(diǎn)擊“退出登錄”按鈕要等待1-2s中才能有反應(yīng)浊吏,這樣的體驗(yàn)很差
3.這個(gè)方法的最大優(yōu)點(diǎn)就是操作簡(jiǎn)單,缺點(diǎn)就是耗內(nèi)存救氯,同時(shí)不很很準(zhǔn)確的去監(jiān)控動(dòng)畫的結(jié)束時(shí)間卿捎,這樣一來(lái)就沒(méi)辦法很準(zhǔn)確的在動(dòng)畫執(zhí)行完畢后進(jìn)行相應(yīng)的操作,所以果斷放棄径密,我們可以使用方法二進(jìn)行替換。
二.使用CAKeyframeAnimation進(jìn)行該動(dòng)畫的實(shí)現(xiàn)
dispatch_async(dispatch_queue_create("queue", DISPATCH_QUEUE_CONCURRENT), ^{
for (int i = 0; i < 87; i++) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"bundle"];
//拼接路徑
filePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"loginbackground_%d.png", i]];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
CGImageRef cgimg = image.CGImage;
[weakObject.dataSour addObject:(__bridge UIImage *)cgimg];
}
dispatch_async(dispatch_get_main_queue(), ^{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
animation.duration = weakObject.dataSour.count / 24.0;
animation.delegate = weakObject;
animation.values = weakObject.dataSour;
[weakObject.backView.layer addAnimation:animation forKey:nil];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"bundle"];
//拼接路徑
filePath = [filePath stringByAppendingPathComponent:@"loginbackground_83.png"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
weakObject.backView.image = image;
});
});
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
TBLog(@"animationDidStop");
[self.dataSour removeAllObjects];
[UIView animateWithDuration:1.0 animations:^{
self.bottomView.alpha = 1.0;
}];
}
方法二的最大的優(yōu)勢(shì)就是可以準(zhǔn)確的監(jiān)控動(dòng)畫執(zhí)行結(jié)束躺孝,這種方法解決我現(xiàn)在最大的痛點(diǎn)享扔。但是它也不是完美的,在消耗內(nèi)存方面植袍,沒(méi)有什么改善惧眠,我找了很多資料,但是到目前為止沒(méi)有解決這個(gè)方法于个,希望各位大神有什么好的建議都可以進(jìn)行交流氛魁。。。秀存。