在Swift中,如何翻转UITableViewCell?

时间:2016-07-01 17:03:14

标签: ios swift uitableview

我有一个tableView,当按下一个单元格时,我需要它翻转到另一个单元格。例如:

enter image description here

对此:enter image description here

一次一个单元格。我在这里看到了关于如何在objective-c中执行此操作的帖子: How can I flip an iOS UITableViewCell?

有没有办法在Swift中做到这一点?

1 个答案:

答案 0 :(得分:1)

enter image description here

enter image description here

enter image description here

以下是您需要披露指标的情况。据我所知,你必须使用自定义的一个来翻转它。我发现这个函数会水平翻转单元格的视图,但我不相信这就是你想要的。

cell.contentView.layer.setAffineTransform(CGAffineTransformMakeScale(-1, 1))

我只是使用自定义公开指示器生成两个单元格,您只需保存公开指示器图像的副本翻转,然后当您单击单元格时,将它们交换为:

var isFirstCell = true
var indexOfSwappingCell: Int = 0

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    isFirstCell = !isFirstCell
    tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: indexOfSwappingCell, inSection: 0)], withRowAnimation: .None)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == indexOfSwappingCell {
        if isFirstCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("customCell1", forIndexPath: indexPath) as! CustomCell1
            //setup cell
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("customCell2", forIndexPath: indexPath) as! CustomCell2
            //setup cell
            return cell
        }
    } else {
        //setup other cells

        return UITableViewCell()
    }
}

如果你想要那些人使用的代码,我只需将它运行到objectivec2swift

//flip the view to flipView

@IBAction func flipButtonAction(sender: UIButton) {
    UIView.transitionWithView(self.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: AnyObject) {
    UIView.transitionWithView(self.contentView, duration: 0.6, options: .TransitionFlipFromLeft, animations: {() -> Void in
        self.contentView.insertSubview(normalView, aboveSubview: flipView)
    }, completion: {(finished: Bool) -> Void in
    })
}

回复您的评论

viewDidLoad

上面添加这2个
var isFirstLoad = true
var contentViewToFlip: UIView!

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == indexOfSwappingCell {

        if isFirstCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("customCell1", forIndexPath: indexPath) as! CustomCell1
            if isFirstLoad == false {
                UIView.transitionWithView(self.contentView, duration: 0.6, options: .TransitionFlipFromRight, animations: {() -> Void in
                    contentViewToFlip.insertSubview(cell.contentView, aboveSubview: contentViewToFlip)
                    }, completion: {(finished: Bool) -> Void in
                })
            }
            isFirstLoad = false
            contentViewToFlip = cell.contentView
            //setup cell
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("customCell2", forIndexPath: indexPath) as! CustomCell2
            UIView.transitionWithView(self.contentView, duration: 0.6, options: .TransitionFlipFromLeft, animations: {() -> Void in
                contentViewToFlip.insertSubview(cell.contentView, aboveSubview: contentViewToFlip)
                }, completion: {(finished: Bool) -> Void in
            })
            contentViewToFlip = cell.contentView
            //setup cell
            return cell
        }
    } else {
        //setup other cells

        return UITableViewCell()
    }
}