简体   繁体   中英

Swift - UIView on top of UIImageView/UIScrollView

I need to annotate an image which is a pan and zoom UIImageView in a UIScrollView using the 'ImageScrollView' class.

At certain pixel coordinates, I need to add markers to the image displayed. So that when the user pinches zooms and pans, the markers move in their respective pixel locations and move as the image moves. The idea is like pins in Maps app, but on a UIImageView. The markers must stay the same size regardless of the image zooms.

The markers would be an image or text.

What is the easiest way to place markers at pixel locations on a UIImageView that moves as the image moves and resizes?

The pixel coordinates are CGFloats x and y.

In case anyone wants the answer:

     let tappedPoint: CGPoint = gestureRecognizer.location(in: self.zoomView!)

     let imageName = "pointer"
     let image = UIImage(named: imageName)
     let imageView = UIImageView(image: image!)

     let frame: CGRect =  CGRect(x: Int(tappedPoint.x), y: Int(tappedPoint.y), width: 64, height: 64)
     imageView.frame = frame;

     imageView.contentMode = UIViewContentMode.scaleAspectFill
     self.zoomView?.addSubview(imageView)

So create a UIView, set the coordinates and the size as a CGRect, and add the subview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM