Swift click imageView full screen preview (a prompt box pops up in UIview)

  @objc func didTap() {

        print("Browse photos")

        let image = self.chatImageView.image

        let window = self.viewController().view

        let backgroundView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: WIDTH, height: HEIGHT))

        oldFrame = self.chatImageView.convert(self.chatImageView.bounds, to: window)

        backgroundView.backgroundColor = UIColor.black

        backgroundView.alpha = 0.5

        let imageView = UIImageView.init(frame: oldFrame)

        imageView.image = image

        imageView.tag = 1

        backgroundView.addSubview(imageView)

        window?.addSubview(backgroundView)

        // Click the image to zoom out gesture

        let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.hideImage(tap:)))

        tap.numberOfTapsRequired = 1

        let longTap = UILongPressGestureRecognizer.init(target: self, action: #selector(self.alertShow))

        backgroundView.addGestureRecognizer(tap)

        backgroundView.addGestureRecognizer(longTap)

 

        UIView.animate(withDuration: 0.3) {

            imageView.frame =  CGRect.init(x: 0, y: (HEIGHT - (image?.size.height)! * WIDTH/(image?.size.width)!)/2, width: WIDTH, height: (image?.size.height)! * WIDTH/(image?.size.width)!)

            backgroundView.alpha = 1

        }

    }

    

    @objc func hideImage (tap: UITapGestureRecognizer) {

        let backgroundView = tap.view

        let imageView = tap.view?.viewWithTag(1) as! UIImageView

        UIView.animate(withDuration: 0.3, animations: {

            imageView.frame = self.oldFrame

            backgroundView?.alpha = 0

        }) { (finished) in

            backgroundView?.removeFromSuperview()

        }

       

    }

    

    @objc func alertShow() {

 

        let alert = UIAlertController.init(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)

        let action1 = UIAlertAction.init(title: "Download image", style: UIAlertActionStyle.default) { (action) in

 

        }

        let action2 = UIAlertAction.init(title: "Cancel", style: UIAlertActionStyle.cancel) { (action) in

 

        }

        alert.addAction(action1)

        alert.addAction(action2)

        self.viewController().present(alert, animated: true, completion: nil)

    }

    //Get the controller where the current view is located

    func viewController () -> (UIViewController){

        var next:UIResponder?

        next = self.next!

        repeat {

            if ((next as?UIViewController) != nil) {

                return (next as! UIViewController)

            }else {

                next = next?.next

            }

        } while next != nil

        return UIViewController()

    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325146154&siteId=291194637
Recommended