swift 点击imageView全屏预览(UIview中弹出提示框)

  @objc func didTap() {

        print("浏览照片")

        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)

        //点击图片缩小的手势

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

    }

转载于:https://my.oschina.net/u/3382458/blog/1620902

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值