nicklockwood / layout

A declarative UI framework for iOS
MIT License
2.23k stars 97 forks source link

Long Press Gesture? #167

Open sidhenn opened 5 years ago

sidhenn commented 5 years ago

I need to add a Long Press Gesture to an existing UICollectionView cell in Layout but having problems.

This is the extra code.

extension TimerCollectionViewController: UIGestureRecognizerDelegate
{
    @objc func onDidLongPressTimer(gestureRecognizer : UILongPressGestureRecognizer)
    {
        if gestureRecognizer.state != UIGestureRecognizer.State.began { return }

        let p = gestureRecognizer.location(in: timerCollectionView)
        if let indexPath = timerCollectionView?.indexPathForItem(at: p)
        {
            print("\(indexPath.row) Let's delete this timer.")
        } else {
            print("got nothing...")
        }
    }
}

I also added the following to viewDidLoad()

// Add Long Press Gesture to delete
        let lpgr : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(onDidLongPressTimer))
        lpgr.minimumPressDuration = 0.5
        lpgr.delegate = self
        lpgr.delaysTouchesBegan = true
        timerCollectionView?.addGestureRecognizer(lpgr)
sidhenn commented 5 years ago

Anyone have thoughts on this. Not working using 0.6.35.

Thanks.