2015-01-04 2 views
1

A 지점에서 B 지점으로 움직일뿐만 아니라 배경 이미지도 변경하는 애니메이션을 얻는 데 관심이 있습니다. 초기 위치는 0,0이고 finis는 0,100이라고 가정합니다. 지속 시간은 1 초이고 배경에 4 개의 이미지가 있습니다. 그런 다음 배경이 0/4 초와 25 픽셀마다 변경되기를 원합니다.배경 이미지를 변경하고 UIImageView의 위치를 ​​변경하여 애니메이션을 적용하는 방법

지금까지는 이미지를 움직이는 다음 코드가 있지만 여전히 동작을 구현해야하며이를 수행하는 방법을 모르겠습니다. 여기에 미리와

덕분에 내가 가지고있는 코드는 지금까지 :

 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]; 

UPDATE :

내 코드에 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 

지금 당면한 문제는 애니메이션이 올바르게 트리거되지 않아서 한 애니메이션이 t 전에 발생한다는 것입니다. 난 그냥 테스트 지금은 잘 작동하는 것 같다

답변

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

에 그 코드 이동은 이해 부분을 제공 : D 당신이 도움을 주셔서 감사합니다! –

+0

그리고 완료 줄을 추가하면 다른 코드 줄이 실행되기 전에 애니메이션이 완료됩니다. 미리 인내심을 가져 주셔서 감사합니다 –

+0

다른 코드가 수행하는 것을 달성하기 위해 노력하고 있는지 잘 모르겠습니다. 완료 블록 (NSLog (@ "animation done");)에 다른 메소드를 호출 할 수 있습니다. – gvuksic

관련 문제