使用 UIViewAnimation 旋转 UITableViewCell 的 UIImageView (我的应用程序挂起!!)

发布于 2024-11-09 17:07:06 字数 1172 浏览 4 评论 0原文

我将这个 UITableViewCell 放在表格中,并将左侧 imageView 设置为图像。现在我想要的就是当用户选择单元格(别名行)时连续旋转该图像。我可以将单元格 imageView 设置为旋转动画,但一旦执行此操作,应用程序就会停止响应用户输入。换句话说,应用程序挂起并且图像按其应有的方式旋转。下面是相关的代码片段。

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//...
    [NSThread detachNewThreadSelector:@selector(rotateImage) toTarget:self withObject:nil];

//...
}

- (void) rotateImage {
    UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];

    [UIView beginAnimations:@"looping animation" context:nil];
    // other animation options here if you'd like, and the duration can be anything, not just 3.

    [UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionRepeat animations: ^{
        // do your rotation stuff on your image, in this block, for the cell you will be returning.
        selectedCell.imageView.transform = CGAffineTransformMakeRotation(kDegreesToRadians(90));
    } completion:nil];

    [UIView commitAnimations];  
}

如果我注释 NSThread 代码行,应用程序不会挂起,所以基本上我在动画代码中做错了什么,只是导致应用程序进入挂起状态。

I have this UITableViewCell in a table with left imageView set to an image. Now all I want is continuously rotate this image when user selects the cell (alias row). I'm able to animate the cell imageView to rotate but application stops responding to user inputs once I does so. In other words application hangs and the image rotating as it should be. Below is the relevant code snippet.

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//...
    [NSThread detachNewThreadSelector:@selector(rotateImage) toTarget:self withObject:nil];

//...
}

- (void) rotateImage {
    UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];

    [UIView beginAnimations:@"looping animation" context:nil];
    // other animation options here if you'd like, and the duration can be anything, not just 3.

    [UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionRepeat animations: ^{
        // do your rotation stuff on your image, in this block, for the cell you will be returning.
        selectedCell.imageView.transform = CGAffineTransformMakeRotation(kDegreesToRadians(90));
    } completion:nil];

    [UIView commitAnimations];  
}

If I comment the NSThread code line, application doesn't hang so basically I did something wrong in the animation code only that made the app go in hang state.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2024-11-16 17:07:06

块动画的(烦人的)默认设置是禁用用户交互。试试这个:

[UIView animateWithDuration:10 delay:0
                    options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                    selectedCell.imageView.transform = CGAffineTransformMakeRotation(M_PI);
                }
                 completion:NULL];

The (annoying) default with block animations is to disable user interaction. Try this:

[UIView animateWithDuration:10 delay:0
                    options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                    selectedCell.imageView.transform = CGAffineTransformMakeRotation(M_PI);
                }
                 completion:NULL];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文