Skip to content

Instantly share code, notes, and snippets.

@mstolin
Last active September 12, 2022 17:12
Show Gist options
  • Save mstolin/242ba34ecb53bf519ed2 to your computer and use it in GitHub Desktop.
Save mstolin/242ba34ecb53bf519ed2 to your computer and use it in GitHub Desktop.
UICollectionView cell touches animation.
//
// CollectionViewCell.swift
// CollectionTest
//
// Created by Marcel Stolin on 08.05.15.
// Copyright (c) 2015 Marcel Stolin. All rights reserved.
//
import UIKit
// MARK: CollectionViewCellDelegate protocol
@objc protocol CollectionViewCellDelegate {
optional func collectionViewCellTouchesBegan(cell: CollectionViewCell, event: UIEvent)
optional func collectionViewCellTouchesCancelled(cell: CollectionViewCell, event: UIEvent)
optional func collectionViewCellTouchesEnded(cell: CollectionViewCell, event: UIEvent)
}
// MARK: CollectionViewCell class
class CollectionViewCell: UICollectionViewCell {
// MARK: IBOutlets
@IBOutlet weak var wrapper: UIView!
// MARK: Internal properties
internal var delegate: CollectionViewCellDelegate?
// MARK: Super override
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// MARK: Touches
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// scale the view a little bit
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.wrapper.transform = CGAffineTransformMakeScale(0.9, 0.9)
})
// run the delegate method
if let cellDelegate = self.delegate {
cellDelegate.collectionViewCellTouchesBegan?(self, event: event)
}
}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
// back to the roots
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.wrapper.transform = CGAffineTransformMakeScale(1, 1)
})
// run the delegate method
if let cellDelegate = self.delegate {
cellDelegate.collectionViewCellTouchesCancelled?(self, event: event)
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
// back to the roots
UIView.animateWithDuration(0.4, animations: { () -> Void in
self.wrapper.transform = CGAffineTransformMakeScale(1, 1)
})
// run the delegate method
if let cellDelegate = self.delegate {
cellDelegate.collectionViewCellTouchesEnded?(self, event: event)
}
}
}
//
// CollectionViewController.swift
// CollectionTest
//
// Created by Marcel Stolin on 08.05.15.
// Copyright (c) 2015 Marcel Stolin. All rights reserved.
//
import UIKit
class CollectionViewController: UICollectionViewController, CollectionViewCellDelegate {
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 14
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
var size = CGSizeZero
// just a test size
size.width = self.view.frame.width / 2 - 15
size.height = size.width
return size
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("BasicCell", forIndexPath: indexPath) as! CollectionViewCell
cell.delegate = self
return cell
}
// MARK: CollectionViewCellDelegate
func collectionViewCellTouchesBegan(cell: CollectionViewCell, event: UIEvent) {
println("tochesBegan")
}
func collectionViewCellTouchesCancelled(cell: CollectionViewCell, event: UIEvent) {
println("tochesCancelled")
}
func collectionViewCellTouchesEnded(cell: CollectionViewCell, event: UIEvent) {
println("tochesEnded")
if let cView = self.collectionView {
// maybe perform a segue
println("\(cView.indexPathForCell(cell))")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment