I seem to be having some trouble with the following setup:
- I have a subclass of a
UILongPressGestureRecognizer
(import UIKit.UIGestureRecognizerSubclass
) in order to overridetouchesMoved
. - I have a button that contains an object of this GestureRecognizer subclass, which calls a selector.
- The method called by this selector contains a callback to be called by the original GestureRecognizer subclass every time
touchesMoved
is called.
So when I longPress on the button, and then move my finger around, the callback is called multiple times and I can view the properties of the touches, like the number, for example. (touches.count
)
I am trying to transition view controllers of a TabBarController based on touches information, but when I implement this, (either through selectedIndex =
or UIView.transitionFromView()
) the callback is only called at the beginning of the longPressEvent. (i.e. only once, and not multiple times.) I am not sure why this happens. Since the button is in the TabBarController, it should not be affected by transitioning views.
Here is some of the relevant code:
The GestureRecognizer subclass:
import UIKit.UIGestureRecognizerSubclass
class LongPressGestureRecongnizerSubclass: UILongPressGestureRecognizer{
var detectTouchesMoved = false
var toCallWhenTouchesMoved: ((touches: Set<UITouch>) -> ())?
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
if detectTouchesMoved{
toCallWhenTouchesMoved?(touches: touches)
}
}
}
The delegate method called when a longPress is recognized.
func centerButtonLongPressed(tabBarView: TabBarView!){
for gestureRecognizer in tabBarView.centerButton.gestureRecognizers!{
if let longPressGestureRecognizer = gestureRecognizer as? LongPressGestureRecongnizerSubclass{
longPressGestureRecognizer.detectTouchesMoved = true
longPressGestureRecognizer.toCallWhenTouchesMoved = ({(touches: Set<UITouch>) -> (Void) in
//Here I can view touches properties every time touchesMoved() is called, but not when I try to transition view here.
})
}
}
}
UPDATE:
I realized that the problem is that since the transition is being called synchronously, it messes with touchesMoved()
. When I move the transition method off the UI thread, the following error is thrown:
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
How can I prevent the slow, synchronous code of the transition from messing with the touchesMoved()
?
How can I get achieve what I am envisioning? Thanks for any help.