如何通过更改背景图像和更改 UIImageView 的位置来设置动画

How to animate by changing the background image and chnage the position of an UIImageView

我有兴趣获得一个动画,它不仅可以从 A 点动画到 B 点,还可以更改背景图像。 假设初始位置是 0,0,最后一个是 0,100;持续时间是 1 秒,背景有 4 张图像,然后我希望背景每 0/4 秒和 25 像素更改一次。

直到现在我有了下一个动画图像的代码,但我仍然需要实现运动,但我不知道该怎么做。

在此先感谢,这是我到目前为止的代码:

        UIImageView * animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50,50)];

       testArray  = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"test_1.png"], [UIImage imageNamed:@"test_2.png"], [UIImage imageNamed:@"test_3.png"],nil];
       animTime =3.0;
       [animatedImageView setAnimationImages:testArray] ;
       animatedImageView.animationDuration =  animTime;
       animatedImageView.animationRepeatCount = 1;
       [self.view addSubview: animatedImageView];
       [animatedImageView startAnimating];

更新:

我已经在我的代码中实现了 Mobile Project Lab 的答案

// I have a pathArray that is in C style and is bidimensional, that stores the path in reverse
// the maps is a 2d tile map
// the dimensions of the tiles are 64x64 px
//thes size of the character is 128x128
          for (int possitionInThePathArray = sizeOfPathArray - 1; possitionInThePathArray >= 0; possitionInThePathArray--) {

    xWalkingDirection = pathArray[possitionInThePathArray-1][1] - pathArray[possitionInThePathArray][1];  
    yWalkingDirection = pathArray[possitionInThePathArray-1][0] - pathArray[possitionInThePathArray][0];




              if (xWalkingDirection== 0 && yWalkingDirection == -1){
    //walking animation to North for 1 tile

        NSArray *testArray;
        float animTime;

        testArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"scientist_s0.png"],
                     [UIImage imageNamed:@"scientist_s1.png"],
                     [UIImage imageNamed:@"scientist_s2.png"],
                     [UIImage imageNamed:@"scientist_s3.png"],
                     [UIImage imageNamed:@"scientist_s4.png"],
                     [UIImage imageNamed:@"scientist_s5.png"],
                     [UIImage imageNamed:@"scientist_s6.png"],
                     [UIImage imageNamed:@"scientist_s7.png"],
                     [UIImage imageNamed:@"scientist_s8.png"],
                     [UIImage imageNamed:@"scientist_s9.png"], nil]; // 4th image added
        animTime = 10.0; // time is changed to 10.0
        [myCharacterFrame setAnimationImages:testArray];
        myCharacterFrame.animationDuration = animTime;
        myCharacterFrame.animationRepeatCount = 1;
        [myCharacterFrame startAnimating];

        [UIView animateWithDuration:animTime
                         animations:^{
                             myCharacterFrame.frame = CGRectOffset(myCharacterFrame.frame, 0.0f, -64.0f); // move 100 on x axis, 0 on y axis
                         }
                         completion:^(BOOL finished){
                             NSLog(@"animation done");
                         }];

        yMyCharacterFrame = yMyCharacterFrame-64.0;
        myCharacterFrame.frame = CGRectMake(xMyCharacterFrame, yMyCharacterFrame, 128, 128);
        myCharacterFrame.image = [UIImage imageNamed:@"scientist_s0.png"];


    }else if (xWalkingDirection== 1 && yWalkingDirection == 0){
     //walking animation to Est for 1 tile
    .....
    //and so on for all the four directions of walking

我现在面临的问题是动画没有正确触发,所以在代码继续之前就发生了一个动画

NSArray *testArray;
float animTime;

UIImageView *animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
testArray = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"test_001.png"], [UIImage imageNamed:@"test_002.png"],[UIImage imageNamed:@"test_001.png"], [UIImage imageNamed:@"test_002.png"], nil]; // 4th image added
animTime = 1.0; // time is changed to 1.0
[animatedImageView setAnimationImages:testArray];
animatedImageView.animationDuration = animTime;
animatedImageView.animationRepeatCount = 1;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];


[UIView animateWithDuration:animTime
                 animations:^{
                     animatedImageView.frame = CGRectOffset(animatedImageView.frame, 100.0f, 0.0f); // move 100 on x axis, 0 on y axis
                 }
                 completion:^(BOOL finished){
                     NSLog(@"animation done");
                 }];