在我的 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 会正确旋转,但当第二次单击单元格时它不会旋转回来。为什么?
感谢您的帮助!
最佳答案
问题是 View 转换属性旋转到 View 原始转换指定的度数。因此,一旦您的按钮旋转 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;
}
关于iphone - UIView 旋转不能发生两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12143664/