zyterence
10/9/2016 - 9:07 AM

SharingViewController.swift

//
//  SharingViewController.swift
//
//  Created by zhangyang on 2016/10/9.
//  Copyright 2016 zhangyang. All rights reserved.
//

import UIKit
import SnapKit

class SharingViewController: UIViewController {

    static let shareViewheight = UIScreen.main.bounds.width/4 + 10
    static let shareButtonWidth = UIScreen.main.bounds.width/5
    static let shareIconWidth = UIScreen.main.bounds.width/8
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.32)
        self.view.isOpaque = false
        
        let containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.backgroundColor = .white
        
        let dissmissButton = UIButton()
        dissmissButton.translatesAutoresizingMaskIntoConstraints = false
        dissmissButton.addTarget(self, action: #selector(SharingViewController.dissmis), for: .touchUpInside)

        view.addSubview(containerView)
        containerView.snp.makeConstraints { (make) -> Void in
            make.height.equalTo(SharingViewController.shareViewheight)
            make.bottom.equalTo(self.view)
            make.left.equalTo(self.view)
            make.right.equalTo(self.view)
        }
        
        view.addSubview(dissmissButton)
        dissmissButton.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.view)
            make.bottom.equalTo(containerView.snp.top)
            make.left.equalTo(self.view)
            make.right.equalTo(self.view)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = "分享到:"
        titleLabel.font = .systemFont(ofSize: 16)
        titleLabel.textAlignment = .center
        titleLabel.textColor = .gray
        containerView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(containerView)
            make.left.equalTo(containerView)
            make.right.equalTo(containerView)
            make.height.greaterThanOrEqualTo(35)
        }
        
        guard let sharingBar = stackView() else { return }
        sharingBar.backgroundColor = .clear
        containerView.addSubview(sharingBar)
        sharingBar.snp.makeConstraints { (make) in
            make.centerX.equalTo(containerView)
            make.top.equalTo(titleLabel.snp.bottom)
            make.bottom.lessThanOrEqualTo(containerView)
        }
        
    }

}

extension SharingViewController {
    
    func stackView() -> UIStackView? {
        let stackView = UIStackView()
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        stackView.alignment = .center
        stackView.spacing = 30
        
        guard let chatImage = UIImage(named: "icon_share_chat"),
            let momentsImage = UIImage(named: "icon_share_moments"),
            let collectionImage = UIImage(named: "icon_share_collection") else {
                return nil
        }
        
        let chat = sharingItemView(title: "微信好友", icon: chatImage) {
            print("shared to session")
        }
        let moments = sharingItemView(title: "微信朋友圈", icon: momentsImage) {
            print("shared to moments")
        }
        let collection = sharingItemView(title: "微信收藏", icon: collectionImage) {
            print("shared to collection")
        }
        
        stackView.addArrangedSubview(chat)
        stackView.addArrangedSubview(moments)
        stackView.addArrangedSubview(collection)
        
        return stackView
    }
    
    func sharingItemView(title: String, icon: UIImage, whenTapped:@escaping ()->Void) -> UIView {
        
        let imageWidth = SharingViewController.shareIconWidth
        let containerWidth = SharingViewController.shareButtonWidth
        let container = UIView()
        container.translatesAutoresizingMaskIntoConstraints = false
        
        let image = UIImageView(image: icon)
        container.addSubview(image)
        image.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: imageWidth, height: imageWidth))
            make.top.equalTo(container)
            make.centerX.equalTo(container)
        }
        
        let label = UILabel()
        label.text = title
        label.font = .systemFont(ofSize: 13)
        label.textColor = .gray
        label.textAlignment = .center
        container.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.top.equalTo(image.snp.bottom)
            make.left.equalTo(container)
            make.right.equalTo(container)
            make.height.equalTo(20)
            make.bottom.lessThanOrEqualTo(container)
        }
        
        container.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width:containerWidth , height:containerWidth))
        }
        
        container.isUserInteractionEnabled = true
        let gesture = SharingTapGestureRecognizer(target: self, action: #selector (self.share))
        gesture.completion = whenTapped
        container.addGestureRecognizer(gesture)
        
        return container
    }
    
    func share(gestureRecognizer: SharingTapGestureRecognizer) {
        self.dismiss(animated: false) {
            gestureRecognizer.completion?()
        }
    }
    
    @objc func dissmis(){
        print("dismiss")
        self.dismiss(animated: false, completion: nil)
    }

}

class SharingTapGestureRecognizer: UITapGestureRecognizer {
    var completion: (()->Void)?
}