0

I have a UIViewController with a UIScrollView on it. I wanted to have a behavior in my app as follows:

1) user opens the app and sees the photo in normal size (default size of the UIScrollView)

2) when user single taps on the photo, it goes to full screen and also user is able to zoom in and move the photo around

3) when user single taps again on the photo in full screen mode it shrinks to the original size from point no 1) and zooming and moving around is disabled.

For handling the scrolling and moving the photo around I've decided to go with this plugin: https://github.com/huynguyencong/ImageScrollView .

My code is as follows:

@IBOutlet weak var requestPhoto: ImageScrollView!

var originalPhotoSize :CGRect?
var isPhotoOnFullScreen: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapHandler:")

    self.requestPhoto.addGestureRecognizer(tapGestureRecognizer)
    self.requestPhoto.userInteractionEnabled = true

    originalPhotoSize = requestPhoto.frame
}


override func viewDidAppear(animated: Bool) {
    super.viewWillAppear(animated)
    requestPhoto.scrollEnabled = false
    requestPhoto.pinchGestureRecognizer?.enabled = false
    requestPhoto.panGestureRecognizer.enabled = false
    requestPhoto.zoomScale = 1
}

func tapHandler(sender: UITapGestureRecognizer) {

    if sender.state == .Ended {
        if(isPhotoOnFullScreen == true){
            self.requestPhoto.frame = originalPhotoSize!
            self.requestPhoto.zoomScale = 1
            self.requestPhoto.pinchGestureRecognizer?.enabled = false
            self.requestPhoto.scrollEnabled = false
            isPhotoOnFullScreen = false
        }else{
            self.requestPhoto.frame = self.view.frame
            self.requestPhoto.pinchGestureRecognizer?.enabled = true
            self.requestPhoto.scrollEnabled = true
            isPhotoOnFullScreen = true
        }

}
}

And my exact problems are:

1) when user opens the app he can pinch to zoom on the small photo (even though I set up requestPhoto.pinchGestureRecognizer?.enabled = false in viewDidLoad)

2) when user taps the photo it opens on full screen. That's fine, but when he pinches to zoom and at some point single taps the photo, it goes back to the small size, but the zoom level is not set to default (even though I'm setting self.requestPhoto.zoomScale = 1)

Can you help me with those two problems above?

====EDIT

speaking about problem no. 1 - when user opens the app he can pinch to zoom the photo around, but when he opens the image on full screen and comes back to normal mode - then zooming is disabled. Why it's not disabled from the very beginning?

speaking about problem no. 2 - somehow this line self.requestPhoto.zoomScale = 1 is not working, when I printed the value of self.requestPhoto.zoomScale right after setting it to 1, it still has some different value, I'm not sure whether it's this plugin ( https://github.com/huynguyencong/ImageScrollView ) fault or am I doing something wrong?

randomuser1
  • 2,733
  • 6
  • 32
  • 68

1 Answers1

0

You can use native UIScrollView to do your achievements. You not need to use third party SDK.

If you choose to build your app with native ScrollViewDelegate, you can use this scrollView delegate method for the set zoom levels.

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        scrollView.minimumZoomScale = 1
        scrollView.minimumZoomScale = 10
        return self.imageView // Your Image View.
    }
emresancaktar
  • 1,507
  • 17
  • 25
  • Thanks for this! However, as for now I've decided to stay with the 3rd party sdk and tried to fix my bugs, if that won't work I will try to use your solution! Do you maybe know what might be the reason of my problems though? – randomuser1 Mar 19 '16 at 13:28