5

在我的 UITableViewCell 中,我有 UIImageView,每次用户单击该行时我想旋转 180° (didSelectRowAtIndexPath:)。代码很简单:

- (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 正确旋转,但当第二次单击单元格时它不会旋转回来。为什么?

感谢帮助!

4

2 回答 2

10

问题是视图变换属性旋转到视图原始变换指定的程度。因此,一旦您的按钮旋转 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;
}
于 2012-08-27T14:16:24.113 回答
1

您只是在设置转换。要应用多个变换,您必须将imgArrow.transform变换矩阵乘以所需的新变换。您可以使用它CGAffineTransformConcat()来执行此操作。

CGAffineTransform currTransform = [imgArrow transform];
CGAffineTransform newTransform = CGAffineTransformConcat(currTransform, CGAffineTransformMakeRotation(M_PI));
[imgArrow setTransform:newTransform];
于 2012-08-27T14:15:46.527 回答