0

I'm trying to follow the instructions from this post How to zoom in/out an UIImage object when user pinches screen? but I don't know how to implement these methods. I know that is a basic question but if anyone could take a minute to help a beginner I would greatly appreciate it thanks!

Edit: Error I'm getting is 'optional' can only be applied to protocol members.

This stuff is incredibly difficult to figure out for a beginner... is there an easier way to learn coding??

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var scrollView: UIScrollView!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    scrollView.delegate = self


    optional func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {


        return self.ImageView;


    }
}

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

}

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

}

Community
  • 1
  • 1
Nathaniel
  • 81
  • 1
  • 6

2 Answers2

0

You error is because of the 'optional' tag you have before your viewForZoomingInScrollView method (function). The optional tag is only for use in protocols.

This method also needs to be removed from the viewDidLoad method, but still be inside the class like this:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    scrollView.delegate = self

}

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {

    return self.ImageView

}

You may want to read a more thorough tutorial about getting it working. Here is one: http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift

WaterNotWords
  • 997
  • 1
  • 9
  • 24
  • Also check out the following link with a bunch of resources on getting started. https://github.com/vsouza/awesome-ios#getting-started – WaterNotWords May 30 '15 at 11:12
0

In Swift 5.0, the delegate method should be writed in this way:

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