ios - Detecting tap on a UITextView -


i have been struggling detect tap on uitextview swift.

my uitextviews in table, must able detect links , press them, links length unknown.

also if tap on cell, don't tap on link, want push new uiviewcontroller on navigation controller stack.

i tried create own textview overwrite touchescancelled, wasn't success. detects cancellation isn't considered tap on real device.

the bug doesn't occur in simulator, seems can't tap on real device, long press work.

class linktextview: uitextview{     override func touchescancelled(touches: set<nsobject>!, withevent event: uievent!) {          self.textviewwastapped(self)      } } 

i tried adding directly gesture recognizer. didn't have success there either. doesn't call gesture recognizer @ all.

i added uigesturereconizer delegate uiviewcontroller , lines in

func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {      var singletap : uigesturerecognizer = uigesturerecognizer(target: self, action: "taptextview:")         singletap.delegate = self              cell.mtextoutlet.attributedtext = mymutablestring              cell.mtextoutlet.addgesturerecognizer(singletap)  

in linktextview class :

class linktextview: uitextview{     func taptextview(stapgesture: uigesturerecognizer){         println("tapping1")     } } 

i looked forums , found post : post. suggests use chhlinktextview. tried use want detect link automatically, normal uitextview does.

i did try using checkbox in interface builder parse links chhlinktextview, doesn't work. didn't see in documentation suggesting done.

how should proceed ?

you're close first attempt in subclassing uitextview, instead of overriding touchescancelled, you'll want override of touches* methods, i.e.

  • touchesbegan
  • touchesmoved
  • touchesended
  • touchescancelled

in overridden methods, send touch down responder chain getting textview's nextresponder(), check isn't nil, , call method on next responder.

the reason works because uitextview intercept touch if it's on url , uiresponder methods never called -- try implementing textview:shouldinteractwithurl:inrange in tableviewcell or wherever, , you'll see it's called before touch events passed along.

this should minimum you're trying (it's repetitive short):

class passthroughtextview: uitextview {     override func touchesbegan(touches: set<nsobject>, withevent event: uievent) {         if let next = nextresponder() {             next.touchesbegan(touches, withevent: event)         }         else {             super.touchesbegan(touches, withevent: event)         }      }      override func touchesended(touches: set<nsobject>, withevent event: uievent) {         if let next = nextresponder() {             next.touchesended(touches, withevent: event)         }         else {             super.touchesended(touches, withevent: event)         }     }      override func touchescancelled(touches: set<nsobject>!, withevent event: uievent!) {         if let next = nextresponder() {             next.touchescancelled(touches, withevent: event)         }         else {             super.touchescancelled(touches, withevent: event)         }     }      override func touchesmoved(touches: set<nsobject>, withevent event: uievent) {         if let next = nextresponder() {             next.touchesmoved(touches, withevent: event)         }         else {             super.touchesmoved(touches, withevent: event)         }     }   }  // in case want url or textview in tableviewcell... class tableviewcell: uitableviewcell, uitextviewdelegate {     @iboutlet var textview: passthroughtextview!      func textview(textview: uitextview, shouldinteractwithurl url: nsurl, inrange characterrange: nsrange) -> bool {         println("textview shouldinteractwithurl")         return true     }  } 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -