Collection View with Animations

This entry is part 30 of 32 in the series iOS Apps

Collections View Animations – Swift with Xcode.

ViewController.swift

import UIKit

class ViewController: UICollectionViewController {
    
    var imagesArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        imagesArray = ["light","light1","light2","light3"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        
        let imageView = cell.viewWithTag(1) as! UIImageView
        
        imageView.image = UIImage(named: imagesArray[indexPath.row])
        
        let backBtn = cell.viewWithTag(3) as! UIButton
        backBtn.hidden = true
        
        return cell
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imagesArray.count
        
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        cell?.superview?.bringSubviewToFront(cell!)
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: UIViewAnimationOptions.TransitionNone, animations: ({
            
            cell?.frame = collectionView.bounds
            collectionView.scrollEnabled = false
            let backBtn = cell!.viewWithTag(3) as! UIButton
            backBtn.hidden = false
            
        }), completion: nil)
    }
    
    
    @IBAction func bkBtnAction(sender: UIButton) {
        let indexPath = collectionView?.indexPathsForSelectedItems()
        collectionView?.scrollEnabled = true
        collectionView?.reloadItemsAtIndexPaths(indexPath!)
    }
}

 

Series Navigation<< How to read JSON using Swift 2.0
Tabbar and Navigation Controller >>