UIScrollView Tutorial: Getting Started

Awesome @ckrych . Exactly what I was looking for. Paging scroll view =D

My Zoom Scale always set to 1.After converting this code to objective c
-(void)updateMinZoomScaleForSize:(CGSize)size {

CGFloat widthScale = size.width / self.imageView.bounds.size.width;
CGFloat heightScale = size.height / self.imageView.bounds.size.height;
CGFloat minScale = MIN(widthScale, heightScale);

self.imageScrollView.minimumZoomScale = minScale;

self.imageScrollView.zoomScale = minScale;

}

Can you tell me my mistake.

I notice that from collection view to zoom view you set the image by name

override func viewDidLoad() { imageView.image = UIImage(named: photoName) }
what if the image is loaded from PHAsset, here is what I tried

 override func viewDidLoad() {
        super.viewDidLoad()
        let imgManager = PHCachingImageManager()
        
        _ = imgManager.requestImageForAsset(self.devicePhotosAsset[self.index] as! PHAsset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: nil, resultHandler: {(result, info) -> Void in
                self.imageView.image = result

            })
    }

the problem is when selecting a image from collection view, the zoom view will show a full size image, when I tap on the screen, the image then zoomed out and placed at center of scrollview, and if I swipe to next image , first it shows the full size and then zoomed out suddenly. I don’t know how to fix it

Hello ckrych,

in this tutorial, i can not dissmiss keyboard when i Tap another space, i tried many times

please explain to me.

In “adjustInsetForKeyboardShow” function you need to use “UIKeyboardFrameEndUserInfoKey” key when keyboard is showing and “UIKeyboardFrameBeginUserInfoKey” when keyboard is hiding, or finally you get two different heights and your scrollView size would be shrinking every time you show and then dismiss keyboard. My working func looks like that:

func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
let key = show ? UIKeyboardFrameEndUserInfoKey : UIKeyboardFrameBeginUserInfoKey
if let userInfo = notification.userInfo {
if let value = userInfo[key] as? NSValue {
let keyboardFrame = value.CGRectValue()
let adjustmentHeight = (CGRectGetHeight(keyboardFrame) + 20) * (show ? 1 : -1)
scrollView.contentInset.bottom += adjustmentHeight
scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}
}
}

I notice you say that in view controllers the keyboard automatically moves things out of the way. I have never seen it do that. I have always had to write code for that. Have I been missing a setting or something that does that?

You’ve probably already solved this, but for those who are having trouble with this part of the tutorial, when you drag the Tap Gesture Recognizer to the Photo Comment View Controller Scene, be sure to place it within the scrollView container. I used the document outline to do this. Your connections inspector will then look like this:

Thanks for great tutorial! It helped me a lot!

But in case photos downloaded from web. Where downloading process has to be made? What used as placeholder for UIImageView (otherwise autolayout complains about errors)?

Hi, I want to combine photocommentviewcontroller and zoomed photoviewcontroller together, which means at first the photo is smallest, and I could use two fingers to scale it and move it(with one finger and here’s the problem), and also I can swipe to switch pages to show different images. But the problem is that it will stuck sometimes and I think it may be the conflict of moving the images in the current page and trying to switching to a next/last page. So any ideas to remove this? Thanks in advance!

Hi Where can I see the original objective C tutorial? I am not using swift. Thanks.

I think https://www.raywenderlich.com/?p=10518 may be the original link but it jumps to the updated page.

Hi, in the Scrolling and Zooming a Large Image part, I added top, bottom, trailing, leading for both imageView and scrollView (unchecked margins) but there’s an ambiguity / layout error. I’ve checked your sample project and I think i did the same. Any solutions?

The UIImageView has no inherent size, hence the error. Add a default image to solve it :slight_smile:

1 Like

Faithfully recreated part one in a new project. Wired outlets etc. However it does not appear to work. Possibly an XCode 8 issue. Have gone through P1 twice now. Scrolling kicks in, but not resizing.
Sorry - my bad - missed the early step (Back in Main.storyboard, wire up the Scroll View to the Zoomed View Controller by attaching it to the scrollView outlet and setting Zoomed View Controller as the Scroll View’s delegate)

The delegate function viewForZoomingInScrollView is now:

func viewForZooming(in scrollView: UIScrollView) → UIView?

Hence, to enable pinching zoom:

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

Likewise,

func scrollViewDidZoom(scrollView: UIScrollView)

is now

func scrollViewDidZoom(_ scrollView: UIScrollView)

I would have bet cash that those latter two method signatures would be the same, but … no. (As of Apple Swift version 3.0.2)

If you want to use couple UITextFields I highly recommend make small changes in this method (Swift 3):

Great tutorial, thanks for writing it! I noticed that the UIPageControl has a slightly weird rendering behavior when loading. The black background color appears partly on the right side, and it takes a little hiccup before the black color draws on the rest of the control. You can see it on the animated GIF in the “Putting it all together” section. Any ideas here? Is it just on the emulator? (I don’t have a physical device right now!)

Hi,

I am a learner in SWIFT. It is a great tutorial on UIScrollView. How to load images from an URL ? Also it will be great if there are explanation about starter kit in the beginning of this tutorial. It is good to know the concept or build our own starter kit for this UIScrollview tutorial.