Search code examples
iosswiftuitapgesturerecognizer

How to add something on an image in swift


I have an anatomic picture and on it, I want to print a image like a dot or something when the user taps on the first image (the body) to point out where does it hurts.

I've already read something on UITapGestureRecognizer, but I don't really understood how it works.


Solution

  • Try this:

    override func viewDidLoad() {
        super.viewDidLoad()
        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap))
        self.imageView.addGestureRecognizer(gestureRecognizer)
    }
    
    @objc func handleTap(tap: UITapGestureRecognizer) {
        let circle = UIView()
        circle.center = tap.locationInView(imageView)
        circle.frame.size = CGSize(width: 30, height: 30)
        circle.layer.backgroundColor = UIColor.redColor().CGColor
        circle.layer.cornerRadius = 15
        imageView.addSubview(circle)
    }