How to affect animation of UICollectionViewCell animation

I am trying to use a UICollectionViewCell which contains two different views also flipped over them. But I have a problem, when I flip the first collection of the ViewCell collection, the tenth collection of the ViewCell also flips. Please help me

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
    }
}

}

      

+3


source to share


1 answer


The cells are reused. When you scroll up and a cell is out of view, that cell is reused at the bottom of the view. In your case, the flipped cell is reused. What you would like to do is not flip the cell before reusing it.

Before the cell is reused, iOS will call the prepareForReuse () method , here you can flip the cell or reset any custom property.

In your case, it might look something like this (untested code):



override func prepareForReuse() {

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

      

This way, when the cell is reused, it will not be flipped. If you want the flipped cells to stay flipped, you must keep the flipped state in your model.

+1


source







All Articles