Skip to content

Instantly share code, notes, and snippets.

@wujianguo
Last active November 3, 2015 09:36
Show Gist options
  • Save wujianguo/e9dc1c377698c356182e to your computer and use it in GitHub Desktop.
Save wujianguo/e9dc1c377698c356182e to your computer and use it in GitHub Desktop.
can delete collection view cell
import UIKit
protocol CollectionViewCellDeleteDelegate: class {
func collectionViewCellDeleteClick(cell: UICollectionViewCell)
func collectionViewDidSelectCell(cell: UICollectionViewCell)
}
class CanDeleteCollectionViewCell: UICollectionViewCell, UIScrollViewDelegate {
static let notifyDeleteCellScrollName = "notifyScrollName"
weak var delegate: CollectionViewCellDeleteDelegate?
override func awakeFromNib() {
super.awakeFromNib()
setupDelete()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "notifyresetScroll:", name: CanDeleteCollectionViewCell.notifyDeleteCellScrollName, object: nil)
let singleTap = UITapGestureRecognizer(target: self, action: "didSelectCell:")
scrollView.addGestureRecognizer(singleTap)
}
func didSelectCell(gesture: UITapGestureRecognizer) {
NSNotificationCenter.defaultCenter().postNotificationName(CanDeleteCollectionViewCell.notifyDeleteCellScrollName, object: self)
if gesture.state == .Ended {
delegate?.collectionViewDidSelectCell(self)
}
}
func notifyresetScroll(notification: NSNotification) {
if notification.object as? CanDeleteCollectionViewCell == self {
return
}
scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
}
override func layoutSubviews() {
super.layoutSubviews()
var buttonWidth = frame.width / 3
if buttonWidth > CGFloat(80) {
buttonWidth = CGFloat(80)
}
mainView.frame = CGRectMake(0, 0, contentView.frame.width, contentView.frame.height)
scrollView.frame = CGRectMake(0, 0, contentView.frame.width, contentView.frame.height)
deleteButton.frame = CGRectMake(contentView.frame.width, 0, buttonWidth, contentView.frame.height)
scrollView.contentSize = CGSizeMake(contentView.frame.width + deleteButton.frame.width, contentView.frame.height)
}
var scrollView = UIScrollView()
var deleteButton = UIButton()
var mainView = UIView()
func setupDelete() {
scrollView.pagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.bounces = false
scrollView.delegate = self
contentView.addSubview(scrollView)
mainView.frame = CGRectMake(0, 0, frame.width, frame.height)
scrollView.addSubview(mainView)
deleteButton.setTitle("Delete", forState: .Normal)
deleteButton.backgroundColor = UIColor.redColor()
deleteButton.addTarget(self, action: "deleteButtonClick:", forControlEvents: .TouchUpInside)
scrollView.addSubview(deleteButton)
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
NSNotificationCenter.defaultCenter().postNotificationName(CanDeleteCollectionViewCell.notifyDeleteCellScrollName, object: self)
}
func deleteButtonClick(sender: UIButton) {
delegate?.collectionViewCellDeleteClick(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment