底部推出一个半透明的页面,类似友盟分享页面

 

/************

实现思路:

UIViewController的背景设置为: self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)//必须的

使用时候:vc.modalPresentationStyle = .overCurrentContextOK

**********/

//MARK: 实际使用

 let vc = ShareofBSVC()

 vc.titleArr = ["朋友圈", "微信", "QQ", "微博", "空间", "邀请卡"]

 vc.imageArr =  ["share1", "share2", "share3", "share4", "share5", "share6"]

 vc.modalPresentationStyle = .overCurrentContext

 self.present(vc, animated: true, completion: nil)

 ************************************/


class ShareofBSVC: UIViewController {


    var titleArr: [String]!//["朋友圈", "微信", "QQ", "微博", "空间", "邀请卡"]

    var imageArr: [String]!//["share1", "share2", "share3", "share4", "share5", "share6"]

    

    override func viewDidLoad() {

        super.viewDidLoad()

        self.configUI()

    }

    

    private let superView = UIView()

    private let topSpace: CGFloat = 12.0

    private let W =  UIScreen.main.bounds.width / 3

    private let H: CGFloat = CGFloat(75)

    

    func configUI() {

        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)//必须的

        var contentH: CGFloat = 0

        if imageArr.count % 3 > 0 {

            contentH = CGFloat(imageArr.count / 3 + 1) * (H + topSpace)

        } else {

            contentH = CGFloat(imageArr.count / 3) * (H + topSpace)

        }

        contentH = 30 + 2 * topSpace + contentH + 20

        let frame = CGRect.init(x: 0, y: UIScreen.main.bounds.height - contentH, width: UIScreen.main.bounds.width, height: contentH)

        superView.frame = frame

        superView.backgroundColor = UIColor.white

        self.view.addSubview(superView)

        

        let titleLabel = UILabel.init(frame: CGRect.init(x: 0, y: 15, width: superView.frame.width, height: 15))

        titleLabel.text = "分享"

        titleLabel.font = UIFont.init(name: "PingFang-SC-Medium", size: 17)

        titleLabel.textAlignment = .center

        titleLabel.textColor = UIColor.init(red: 140 / 255, green: 140 / 255, blue: 142 / 255, alpha: 1)

        superView.addSubview(titleLabel)

        

        for i in 0..<imageArr.count {

            let viewX = W * CGFloat(i % 3)

            let viewY = titleLabel.frame.maxY + 2 * topSpace + (topSpace + H) * CGFloat(i / 3)

            

            let containView = UIView(frame: CGRect.init(x: viewX, y: viewY, width: W, height: H))

            superView.addSubview(containView)

            

            

            let iv = UIImageView.init(frame: CGRect.init(x: (containView.frame.width - 50) / 2, y: 0, width: 50, height: 50))

            iv.image = UIImage.init(named: imageArr[i])

            iv.isUserInteractionEnabled = true

            containView.addSubview(iv)

            

            let label = UILabel.init(frame: CGRect.init(x: 0, y: iv.frame.maxY, width: containView.frame.width, height: 25))

            label.textAlignment = .center

            label.font = UIFont.init(name: "PingFang-SC-Medium", size: 13)

            label.textColor = UIColor.init(red: 140 / 255, green: 140 / 255, blue: 142 / 255, alpha: 1)

            label.text = self.titleArr[i]

            label.isUserInteractionEnabled = true

            containView.addSubview(label)

            

            let button = UIButton.init(frame: containView.bounds)

            button.setTitle(titleArr[i], for: .normal)

            button.setTitleColor(UIColor.clear, for: .normal)

            button.setTitleColor(UIColor.clear, for: .selected)

            button.addTarget(self, action: #selector(self.action(sender:)), for: .touchUpInside)

            containView.addSubview(button)

        }

    }


    //分享成功的闭包

    var shareSuccessClosure:((String) -> Void)!

    @objc func action(sender: UIButton) {

        let titleStr = sender.currentTitle!

        print("titleStr---->\(titleStr)")

        //此处处理对应的分享

        self.dismiss(animated: true, completion: nil)

        switch titleStr {

        case "朋友圈":

            break

        case "微信":

            break

        case "QQ":

            break

        case "微博":

            break

        case "空间":

            break

        case "邀请卡":

            break

        default:

            break

        }

        shareSuccessClosure(sender.currentTitle!)


    }

    

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        self.dismiss(animated: true, completion: nil)

        

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

    

    



}


猜你喜欢

转载自blog.csdn.net/FlyingFireFish/article/details/80228815