swift 點擊imageView全屏預覽(UIview中彈出提示框)

  @objc func didTap() {ide

        print("瀏覽照片")ui

        let image = self.chatImageView.image.net

        let window = self.viewController().view圖片

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

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

        backgroundView.backgroundColor = UIColor.blackanimation

        backgroundView.alpha = 0.5it

        let imageView = UIImageView.init(frame: oldFrame)io

        imageView.image = imageselect

        imageView.tag = 1

        backgroundView.addSubview(imageView)

        window?.addSubview(backgroundView)

        //點擊圖片縮小的手勢

        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: "下載圖片", 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)

    }

    //獲取當前視圖所在控制器

    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()

    }

相關文章
相關標籤/搜索