UIView轮换不能发生两次

ani*_*hin 4 iphone core-graphics rotation ios

在我的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);}];
 }
Run Code Online (Sandbox Code Playgroud)

问题是这总是只发生一次 - 用户第一次点击单元格时,imgArrow正确旋转,但是当单击第二次单元格时它不会旋转回来.为什么?

感谢帮助!

Mic*_*lum 9

问题是视图变换属性旋转到视图原始变换指定的程度.因此,一旦您的按钮旋转180度,再次调用此按钮将不会执行任何操作,因为它将尝试从当前位置(180)旋转到180.

这就是说,你需要创建一个if语句来检查转换.如果是180,则旋转为"0",反之亦然.

实现这一目标的简单方法是使用a 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;
}
Run Code Online (Sandbox Code Playgroud)