如何暗示UICollectionViewCell Flip动画

时间:2015-06-04 11:51:53

标签: swift ios8

我试图暗示包含两个不同视图的UICollectionViewCell也会翻转每个视图。 但是我遇到了麻烦,当我翻转第一个collectionViewCell时,第十个collectionViewCell也被翻转了。请帮帮我

FlipCollectionViewController

class FlipCollectionViewController: UICollectionViewController {

private struct Storyboard {
    static let FlipCellReuseIdentifier = "FilpCell"
}

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    return 20
}

var selectedCellDefaultFrame:CGRect!
var selectedCellDefaultTransform:CGAffineTransform!


override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    NSLog("select")
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.FlipCellReuseIdentifier, forIndexPath: indexPath) as! FilpCollectionViewCell


}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.FlipCellReuseIdentifier, forIndexPath: indexPath) as! FilpCollectionViewCell
    return cell
}

}

FlipCollectionViewCell

class FilpCollectionViewCell: UICollectionViewCell {

var back: UIView!
var front: UIImageView!
var showingBack = true
var initNumber:Int = 0


required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    let singleTap = UITapGestureRecognizer(target: self, action: "tapped")
    singleTap.numberOfTouchesRequired = 1

    self.contentView.addGestureRecognizer(singleTap)
    self.contentView.userInteractionEnabled = true

}

func tapped() {
    front = UIImageView(image: UIImage(named: "QueenCard"))
    back = UIView(frame: self.frame)
    back.backgroundColor = UIColor.blackColor()
    self.contentView.addSubview(back)

    if showingBack {
        NSLog("showBack")
        UIView.transitionFromView(back, toView: front, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromLeft, completion: nil)
        showingBack = false
    } else {
        UIView.transitionFromView(front, toView: back, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil)
        showingBack = true
    }
}

}

1 个答案:

答案 0 :(得分:1)

细胞正在重复使用。向上滚动并且单元格不在视图中时,此单元格将在视图底部重复使用。在您的情况下,翻转的单元格正在被重用。您想要做的是在重复使用之前不要翻转单元格。

在重复使用单元格之前,iOS会调用方法prepareForReuse(),这是您取消单元格或重置任何自定义属性的位置。

在你的情况下,它看起来像这样(未经测试的代码):

override func prepareForReuse() {

    if showingBack {
        UIView.transitionFromView(back, toView: front, duration: 0, options: UIViewAnimationOptions.TransitionNone, completion: nil)
        showingBack = false
    }
}

这样,当重复使用该单元时,它将被取消翻转。如果您希望翻转单元格保持翻转状态,则应在模型中保持翻转状态。