简体   繁体   中英

Swift - Mapping invisible button on UIImage which zoom In/Out

Can some one suggest the best way to do this..

In the Below Image

  • Add invisible buttons over the image for each section of the map.
  • The image should be in a scrollview to zoom in and out the image.
  • Zooming in/out should not change the button mapping area
  • Should support for all iPhone models.

在此处输入图片说明

What I did..

Added a UIImageView inside a UIScrollView(for zoom in/out) and on viewDidAppear() manually added invisible button for the UIImage like this.

self.image.frame = CGRect(x: xValue , y:yValue, width: 60, height: 60)

Is there any better solution for finding the UIImage coordinates?

Very late to the party, but this should work:

in ViewDidLoad add this:

    scrollView.minimumZoomScale = 0.5
    scrollView.maximumZoomScale = 6.0
    scrollView.contentSize = self.imageView.frame.size
    scrollView.delegate = self

After ViewDidLoad add this:

// MARK: - Update the minimum zoom scale each time the controller updates its subviews
override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    updateMinZoomScaleForSize(view.bounds.size)
}

Then create a file private function to calculate the zoom scale for scrollView

fileprivate func updateMinZoomScaleForSize(_ size: CGSize) {
    let widthScale = size.width / imageView.bounds.width
    let heightScale = size.height / imageView.bounds.height
    let minScale = min(widthScale, heightScale)

    imageScrollView.minimumZoomScale = minScale
    imageScrollView.zoomScale = minScale
}

Are you using nine imageViews? No. it's not correct it seems.

Try like this :

  1. Take a collection view and give necessary constraints like top, bottom, left and right.
  2. Add image view to collection view cell. Add corner radius properties(it seems ur image views need this).
  3. For zoom in and zoom out actions, add pinch gesture to collection view.
  4. Add tap gesture for image view, instead of buttons.

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