ios - 如何通过更改背景图像和更改UIImageView的位置进行动画处理

标签 ios objective-c animation uiimageview

我对获得一种动画不仅感兴趣,该动画不仅可以从A点到B点进行动画处理,还可以更改背景图像。
假设初始位置为0,0,第一个为0,100;持续时间为1秒,我有4张图片作为背景,然后我希望背景每0/4秒和25 px更改一次。

到目前为止,我还有下一个为图像设置动画的代码,但是我仍然需要实现运动,而且我不知道该怎么做。

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

        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");
                 }];

关于ios - 如何通过更改背景图像和更改UIImageView的位置进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27771141/

相关文章:

iphone - 在 UITableView 中垂直居中 UITableViewCell

"content:"上的 css 动画在 Safari 和 Firefox 上不起作用

iphone - 寻找一种优雅的方法来检查字典中是否存在键

ios - 如何单击/迭代 coreData 实例

ios - 将图像添加到我的 UIButton subview

html - 滑动 CSS 动画没有在正确的区域翻转

ios - UIStackView 中 UILabels 上的动画隐藏属性会导致不同的动画

ios - UICollectionViewController Insets 无法正确调整和滚动

objective-c - 在 Objective-C 上 : which is the root directory in iphone

ios - 旋转 UIView 导致它改变位置