Detecting TapGesture on an ImageAttachment to execute associated action #179
Replies: 4 comments 2 replies
-
As i mentioned here #178 i can intercept a tap gesture with an overlay. but this may be a dead end- without some ad hoc geometry there’s no way to know what was clicked |
Beta Was this translation helpful? Give feedback.
-
I asked perplexity.ai to give me some hints... Here's the result.
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
textView.addGestureRecognizer(tapRecognizer)
@objc func handleTap(_ recognizer: UITapGestureRecognizer) {
let textView = recognizer.view as! UITextView
let layoutManager = textView.layoutManager
var location = recognizer.location(in: textView)
location.x -= textView.textContainerInset.left
location.y -= textView.textContainerInset.top
let characterIndex = layoutManager.characterIndex(for: location, in: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if characterIndex < textView.textStorage.length {
// Check if the character at this index has an attachment
let attributeValue = textView.attributedText.attribute(.attachment, at: characterIndex, effectiveRange: nil) as? NSTextAttachment
if let attachment = attributeValue {
// Handle the image change or any other attribute change
changeImageForAttachment(attachment)
}
}
} Now wondering if this AI hallucinations or not... |
Beta Was this translation helpful? Give feedback.
-
@martindufort, have you got a solution? im facing the same issue right now |
Beta Was this translation helpful? Give feedback.
-
There are sooo many things we should be able to do with image attachments, like resizing. But it's not trivial. Even the Notes app doesn't have good image handling. |
Beta Was this translation helpful? Give feedback.
-
I want to insert an ImageAttachment within the RichTextView that would respond to tap gestures.
So when the user taps the ImageAttachment, I need to be reported which ImageAttachment was tapped so I can take the appropriate action.
What would be the bets way to accomplish this?
Beta Was this translation helpful? Give feedback.
All reactions