skip to Main Content

I have an imageview in scrollview (I followed this video https://www.youtube.com/watch?v=0Tz0vI721c8). I want to draw a green dot on that image at x=100, y=100 (from upper left corner of the image). When user zoom in/out and pan, the dot stays at that location (image coord 100,100). How do I do that? I am totally new to ios dev.

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setupScrollView()
    }

    func setupScrollView() {
        scrollView.delegate = self
    }

}

extension ViewController:UIScrollViewDelegate {
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
}

2

Answers


  1. Create a imageView(dot) to Frame layout, attach dot align top and leading to Frame layout.

    enter image description here
    enter image description here

    Login or Signup to reply.
  2. Try ur relative object(dot) layout to absolute object(imageView).

        let dot = UIView.init()
        dot.backgroundColor = .red
        //Use layout
        dot.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(dot)
        let left = dot.leadingAnchor.constraint(equalTo: imageView.leadingAnchor, constant: 100)
        let top = dot.topAnchor.constraint(equalTo: imageView.topAnchor, constant: 100)
        let width = dot.widthAnchor.constraint(equalToConstant: 10)
        let height = dot.heightAnchor.constraint(equalToConstant: 10)
        var layout:[NSLayoutConstraint] = [left,top,width,height]
        NSLayoutConstraint.activate(layout)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search