Method for animating images (like a movie) on iPhone without using MPMoviePlayer

前端 未结 3 1411
攒了一身酷
攒了一身酷 2020-11-27 06:24

I need to be able to display an animation over a static image.

Given that MPMoviePlayer gives you no control over anything useful, the only way I can think to do t

3条回答
  •  没有蜡笔的小新
    2020-11-27 07:11

    You could use a Core Animation CALayer to host your animation, and swap a series of CALayers in and out of that main layer to perform your frame-by-frame animation. You can set the content of an image-frame-hosting CALayer to a CGImageRef using its contents property. A series of CALayers containing your images could be created and stored in an NSMutableArray as needed, then removed when done to minimize memory use.

    You can set the transition duration between frames by wrapping the replaceSublayer:with: method call in a CATransaction, like the following:

    [CATransaction begin];
    [CATransaction setValue:[NSNumber numberWithFloat:0.25f] // 1/4th of a second per frame
                     forKey:kCATransactionAnimationDuration];   
    [mainLayer replaceSublayer:[imageLayers objectAtIndex:oldImageIndex] with:[imageLayers objectAtIndex:newImageIndex]];
    [CATransaction commit];
    

    You might also be able to get away with swapping in and out the CGImageRef in the contents of your main layer, if your frame display time is short enough.

提交回复
热议问题