Skip to content

Instantly share code, notes, and snippets.

@anirudhamahale
Last active August 20, 2017 16:12
Show Gist options
  • Select an option

  • Save anirudhamahale/f3c33fe76dcf6fb4809477bf8767f145 to your computer and use it in GitHub Desktop.

Select an option

Save anirudhamahale/f3c33fe76dcf6fb4809477bf8767f145 to your computer and use it in GitHub Desktop.
Simple code to get the touch event in the UITableView's or UICollectionView's cell.
let tap = UITapGestureRecognizer(target: self, action: #selector(self.openWebView(sender:)))
cell.advtImage.isUserInteractionEnabled = true
cell.advtImage.addGestureRecognizer(tap)
func openWebView(sender: UITapGestureRecognizer) {
let tapLocation = sender.location(in: self.collectionView) // Returns the CGPoints of the touched location
if let indexPath = self.collectionView.indexPathForItem(at: tapLocation) // Returns the IndexPath at the above location {
if let cell = self.collectionView.cellForItem(at: indexPath) as? Advertisment2 {
if let url = URL(string: cell.url) {
UIApplication.shared.openURL(url)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment