iPhone Dev: Animating PNG Sequences

后端 未结 4 675
攒了一身酷
攒了一身酷 2021-01-03 04:44

What is the best or recommended technique for animating PNG Sequences.
Heres what I\'ve learned:

Do it Manually
Using M

4条回答
  •  悲&欢浪女
    2021-01-03 05:35

    Easiest way is to use Core Animation layers to do sprite animation:

    • Make a 'multi-cell' image strip (could be PNG or JPG) of all the various moves for the sprite. Make each 'cell' be a fixed height or width.

    • Put the image in a UIImageView.

    • Take the CALayer of the view, and adjust the contentsRect property of the layer. This acts as a clipping rectangle for the sprite. To animate sprite all you have to do is move the contentsRect over to the next cell position in the image strip.

    Something like this will do it (assuming you've already calculated the newSpritePosition.

      [CATransaction begin];
      [CATransaction setDisableActions:YES];
      spriteLayer.contentsRect = newSpritePosition;
      [CATransaction commit];
    

    (If you don't do the second line, then you'll get a default slide animation instead of a 'jump' to the next state.)

    With this technique you can rotate through all the frames of the sprite -- much like a flipbook. CALAyer transactions are optimized so you should get pretty fast frame rates. You can set an animationDidStop completion handler to move to the next state of the sprite or loop back to the beginning frame.

提交回复
热议问题