简体   繁体   中英

How do I only enable UIPanGestureRecognizer only after UILongPressGesture?

I'm new to swift and wasn't sure if this was possible but I'm running into an issue where I am in a UICollectionView, which is scroll enabled. By default, I only want the scroll enabled. However, if the user holds long enough I want the scroll to be disabled and the UIPanGestureRecognizer enabled. I'm having trouble enabling UIPanGesture after UILongPress. After the pangesture is done, the scroll should be enabled again and pan disabled.

First, the compiler is complaining about your variable has no default value.

You can workaround this by adding an initialize method and set gesture there.

Or you can set the variable as an Optional variable which has default value nil . But after that, you have to unwrap to get real value.

Or if you real know what this is:

class YourViewController: xxxx, yyyy {
    lazy var panGesture: UIPanGestureRecognizer! = {
        let pan = UIPanGestureRecognizer(…
        pan.delegate = self
        return pan
    }
…
}

Second, you could use UIGestureRecognizerDelegate to help.


At first, your pan gesture is disabled. After your long press, you disable the collection view isScrollEnabled and enable your pan gesture. (And after pan gesture finished, you disable pan gesture and reenable collection view isScrollEnabled )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM