2013-07-16 4 views
1

(블록 애니메이션

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)]; 
label.layer.cornerRadius = 5.0f; 
label.text = @"hello world"; 
label.textAlignment = NSTextAlignmentCenter; 
[self.view addSubview:label]; 
[label release]; 
[self startAnimation]; 

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btn.frame = CGRectMake(0, 0, 60, 30); 
[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:btn]; 
} 

- (void)startAnimation 
{ 
CGAffineTransform transForm = CGAffineTransformMakeRotation(angel * M_PI/180.0f); 
[UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^(void){ 
    label.transform = transForm; 
} completion:^(BOOL finished) { 
    NSLog(@"1"); 
    angel = angel + 5; 
    [self startAnimation]; 
}]; 
} 

- (void)btnPressed:(id)sender 
{ 
    //method 1 :[label.layer removeAllAnimations]; not work... 
//method 2 : CGAffineTransform transForm = CGAffineTransformMakeRotation(M_PI/180.0f); 
//label.transform = transForm;  not work... 
} 

) 내가 레이블을 회전

가, 지금은 그것을 취소 할 회전, 나는 사이트의 가능성이 질문을 검색하고, 약 2 솔루션을 발견 , 시도했지만 두 솔루션이 작동하지 않습니다.

+0

btnPressed에 또한 다시 같은 각도로 회전 할 수 있습니다. 예 : +180을 돌린 다음 -180으로 다시 돌리십시오. : P – HDdeveloper

답변

1

[label.layer removeAllAnimations]을 사용하면 finished 변수에 관계없이 [self startAnimation]을 호출하기 때문에 애니메이션이 멈추지 않습니다. 이로 인해 취소 된 경우에도 애니메이션이 계속됩니다.

다음에 애니메이션 완료 블록을 변경해야합니다 :

- (void)startAnimation 
{ 
    CGAffineTransform transForm = CGAffineTransformMakeRotation(angel * M_PI/180.0f); 
    [UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionCurveLinear  animations:^(void){ 
    label.transform = transForm; 
    } completion:^(BOOL finished) { 
    if (finished) { 
     NSLog(@"1"); 
     angel = angel + 5; 
     [self startAnimation]; 
    } 
    }]; 
} 

사용 [label.layer removeAllAnimations]

+0

예, 작동했습니다. 고맙습니다! 버튼을 누르면 "완료된"bool 값이 출력됩니다. 0을 누르면됩니다. 완료 "입니다. 문서는 다음과 같이 말합니다."애니메이션 시퀀스가 ​​끝날 때 실행될 블록 객체입니다.이 블록에는 반환 값이없고 완료 핸들러가 호출되기 전에 애니메이션이 실제로 완료되었는지 여부를 나타내는 단일 부울 인수가 사용됩니다. 애니메이션의 지속 시간이 0이면이 블록은 다음 실행 루프 사이클의 시작에서 수행되며이 매개 변수는 NULL 일 수 있습니다. " removeAllAnimations 작업으로 애니메이션이 끝난 것 같아요. – frank

관련 문제