How to enable scaling in UIScrollView

How to enable zoom effect in UIScrollView?

+51
iphone uiscrollview
Sep 07 '10 at 9:33
source share
5 answers

Watch this video

Description from the video:

  • Add UIScrollViewDelegate Delegate
  • Take UIScrollView
  • Take the UIImageView to be added to Scrollview
  • Place the scroll view in the Main window
  • Connect delegates
  • ImageView setup
  • Set scale / zoom scale [main thing here]
  • Implement delegate method to return view
+9
Sep 07 '10 at 9:48
source share

The answer is here :

The scroll view also handles the scaling and panning of content. When the user makes an input or output gesture, the scroll view adjusts the offset and scale of the content. When the gesture ends, the entity that controls the presentation of the content should update the subviews of the content as necessary. (Note that the gesture may end, and the finger may still be down.) During the gesture, the scroll view does not send any tracking calls to the subview.

The UIScrollView class can have a delegate that must accept the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming: withView: atScale:; In addition, the maximum (maximum ZoomScale) and minimum (minimum ZoomScale) zoom should be different.

So:

  1. You need a delegate that implements UIScrollViewDelegate and is configured to delegate in your instance of UIScrollView
  2. In your viewForZoomingInScrollView: you must implement one method: viewForZoomingInScrollView: ( which should return a view of the content that you are interested in scaling ). You can also implement scrollViewDidEndZooming:withView:atScale: optional.
  3. In your UIScrollView you must set minimumZoomScale and maximumZoomScale (they are 1.0 by default).

Note: The most interesting thing is that if you want to break the scaling. Is it enough to return nil to viewForZooming... ? This disrupts the scaling, but some gestures will be corrupted (for two fingers). Therefore, to stop scaling, you must set the minimum and maximum scaling to 1.0.

+188
May 26 '11 at 19:14
source share

Read this Ray Wenderlich tutorial:

http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift

If you go to the “Scrolling and zooming in a larger image” section, it will get the image up and allow you to pinch and zoom.

In case of changing the link, here is the basic information: Put this code in your view controller (this sets the basic functionality):

 override func viewDidLoad() { super.viewDidLoad() // 1 let image = UIImage(named: "photo1.png")! imageView = UIImageView(image: image) imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:image.size) scrollView.addSubview(imageView) // 2 scrollView.contentSize = image.size // 3 var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewDoubleTapped:") doubleTapRecognizer.numberOfTapsRequired = 2 doubleTapRecognizer.numberOfTouchesRequired = 1 scrollView.addGestureRecognizer(doubleTapRecognizer) // 4 let scrollViewFrame = scrollView.frame let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height let minScale = min(scaleWidth, scaleHeight); scrollView.minimumZoomScale = minScale; // 5 scrollView.maximumZoomScale = 1.0 scrollView.zoomScale = minScale; // 6 centerScrollViewContents() } 

Add this to the class:

 func centerScrollViewContents() { let boundsSize = scrollView.bounds.size var contentsFrame = imageView.frame if contentsFrame.size.width < boundsSize.width { contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0 } else { contentsFrame.origin.x = 0.0 } if contentsFrame.size.height < boundsSize.height { contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0 } else { contentsFrame.origin.y = 0.0 } imageView.frame = contentsFrame } 

And then this is if you want the double-tap sign to be recognized:

 func scrollViewDoubleTapped(recognizer: UITapGestureRecognizer) { // 1 let pointInView = recognizer.locationInView(imageView) // 2 var newZoomScale = scrollView.zoomScale * 1.5 newZoomScale = min(newZoomScale, scrollView.maximumZoomScale) // 3 let scrollViewSize = scrollView.bounds.size let w = scrollViewSize.width / newZoomScale let h = scrollViewSize.height / newZoomScale let x = pointInView.x - (w / 2.0) let y = pointInView.y - (h / 2.0) let rectToZoomTo = CGRectMake(x, y, w, h); // 4 scrollView.zoomToRect(rectToZoomTo, animated: true) } 

If you want to read the tutorial in more detail, but that pretty much covers it.

+8
Nov 02 '15 at 14:20
source share

Make sure you set your viewController as a delegate to scrollViews and implement:

 func viewForZooming(in scrollView: UIScrollView) -> UIView? { return imageView } 
0
Nov 03 '17 at 13:55 on
source share

I don’t think this works for iOS 5.0 and Xcode 4.3+. I searched the same here, I found this for images, but it can help you.

http://www.youtube.com/watch?v=Ptm4St6ySEI

-one
Aug 31 2018-12-12T00:
source share



All Articles