How to animate images using bitmaps / rgb data in iOS

后端 未结 2 1421
一个人的身影
一个人的身影 2021-01-17 05:21

I\'m programming a game on my iPhone, and I have an image I want to animate, say a walking person in a bitmap. The bitmap changes over time. How do I add the bitmaps to the

2条回答
  •  一生所求
    2021-01-17 06:20

    You can do it with a CAKeyframeAnimation.

    I just wrote this and didn't test it - but should get you on the right path. Load all your frames into an NSArray then set the 'values' of the animation to the frames array.

    NSArray *walkFrames = [[NSArray arrayWithObjects:
               [UIImage imageNamed:@"walk.00000.png"],
               [UIImage imageNamed:@"walk.00001.png"],
               [UIImage imageNamed:@"walk.00002.png"],
               [UIImage imageNamed:@"walk.00003.png"],
               [UIImage imageNamed:@"walk.00004.png"],
               [UIImage imageNamed:@"walk.00005.png"],
               [UIImage imageNamed:@"walk.00006.png"],
               [UIImage imageNamed:@"walk.00007.png"],
               [UIImage imageNamed:@"walk.00008.png"],
               [UIImage imageNamed:@"walk.00009.png"],
               nil] retain];
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    
    anim.keyPath = @"contents";
    anim.duration = 2.f;
    anim.values = walkFrames;
    anim.calculationMode = kCAAnimationLinear;
    anim.repeatCount = HUGE_VAL;
    
    [myView addAnimation:anim forKey:@"myFlipAnimation"];
    

提交回复
热议问题