簡體   English   中英

如何通過更改背景圖像和chnage 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");
                 }];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM