I've added long press programmatically but it does nothing. What am I doing wrong? Thanks in advance.
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate {
@IBOutlet weak var monthLabel: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
var selectedDate = Date()
var totalSquares = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setCellView()
setMonthView()
func setupLongGestureRecognizerOnCollection() {
let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gestureRecognizer:)))
longPressedGesture.minimumPressDuration = 0.5
longPressedGesture.delegate = self
longPressedGesture.delaysTouchesBegan = true
collectionView?.addGestureRecognizer(longPressedGesture)
}
}
func setCellView() {
let width = (collectionView.frame.size.width - 2) / 8
let height = (collectionView.frame.size.height - 2) / 8
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flowLayout.itemSize = CGSize(width: width, height: height)
}
func setMonthView(){
totalSquares.removeAll()
let daysInMonth = CalendarHelper().daysInMonth(date: selectedDate)
let firstDayOfMonth = CalendarHelper().firstOfMonth(date: selectedDate)
let startingSpaces = CalendarHelper().weekDay(date: firstDayOfMonth)
var count: Int = 1
while(count <= 42) {
if(count <= startingSpaces || count - startingSpaces > daysInMonth)
{
totalSquares.append("")
}
else
{
totalSquares.append(String(count - startingSpaces))
}
count += 1
}
monthLabel.text = CalendarHelper().monthString(date: selectedDate) + " " + CalendarHelper().yearString(date: selectedDate)
collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return totalSquares.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "calCell", for: indexPath) as! CalendarCell
cell.dayOfMonth.text = totalSquares [indexPath.item]
return cell
}
@objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
if (gestureRecognizer.state != .began) {
return
}
let p = gestureRecognizer.location(in: collectionView)
if let indexPath = collectionView?.indexPathForItem(at: p) {
print("Long press at item: \(indexPath.row)")
}
}
@IBAction func previousMonthButton(_ sender: Any) {
selectedDate = CalendarHelper().minusMonth(date: selectedDate)
setMonthView()
}
@IBAction func nextMonthButton(_ sender: Any) {
selectedDate = CalendarHelper().plusMonth(date: selectedDate)
setMonthView()
}
override open var shouldAutorotate: Bool {
return false
}
}
The GitHub source is here: github.com/codeWithCal/CalendarExampleTutorial
Where do you call setupLongGestureRecognizerOnCollection ?
I do not see it in the code you posted.
Should it be :
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setCellView()
setMonthView()
func setupLongGestureRecognizerOnCollection() {
let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gestureRecognizer:)))
longPressedGesture.minimumPressDuration = 0.5
longPressedGesture.delegate = self
longPressedGesture.delaysTouchesBegan = true
collectionView?.addGestureRecognizer(longPressedGesture)
}
setupLongGestureRecognizerOnCollection() // <<-- ADDED
}