I almost gave up after half a day of debugging, the action listener has to be defined before it can be used, and it can't have the same name as UIGestureRecognizerDelegate.
class MainActivity :AppCompatActivity(){
// gesture recognizer actionlistener
private val mActionListener={recognizer:UIGestureRecognizer->
// gesture recognized
}
override fun onCreate(savedInstanceState:Bundle?){
...
val delegate=UIGestureRecognizerDelegate();
// single tap gesture
val recognizer1=new UITapGestureRecognizer(this)
recognizer1.tapsRequired=1
recognizer1.touchesRequired=1
recognizer1.tag="single-tap";
recognizer1.actionListener=mActionListener
// double tap gesture
val recognizer2=UITapGestureRecognizer(this)
recognizer2.tag="double-tap"
recognizer2.tapsRequired=2
recognizer2.touchesRequired=1
recognizer2.actionListener=mActionListener
// We want to recognize a single tap and a double tap separately. Normally, when the user
// performs a double tap, the single tap would be triggered twice.
// In this way, however, the single tap will wait until the double tap will fail. So a single tap
// and a double tap will be triggered separately.
recognizer1.requireFailureOf=recognizer2
// add both gestures to the delegate
delegate.addGestureRecognizer(recognizer)
delegate.addGestureRecognizer(recognizer2)
// forward the touch events to the delegate
val rootView=findViewById(R.id.root)
rootView.setGestureDelegate(delegate)
// optional delegate methods
delegate.shouldReceiveTouch={recognizer->true}
delegate.shouldBegin={recognizer->true}
delegate.shouldRecognizeSimultaneouslyWithGestureRecognizer={recognizer,other->true}
}
}
I almost gave up after half a day of debugging, the action listener has to be defined before it can be used, and it can't have the same name as UIGestureRecognizerDelegate.