ios - Gesture Recognizer Not Called -
i have set gesture recognizer dismissing keyboard when user taps outside textfield. dismisskeyboard
function not called.
have set observer wrong or different issue? also, tableview being tapped.
code excerpt
class commentsviewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { override func viewdidappear(animated: bool) { super.viewdidappear(animated) nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("keyboardwillshow:"), name: uikeyboardwillshownotification, object: nil) nsnotificationcenter.defaultcenter().addobserver(self, selector: selector("keyboardwillhide:"), name: uikeyboardwillhidenotification, object: nil) } func keyboardframechanged(notification: nsnotification) { println("keyboardframechanged") let userinfo = notification.userinfo let key = uikeyboardframeenduserinfokey if let info = userinfo { let framevalue = info[key] as! nsvalue let _frame = framevalue.cgrectvalue() } } func keyboardwillshow(notification: nsnotification) { if keyboarddismisstapgesture == nil { println("dismiss") keyboarddismisstapgesture = uitapgesturerecognizer(target: self, action: selector("dismisskeyboard:")) self.view.addgesturerecognizer(keyboarddismisstapgesture!) } } func keyboardwillhide(notification: nsnotification) { if keyboarddismisstapgesture != nil { println("test2") self.view.removegesturerecognizer(keyboarddismisstapgesture!) keyboarddismisstapgesture = nil } } func dismisskeyboard(sender: anyobject) { println("dismiss keyboard") commenttextfield.resignfirstresponder() }
i set breakpoint @ dismisskeyboard, doesn't called.
output
when tap textview , keyboard opens, output
keyboardframechanged keyboardframechanged show dismiss
when tap else (trying dismiss keyboard), no further outputs.
set gesture recognizer delegate , add uigesturerecognizerdelegate
protocol.
func keyboardwillshow(notification: nsnotification) { if keyboarddismisstapgesture == nil { println("dismiss") keyboarddismisstapgesture = uitapgesturerecognizer(target: view, action: "dismisskeyboard:") keyboarddismisstapgesture.delegate = self self.view.addgesturerecognizer(keyboarddismisstapgesture!) } }
then add:
func gesturerecognizer(gesturerecognizer: uigesturerecognizer, shouldrecognizesimultaneouslywithgesturerecognizer othergesturerecognizer: uigesturerecognizer) -> bool { return true }
my guess is, gesture of table view interfering new uitapgesture
. try solution or else should fail gesture of table view when new uitapgesture
detected.
to fail uitapgesturerecognizer
of table view use code:
if let recognizers = yourtableview.gesturerecognizers, let index = find(recognizers.map { $0 uitapgesturerecognizer }, true) { (recognizers[index] as! uipangesturerecognizer).requiregesturerecognizertofail(keyboarddismisstapgesture) }
maybe not elegant way of doing it, works me when want fail uipangesturerecognizer
. haven't tested uitapgesturerecognizer
.
edit:
if let recognizers = yourtableview.gesturerecognizers, let index = find(recognizers.map { $0 uigesturerecognizer }, true) { (recognizers[index] as! uigesturerecognizer).requiregesturerecognizertofail(keyboarddismisstapgesture!) }
Comments
Post a Comment