user2363025
user2363025

Reputation: 6505

pinch and zoom swift

I am trying to implement a pinch and zoom on a picture passed into a controller which also contains a scroll view as per the tutorial here:

https://www.veasoftware.com/tutorials/2015/8/3/pinch-to-zoom-uiimageview-with-swift

I'm not sure what i'm doing wrong. My Code:

import UIKit
import Haneke

class imageZoomedController : UIViewController, UIScrollViewDelegate {


    @IBOutlet weak var imageView: UIImageView!
    var imageViewUrl : String?

    @IBOutlet weak var scrollView: UIScrollView!
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.scrollView.minimumZoomScale = 1.0
        self.scrollView.maximumZoomScale = 6.0
        self.imageView.userInteractionEnabled = true
        if let url = NSURL(string: imageViewUrl!) {
            print("url is \(url)")
            self.imageView!.hnk_setImageFromURL(url)
        }
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
    {
        return self.imageView!
    }


}

I can see the image on screen but pinch and zoom gestures aren't taking effect

Update

Constraints for imageView:

Align center X to superView = 0
Align center Y to superView = 0
Trailing to superView = 0
Leading to superView = 0
Bottom space to superView = 0
Top space to superView = 0

Constraints for scrollView:

Trailing space to superView = -20
Leading space to superView = -20
Top space to topLayoutGuide = 20
Bottom space to bottomLayoutGuide = 20
//also tried 0 for all these as per demo but same outcome

Upvotes: 3

Views: 1561

Answers (1)

David Sanford
David Sanford

Reputation: 745

I have used the same video. The issue is you need to set your scrollView as a delegate. Go to your IB and control drag the scrollView to the view controller and select Delegate. This should fix it.

Upvotes: 1

Related Questions