xib图解:
import UIKit
class ShareUMView: UITableViewCell {
@IBOutlet weak var topView: UIView!
@IBOutlet weak var bottomViewH: NSLayoutConstraint!
@IBOutlet weak var shareTitleLabel: UILabel!
@IBOutlet weak var contentViewbs: UIView!
var titleArr: [String]! = []//["朋友圈", "微信", "QQ", "微博", "空间", "邀请卡"]
var imageArr: [String]! = []//["share1", "share2", "share3", "share4", "share5", "share6"]
override func awakeFromNib() {
super.awakeFromNib()
self.topView.addGestureRecognizer({
return UITapGestureRecognizer.init(target: self, action: #selector(self.dismissAction(tap:)))
}())
self.contentView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
}
func config(titles: [String], images: [String]) {
self.titleArr = titles
self.imageArr = images
config()
}
private let topSpace: CGFloat = 12.0
private let W = UIScreen.main.bounds.width / 3
private let H: CGFloat = CGFloat(75)
func config() {
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)
}
bottomViewH.constant = contentViewbs.frame.minY + contentH + 20
for i in 0..<imageArr.count {
let viewX = W * CGFloat(i % 3)
let viewY = (topSpace + H) * CGFloat(i / 3)
let containViewme = UIView(frame: CGRect.init(x: viewX, y: viewY, width: W, height: H))
contentViewbs.addSubview(containViewme)
let iv = UIImageView.init(frame: CGRect.init(x: (containViewme.frame.width - 50) / 2, y: 0, width: 50, height: 50))
iv.image = UIImage.init(named: imageArr[i])
iv.isUserInteractionEnabled = true
containViewme.addSubview(iv)
let label = UILabel.init(frame: CGRect.init(x: 0, y: iv.frame.maxY, width: containViewme.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
containViewme.addSubview(label)
let button = UIButton.init(frame: containViewme.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)
containViewme.addSubview(button)
}
}
var shareSuccessClosure:((String) -> Void)!
@objc func action(sender: UIButton) {
let titleStr = sender.currentTitle!
print("titleStr---->\(titleStr)")
//此处处理对应的分享
self.removeFromSuperview()
switch titleStr {
case "朋友圈":
break
case "微信":
break
case "QQ":
break
case "微博":
break
case "空间":
break
case "邀请卡":
break
default:
break
}
shareSuccessClosure(sender.currentTitle!)
}
@objc func dismissAction(tap: UITapGestureRecognizer) {
self.removeFromSuperview()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
//实际使用
let shareV = Bundle.main.loadNibNamed("ShareAndGetView", owner: self, options: nil)?.first as! ShareAndGetView
shareV.configData(frameCustom: self.view.bounds, imageStr: nil, getMoneyStr: nil, mark: 0)
shareV.shareCloaure = {(shareStr) -> Void in
let sharetoView = Bundle.main.loadNibNamed("ShareUMView", owner: self, options: nil)?.first as! ShareUMView
sharetoView.config(titles: ["朋友圈", "微信", "QQ", "微博", "空间", "邀请卡"], images: ["share1", "share2", "share3", "share4", "share5", "share6"])
sharetoView.shareSuccessClosure = {(titleStr) -> Void in
print("分享页面的点击\(titleStr)")
}
self.view.addSubview(shareV)