2012-08-27 4 views
4

내 UITableViewCell있는 사용자가 행 (didSelectRowAtIndexPath :) 때마다 180 ° 회전 할 원하는 UIImageView가 있습니다.UIView 회전 두 번 발생할 수 없습니다

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *curCell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    UIImageView *imgArrow = (UIImageView*)[curCell viewWithTag:3]; 
    [UIView animateWithDuration:0.3 animations:^{imgArrow.transform = CGAffineTransformMakeRotation(M_PI);}]; 
} 

문제는이 항상 한 번만 발생한다는 것입니다 - 사용자가 셀을 클릭 처음으로, imgArrow가 제대로 회전을하지만 늘 셀이 두 번째로 클릭하면 다시 회전 : 코드는 매우 간단하다. 왜?

도움 주셔서 감사합니다.

답변

9

문제는 views 변환 속성이 ​​원래 변환보기에서 지정된 각도로 회전한다는 것입니다. 따라서 버튼이 180도 회전하면 다시 180을 180에서 180으로 회전하려고하기 때문에 아무 것도하지 않습니다.

이렇게 말하면 변환을 확인하기 위해 if 문을 작성해야합니다. . 180 인 경우 "0"으로 설정하고 반대의 경우도 마찬가지입니다.

이것을 달성하는 쉬운 방법은 BOOL을 사용하는 것입니다.

if (shouldRotate){ 
    [UIView animateWithDuration:0.3 animations:^{imgArrow.transform = CGAffineTransformMakeRotation(M_PI);}]; 
    shouldRotate = NO; 
}else{ 
    [UIView animateWithDuration:0.3 animations:^{imgArrow.transform = CGAffineTransformMakeRotation(0);}]; 
    shouldRotate = YES; 
} 
1

변환을 설정하는 중입니다. 여러 변환을 적용하려면 imgArrow.transform 변환 행렬에 원하는 새 변환을 곱해야합니다. 이것을 수행하려면 CGAffineTransformConcat()을 사용할 수 있습니다.

CGAffineTransform currTransform = [imgArrow transform]; 
CGAffineTransform newTransform = CGAffineTransformConcat(currTransform, CGAffineTransformMakeRotation(M_PI)); 
[imgArrow setTransform:newTransform];