//这个图片需要导入进工程中 - 名字为"popTop" 如果改变这个图片需要到HKPopView.swift中改变这个图片的名字
- 封装一个Control控件 - 作为每一个cell单元
//
// HKPopControl.swift
// HKSwiftDemo
//
// Created by isHakan on 2018/1/31.
// Copyright © 2018年 liuhuakun. All rights reserved.
//
import UIKit
class HKPopControl: UIControl {
init(frame: CGRect, imageName:String, title:String, hiddenLine:Bool) {
super.init(frame: frame)
self.frame = frame
self.backgroundColor = UIColor.init(red: 65/255.0, green: 65/255.0, blue: 65/255.0, alpha: 1)
let w = frame.size.width
let h = frame.size.height
let imageView_x:CGFloat = 8.0
let imageView_y:CGFloat = 4.0
let imageView_h = h - 2*imageView_y
let imageView_w = imageView_h
var imageView_rect = CGRect.init()
imageView_rect.origin.x = imageView_x
imageView_rect.origin.y = imageView_y
imageView_rect.size.width = imageView_w
imageView_rect.size.height = imageView_h
let imageView = UIImageView.init(frame: imageView_rect)
imageView.image = UIImage.init(named: imageName)
self.addSubview(imageView)
let label_x = imageView.frame.maxX + 8.0
let label_y = imageView_y
let label_h = imageView_h
let label_w = w-label_x
var label_rect = CGRect.init()
label_rect.origin.x = label_x
label_rect.origin.y = label_y
label_rect.size.width = label_w
label_rect.size.height = label_h
let titleLabel = UILabel.init(frame: label_rect)
titleLabel.text = title
titleLabel.textColor = UIColor.white
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.font = UIFont.systemFont(ofSize: 14.0)
self.addSubview(titleLabel)
let line_x = imageView_x
let line_y = h - 1.0
let line_w = w - 2*line_x
let line_h:CGFloat = 1.0
var line_rect = CGRect.init()
line_rect.origin.x = line_x
line_rect.origin.y = line_y
line_rect.size.width = line_w
line_rect.size.height = line_h
let lineLable = UILabel.init(frame: line_rect)
lineLable.backgroundColor = UIColor.gray
lineLable.isHidden = hiddenLine
self.addSubview(lineLable)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
- 封装一个弹出框view(popView)
//
// HKPopView.swift
// HKSwiftDemo
//
// Created by isHakan on 2018/1/31.
// Copyright © 2018年 liuhuakun. All rights reserved.
//
import UIKit
protocol HKPopCtrlCellClickDelegate:NSObjectProtocol {
func popCtrlCellClick(tag:Int)
}
class HKPopView: UIView {
//代理
var delegate:HKPopCtrlCellClickDelegate?
/// MARK - frame为弹出popView一个cell的frame
init(frame: CGRect, imageNameArr:[String], titleArr:[String]) {
super.init(frame: UIScreen.main.bounds)
//整个屏幕背景
self.frame = UIScreen.main.bounds
self.backgroundColor = UIColor.black.withAlphaComponent(0.1)
//凸起的小角
let small_w:CGFloat = 10.0
let small_h:CGFloat = 6.0
let small_y = frame.origin.y - small_h
var ww:CGFloat
if UIScreen.main.bounds.size.width == 414.0 {//iphone plus
ww = 15.0
}else {
ww = 12.0
}
let small_x = UIScreen.main.bounds.size.width - 8 - ww - small_w
var small_rect = CGRect.init()
small_rect.origin.x = small_x
small_rect.origin.y = small_y
small_rect.size.width = small_w
small_rect.size.height = small_h
let smallImageView = UIImageView.init(frame: small_rect)
smallImageView.image = UIImage.init(named: "popTop")
self.addSubview(smallImageView)
let popCtrl_w = frame.size.width
let popCtrl_h = frame.size.height
let popCtrl_x = frame.origin.x
for index in 0..<imageNameArr.count {
let popCtrl_y = frame.origin.y + CGFloat(index) * popCtrl_h
var popCtrl_rect = CGRect.init()
popCtrl_rect.origin.x = popCtrl_x
popCtrl_rect.origin.y = popCtrl_y
popCtrl_rect.size.width = popCtrl_w
popCtrl_rect.size.height = popCtrl_h
var isLastCell:Bool = false
///最后一个cell隐藏底部line
if index == imageNameArr.count-1 {
isLastCell = true
}
let popCtrl = HKPopControl.init(frame: popCtrl_rect, imageName: imageNameArr[index], title: titleArr[index], hiddenLine:isLastCell)
popCtrl.tag = 1000+index
popCtrl.addTarget(self, action: #selector(popCtrlClick), for: .touchUpInside)
self.addSubview(popCtrl)
}
UIApplication.shared.keyWindow?.addSubview(self)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animate(withDuration: 0.25, animations: {
self.alpha = 0
}) { (view) in
self.removeFromSuperview()
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
UIView.animate(withDuration: 0.25, animations: {
self.alpha = 0
}) { (view) in
self.removeFromSuperview()
}
}
@objc func popCtrlClick(popCtrl:HKPopControl) {
guard delegate == nil else {
delegate?.popCtrlCellClick(tag: popCtrl.tag)
UIView.animate(withDuration: 0.25, animations: {
self.alpha = 0
}, completion: { (view) in
self.removeFromSuperview()
})
return
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
- 在需要的界面使用起来
//
// HKUIImageViewViewController.swift
// HKSwiftDemo
//
// Created by isHakan on 2018/1/26.
// Copyright © 2018年 liuhuakun. All rights reserved.
//
import UIKit
//遵守HKPopView.swift代理
class HKUIImageViewViewController: HKBaseViewController,HKPopCtrlCellClickDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let barBtnItem = UIBarButtonItem.init(barButtonSystemItem: .add, target: self, action: #selector(barBtnItemClick))
self.navigationItem.rightBarButtonItem = barBtnItem
}
@objc func barBtnItemClick(barBtnItem:UIBarButtonItem) {
//设置每一个弹出cell的大小跟坐标 - frmae
let popViewCell_w = UIScreen.main.bounds.size.width/3.0
let popViewCell_h:CGFloat = 35.0
let popViewCell_x = UIScreen.main.bounds.size.width*2/3.0 - 8.0
let popViewCell_y:CGFloat = self.NAVIGATIONBAR_H! + 8.0//self.NAVIGATIONBAR_H!是tableBar的高度 iPhone X为88.0,其他的为64.0
var popViewCell_rect = CGRect.init()
popViewCell_rect.origin.x = popViewCell_x
popViewCell_rect.origin.y = popViewCell_y
popViewCell_rect.size.width = popViewCell_w
popViewCell_rect.size.height = popViewCell_h
//图片数组名
let imageNameArr = ["1","1","1","1"]
//label显示标题名
let titleArr = ["发起群聊","添加朋友","扫一扫","收付款"]
let popView = HKPopView.init(frame: popViewCell_rect, imageNameArr: imageNameArr, titleArr: titleArr)
popView.delegate = self
}
///点击popCtrlCell代理传回ViewController
func popCtrlCellClick(tag:Int) {
print("点击的是:\(tag)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}