swift 3.0 全屏瀏覽圖片 + 縮放

 //MARK:全屏+縮放圖片ui

    func showZoomImageView(cell:TableViewCell) {圖片

        bgView = UIScrollView.init(frame: UIScreen.main.bounds)rem

        bgView.backgroundColor = UIColor.blackget

        bgView.delegate = selfanimation

        let tapBg1 = UITapGestureRecognizer.init(target: self, action: #selector(tapBgAction(tap:)))it

        let tapBg2 = UITapGestureRecognizer.init(target: self, action: #selector(tapBgAction(tap:)))io

        tapBg1.numberOfTapsRequired = 1require

        tapBg1.require(toFail: tapBg2)select

        tapBg2.numberOfTapsRequired = 2scroll

        bgView.minimumZoomScale = 0.5

        bgView.maximumZoomScale = 2.0

        bgView.addGestureRecognizer(tapBg1)

        bgView.addGestureRecognizer(tapBg2)

        let picView = cell.customView as! UIImageView

        let imageView = UIImageView.init(frame:picView.frame)

        imageView.image = picView.image

        imageView.frame = bgView.convert(picView.frame, from: cell)

        self.imageView = imageView

        bgView.addSubview(imageView)

        UIApplication.shared.keyWindow?.backgroundColor = UIColor.black// 設置window黑色背景,避免圖片縮放時,看到聊天記錄,體驗很差

        UIApplication.shared.keyWindow?.addSubview(bgView)

        UIView.animate(withDuration: 0.3) { 

            imageView.frame = CGRect.init(x: 0, y: 0, width: SCREENW, height: SCREENH)

            imageView.contentMode = .scaleAspectFit

        }

    }

    //MARK: 單擊 消失雙擊 放大 縮小

    func tapBgAction(tap:UITapGestureRecognizer) {

        let view = tap.view

        let touchCount:Int = tap.numberOfTapsRequired

        switch touchCount {

        case 1:

            UIView.animate(withDuration: 0.5, animations: { 

                view?.removeFromSuperview()

            })

            break

        case 2:

            self.handleDoubleClick(tap: tap)

            break

        default:

            break

        }

    }

    //MARK:處理圖片雙擊放大縮小

    var isScaleBig:Bool = false

    func handleDoubleClick(tap:UIGestureRecognizer) {

        let scale = bgView.zoomScale

        if isScaleBig{

            bgView.setZoomScale(scale / 0.3, animated: true)

            isScaleBig = false

        }else{

            bgView.setZoomScale(1.0, animated: true)

            isScaleBig = true

        }

    }

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {

        return self.imageView

    }

    func scrollViewDidZoom(_ scrollView: UIScrollView) {

        let imageView = scrollView.subviews.first as! UIImageView

        self.centerShow(scrollView: scrollView, imageView: imageView)

    }

    //MARK:居中顯示

    func centerShow(scrollView:UIScrollView,imageView:UIImageView){

        let offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width) ? (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;

        let offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height) ? (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;

        imageView.center = CGPoint.init(x: scrollView.contentSize.width * 0.5 + offsetX, y: scrollView.contentSize.height * 0.5 + offsetY)

    }

相關文章
相關標籤/搜索