如何在UICollectionViewCell中翻转图像? - 斯威夫特4

时间:2018-04-22 18:05:09

标签: ios swift uicollectionview uicollectionviewcell swift4

我正在使用 Swift 4 尝试翻转redraw()内的卡片(UIImageView)。 我有两个图像,当我运行应用程序时,我可以看到其中一个。但翻转之后,我得到的是蓝色图像而不是第二个图像。

以下是我UICollectionViewCell课程的代码。

UICollectionViewCell
import UIKit

class CardViewCell: UICollectionViewCell {

    var cardFlipped = false
    var frontImage = UIImage(named: "image1")
    var backImage = UIImage(named: "image2")

    @IBOutlet var weak cardImage: UIImageView!
    @IBOutlet weak var flipCardButton: UIButton!

    @IBAction func flipCardAction(_ sender: Any) {
        flipCardAnimation()
    }

    func flipCardAnimation() {
        cardFlipped ? showFront() : showBack()
        contentView.reloadInputViews()
    }

    func showFront() {
        var image = cardImage.image
        image = frontImage
        flipCardButton.setImage(image, for: .normal)

        let transitionOptions = UIViewAnimationOptions.transitionFlipFromLeft
        UIView.transition(with: flipCardButton,
                          duration: 0.3,
                          options: transitionOptions,
                          animations: nil,
                          completion: nil)

        cardFlipped = true
    }

    func showBack() {
        var image = cardImage.image
        image = backImage
        flipCardButton.setImage(image, for: .normal)

        let transitionOptions = UIViewAnimationOptions.transitionFlipFromLeft
        UIView.transition(with: flipCardButton,
                          duration: 0.3,
                          options: transitionOptions,
                          animations: nil,
                          completion: nil)

        cardFlipped = false
    }
}

0 个答案:

没有答案