iphone - UIView 旋转不能发生两次

标签 iphone ios rotation core-graphics

在我的 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/

相关文章:

ios - 渲染CCSprite纹理给出旧图像

iphone - 我在我的应用程序中删除了一个视频,但它仍然存在于相机胶卷中,反之亦然

ios - 来自 UITableView 的 View 消失

ios - AFNetworking 2.0 和背景图片

android - 如何使用 c/c++ native 代码在 android 中旋转位图

ios - 如果我的应用程序要长期使用,我应该将我的 ObjC 应用程序转换为 Swift 吗?

iphone - UIWebView 模式 YouTube 播放器 "Done"按钮操作

ios - Dreamfactory - 上传文件的不同内容类型

ios - UIViewController 的模态呈现和旋转

opencv - 使用opencv旋转矩阵