第一次运行时关键帧动画延迟

时间:2012-08-07 10:59:35

标签: ios animation cakeyframeanimation

我正在使用关键帧动画来动画一系列图像。但是当我第一次运行它时,动画开始之前有一段延迟。之后它运行顺利。我试着加载所有图像。它减少了延迟但仍然可见。如何进一步减少延迟。

4 个答案:

答案 0 :(得分:2)

Apple因使用“懒惰”加载技术而臭名昭着,很可能放置从“[UIImage imageNamed:]”检索的图像实际上并不创建缓存的位图,只是创建它的收据。

如果所有其他方法都失败,请尝试使用强力方法:强制系统通过在您刚刚丢弃的上下文中渲染图像来渲染它。

CGSize bigSize; // MAX width and height of your images

UIGraphicsBeginImageContextWithOptions(bigSize, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

for(UIImage *image in arrayOfImages) {
    CGContextDrawImage(context, (CGRect){ {0,0}, image.size }, [image CGImage]);
}
UIGraphicsEndImageContext();

现在你不仅要引用图像,而且还要强制它们渲染,所以希望系统能够保留内部位图。应该是一个简单的测试。

答案 1 :(得分:1)

这段代码是swift 2.2!

尝试在动画结束前放置您想要的最终图像。

更像是:

// the animation key is for retrive the informations about the array images
func animation(cicleTime:Int, withDuration: Double, animationKey:String){

    var images = [UIImage]()


    // get all images for animation
    for i in 0...cicleTime {
        let fileName = String(format: "image%d",i)
        let image = UIImage(named: fileName)

        images.append(image!)
    }

    let animation = CAKeyframeAnimation(keyPath: "contents")

    animation.setValue(animationKey, forKey: "animationName")

    animation.duration = withDuration
    animation.repeatCount = 1 // times you want to repeat the animation
    animation.values = images.map{$0.CGImage as! AnyObject}
    animation.delegate = self
    images.removeAll(keepCapacity: false)
    YourViewHere.layer.addAnimation(animation, forKey: "contents")

    YourViewHere.image = UIImage(named: "nameOfYourLastImageOfAnimation")
}

嗯,这适合我。

答案 2 :(得分:0)

我最近遇到了这个问题并通过尽早“预先运行”动画来解决它,持续时间为0,而不是在完成时删除它。当我真正想要运行它时,整个序列被加载并且平滑

let animation: CAKeyframeAnimation = CAKeyframeAnimation(keyPath: "contents")
    animation.calculationMode = kCAAnimationDiscrete
    animation.duration = 0
    animation.values = spritesArray
    animation.repeatCount = 1
    animation.removedOnCompletion = false
    animation.fillMode = kCAFillModeForwards
    layer.addAnimation(animation, forKey: "animateIn")

在我的情况下,我实际上已将序列拆分为2个单独的关键帧动画,用于intro / outro,并且outro始终在开始之前停止。在0持续时间关键帧动画中首先预加载整个事物阻止了这一点。

答案 3 :(得分:-1)

考虑在NSArray中预加载图片。 您的延迟很可能是因为它首先必须加载图像。

所以,基本上,假设你有img1.pngimg2.png等等,直至img10.png

//do this before your keyframe animation.

NSMutableArray *frames = [NSMutableArray array];
for(int i = 1 ; i <= 10 ; i++)
    [frames addObject:[UIImage imageNamed:[NSString stringWithFormat:@"img%d.png" , i]]];

//now use this array for the animation

希望这会有所帮助。干杯!

相关问题