Scale ImageView inside UIScrollView to center and fit

时间:2015-07-28 23:04:46

标签: ios objective-c swift uiscrollview uigesturerecognizer

I've been building a UIScrollView with GestureRecognizer to place UIImageView inside UIScrollView scaled to the center of the UIScrollView. but i am facing a little problem with scaling the Image to fit inside UIScrollView. i have to zoom with finger then the image comes from the left side(out of the screen) of the screen phone to the center. but i just wanted the image to be scaled to be fit when my UIViewController loads.

So here is my code viewDidLoad:

scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = myimageView.bounds.size
scrollView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
scrollView.contentOffset = CGPoint(x: 1000, y: 450)

scrollView.addSubview(myimageView)
view.addSubview(scrollView)

scrollView.delegate = self

setZoomScale()

setupGestureRecognizer()

And the other methods to Scale and zoom :

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

        return myimageView
    }

    override func viewWillLayoutSubviews() {
        setZoomScale()
    }

    func scrollViewDidZoom(scrollView: UIScrollView) {
        let imageViewSize = myimageView.frame.size
        let scrollViewSize = scrollView.bounds.size

        let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
        let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0

        scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
    }

    func setZoomScale() {
        let imageViewSize = myimageView.bounds.size
        let scrollViewSize = scrollView.bounds.size
        let widthScale = scrollViewSize.width / imageViewSize.width
        let heightScale = scrollViewSize.height / imageViewSize.height

        scrollView.minimumZoomScale = min(widthScale, heightScale)
//        self.scrollView.sizeToFit()
        scrollView.zoomScale = 0
    }

    func setupGestureRecognizer() {
        let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
        doubleTap.numberOfTapsRequired = 2
        scrollView.addGestureRecognizer(doubleTap)
    }

    func handleDoubleTap(recognizer: UITapGestureRecognizer) {

        if (scrollView.zoomScale > scrollView.minimumZoomScale) {
            scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
        } else {
            scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
        }
    }

For some images which the height for it is large this issue happened but not for squared images. and i think it can be fixed from here : scrollView.minimumZoomScale but i can't make it right !

0 个答案:

没有答案