ios - tapGesture hyperLink swift -
i have image , want go website when hit picture used tapgesture convert image big button , don't know how , want app take user website when user hit image
that depends bit on want link open. 2 standard approaches either open url in uiwebview provide inside app, or tell system open link in mobile safari browser (which send app background).
to me sounds second behaviour want. can achieve telling uiapplication open url, so:
@ibaction func linktapped(sender:uitapgesturerecognizer) { if let url = nsurl(string: "http://stackoverflow.com/") { uiapplication.sharedapplication().openurl(url) } }
edit:
some more info on how set in way described: in viewdidload
, set gesture recognizer this:
let tapgesturerecognizer = uitapgesturerecognizer(target: self, action: "linktapped:") self.yourimageview.addgesturerecognizer(tapgesturerecognizer) self.yourimageview.userinteractionenabled = true
make sure iboutlet yourimageview
connected correctly. add code given in original answer method same class containing viewdidload
method. if gesture recognizer fires, should execute code in linktapped:
method , open url.
edit 2:
and because fits in ~10 lines of code, here's minimal view controller class example implementation.
class viewcontroller: uiviewcontroller { @iboutlet var myimageview: uiimageview! //check if connected correctly! override func viewdidload() { super.viewdidload() let tapgesturerecognizer = uitapgesturerecognizer(target: self, action: "linktapped:") myimageview.addgesturerecognizer(tapgesturerecognizer) myimageview.userinteractionenabled = true } func linktapped(sender:uitapgesturerecognizer) { if let url = nsurl(string: "http://stackoverflow.com/") { uiapplication.sharedapplication().openurl(url) } } }
Comments
Post a Comment