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
)