2012-09-25 2 views
3

Im 빨간색 사각형을보기의 맨 아래에서 무작위 위치로 이동하고 무작위 위치에서 다시 위로 이동합니다.IOS 애니메이션이 내 애니메이션 블록과 함께 작동하지 않습니다.

[redSquare setCenter:CGPointMake(25,25)]; 

가 (적색 사각형보기 × 50 50 집합이 왼쪽 상단 구석에 redsquare과 앱 시작) 앱 [슈퍼 viewLoad]에 다음 코드를 시작한다.

애니메이션의 첫 번째 부분은 잘 작동합니다. 빨간색 사각형은 페이지 아래쪽의 임의 위치로 움직입니다. 그러나 애니메이션없이 맨 위로 임의의 위치로 점프합니다. 나는이 간단한 애니메이션을 돕기 위해 내가 잘못하고있는 일이나 다른 해결책을 도울 수 있도록 도와주세요.

-(void)moving{ 

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{ 
    int randomx = arc4random() % 295; 
    [redSquare setCenter:CGPointMake(randomx,435)]; 


    } completion:^(BOOL finished) { 

    [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)]; 
    }]; 

} 

답변

3

당신은 예를 들어 완료 블록에 새로운 애니메이션을 시작합니다 :

completion:^(BOOL finished) { 
      [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn  animations:^{ 
      int randomx = arc4random() % 295; 
      [redSquare setCenter:CGPointMake(randomx,25)]; 


    } completion:^(BOOL finished) { 


    }]; 
    }]; 
+0

감사합니다 - 그것은 나에게 가장 논리적를 보인다 나는 sheraza에서 제안을 선호한다. 위의 코드에 타이머를 추가하고 빨간색 사각형을 이제 의도대로 움직입니다. – pete

1

이 제품은 당신이 설명 정확히 수행합니다

completion:^(BOOL finished) { 
    [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)]; 
} 

즉, 화면 상단에 객체를 반환합니다 이후에 애니메이션이 완료되었습니다. 이것은 애니메이션되지 않으며 애니메이션이 완료된 후에 발생하므로 순간적입니다.

애니메이션이 완료된 후에 새 애니메이션을 트리거해야합니다.

여러 중첩 된 애니메이션 블록 그러나 조금 지저분한 될 수 있습니다 만들려면, 가장 깨끗한 방법은 다음과 같이 다시 애니메이션을위한 다른 방법을 작성하고 최초의 애니메이션이 완료되면 호출하는 것입니다

-(void) moveDown{ 
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{ 
     int randomx = arc4random() % 295; 
     [redSquare setCenter:CGPointMake(randomx,435)]; 

    } completion:^(BOOL finished) { 
     // First animation completed 
     [self moveBack]; 
    }]; 
} 

-(void) moveBack{ 
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{ 
     [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)]; 
    } completion:^(BOOL finished) { 
     // Second animation completed 
    }]; 
} 
+0

블록 사용의 요점은 콜백의 사용을 피하는 것입니다. – Abizern

+0

콜백을 피하는 주된 이유는 무엇입니까? 제 생각에는 블록 표기법과 이전 "beginanimations"표기법의 장점은 선택기가 컴파일러에 의해 유효성이 검사되지 않기 때문에 선택기를 사용하지 않아도된다는 것입니다. –

+0

그것은 어떤 일이 일어나고있는 지의 논리를 그것을 시작하는 코드와 분리하기 때문입니다. 따라서 응집력이 적은 척과 버그가 발생하기 쉽습니다. 또한 메소드가 어떻게 든 일반적이고 재사용 가능하고 종종 이러한 경우에는 존재하지 않는다는 암묵적인 계약이 있습니다. – EricLeaf

4

귀하 완료 블록은 단순히 새 위치를 설정합니다. 위로 가기를 애니메이션되지 않습니다 추가하는 대신,

이 시도

-(void)moving { 

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{ 
     int randomx = arc4random() % 295; 
     [redSquare setCenter:CGPointMake(randomx,435)]; 
    } completion:^(BOOL finished) { 
     [UIViewAnimateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{ 
     [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)]; 
     } completion: NULL 
    }]; 

} 

편집

그것은 하위 레벨이 혼란 스러울 수 있다는 사실입니다,하지만 당신은 생활 간단하게 할 수 있으며 메소드와 별도로 블록을 정의 할 경우 들여 쓰기가 적습니다.

예를 들어, 당신은 위의를 다시 작성 수 : 모든 제안

-(void)moving { 

    workBlk_t animationBlock =^{ 
     int randomx = arc4random() % 295; 
     [redSquare setCenter:CGPointMake(randomx,435)]; 
    }; 

    void (^completionBlock)(BOOL finished) = ^{ 
     [UIViewAnimateWithDuration:1.0 delay:0.0 options:UIViewAnimationCurveEaseIn animations:^{ 
      [redSquare setCenter:CGPointMake(redSquare.frame.origin.x,25)]; 
     } completion : NULL 
    }; 

    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationCurveEaseIn 
        animations:animationBlock 
        completion:completionBlock 
    }]; 

} 
+0

그러나 두 번째 애니메이션이 나중에 완성 된 후에 또 다른 애니메이션을 만들어야하는 경우 이처럼 중첩 된 애니메이션은 매우 지저분해질 수 있습니다. –

+0

편집 후 = 개선됨 –

관련 문제