如何翻转iOS UITableViewCell?

我有一个包含许多单元格的tableview。 每个单元格都有一个小信息“i”按钮,我希望用户能够通过从右侧“翻转”单元格来编辑该单元格中的信息。

我为两个可能的单元格状态中的每一个都有一个单独的表格单元格XIB,并且cellForRowAtIndexPath提供正确的XIB(具有不同的单元格标识符),具体取决于该单元格是应该位于常规视图还是详细/编辑视图中。 使用例如reloadRowsAtIndexPaths可以正常工作,以反映按下“i”按钮时的更改状态。

但是,当按下单元格的“i”按钮时,我无法弄清楚如何为翻盖设置动画。 翻转不是reloadRowsAtIndexPaths中“withRowAnimation”的选项之一。 使用像UIView animateWithDuration这样的标准动画方法不起作用:reloadRowsAtIndexPaths只会导致相关单元格立即重新加载,忽略动画指令。 我甚至试图使用drawViewHierarchyInRect来获取单元格的未来状态作为图像,但我无法让单元格快速地重绘自己以便被drawViewHierarchyInRect拾取(我不希望用户实际看到结果动画之前的动画)

有谁知道如何处理这个?

编辑

总而言之,我遇到的挑战是:除了reloadRowsAtIndexPaths(打破动画)之外,还有另一种方法可以将UITableView单元格内容与不同的UITableViewCell Xib交换,而不会给新Xib的IBOutlets带来麻烦吗? (将它作为Nib加载到子视图中似乎打破了IBOutlets。)到目前为止,答案还没有解决这个问题。

如果您使用两个子类UITableViewCell ,而不是使用单个子类UITableViewCell ,并执行翻转动画,例如,

让xib包含子类UITableViewCell在其contentView中添加两个视图

  1. 正常查看
  2. 翻转视图

在这个视图中添加你想要显示的内容,并确保单元格中的havsockets,例如我拍摄的内容

在此处输入图像描述

在上面的两个视图中,第一个View-FlipView及其内容标签和按钮以及第二个也与第一个视图相同,顶视图应该是normalView
将按钮操作连接到单元格,您只需在自定义单元格中执行如下操作

  //flip the view to flipView - (IBAction)flipButtonAction:(UIButton *)sender { [UIView transitionWithView:self.contentView duration:0.6 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{ [self.contentView insertSubview:_flipView aboveSubview:_normalView]; } completion:^(BOOL finished) { }]; } //flip the view back to normalView - (IBAction)flipBackButtonAction:(id)sender { [UIView transitionWithView:self.contentView duration:0.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{ [self.contentView insertSubview:_normalView aboveSubview:_flipView]; } completion:^(BOOL finished) { }]; } 

它看起来像下面的东西

在此处输入图像描述

任何问题只是评论… :)希望这有助于你.. 🙂

您可能不希望重新加载表的数据以翻转任何给定的单元格。 你的细胞应该能够“自己”翻转。 因此,您需要UITableViewCell并且在子类中,您将拥有类似于flip函数的东西,它可以对单元格的子视图进行一些动画处理。

现在这是一个翻转动画的技巧。 您不仅可以同时更改视图层次结构并进行翻转,还可以在动画期间的错误时间看到视图的“背面”。 连续使用两个动画:

 [UIView animateWithDuration:... { // Flip the content "half way" by rotating 90 degrees about the y axis. // At the end of this animation your view will be perpendicular to the // screen and *not visible*. contentView.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 1, 0); } completion: { // Here you can rearrange your views (remember the view isn't visible // right now! // // When the "back side" views are all ready, you can kick off the second // half of the flip animation. [UIView animateWithDuration:... { // Flip *back* to the identity (not all the way around to M_PI) // because if you flip all the way around your views will actually // be flipped. You don't want that. Users will instinctively see // the movement as *continuous* and interpret the flip back and forth // animation as one continuous rotation all the way around. contentView.layer.transform = CATransform3DIdentity; } } 

我不明白为什么你不能使用[UIView animateWithDuration …]。

你可以在每个单元格中有两个UIViews来扩展你的动画效果。 第一个是可见的,第二个是隐藏的

在动画的中间点,只需隐藏第一个UIView并取消隐藏第二个UIView并从那里继续动画。

斯威夫特4:

 @IBAction func flipButtonAction(_ sender: UIButton) { UIView.transition(with: contentView, duration: 0.6, options: .transitionFlipFromRight, animations: {() -> Void in self.contentView.insertSubview(flipView, aboveSubview: normalView) }, completion: {(_ finished: Bool) -> Void in }) } //flip the view back to normalView @IBAction func flipBackButtonAction(_ sender: Any) { UIView.transition(with: contentView, duration: 0.6, options: .transitionFlipFromLeft, animations: {() -> Void in self.contentView.insertSubview(normalView, aboveSubview: flipView) }, completion: {(_ finished: Bool) -> Void in }) }