Commit 4331e9c4 authored by jz's avatar jz

迁移一部分到 swift

parent f3b40c71
//
// WMTagView.swift
// test
//
// Created by wangyang on 15/12/8.
// Copyright © 2015年 Wanmiechuangyi. All rights reserved.
//
import UIKit
//import GMBaseSwift
/*
高效的,可以在table及collectionView中使用的tagView。
现在用在了日记本卡片中,以保证首页流畅度。其它地方涉及需求时再改动
*/
@objcMembers
class WYTagView: GMView {
/// 在layoutsubview中使用该属性最后设置self的宽度
var maxWidth: CGFloat = Constant.screenWidth
var tapBlock: ((_ index: Int) -> Void)?
var tags: [String] = [] {
didSet {
for (index, view) in self.subviews.enumerated() {
let label = view as! GMLabel
// 如果视图的index超过数据的长度,则隐藏
if index >= tags.count {
label.isHidden = true
} else {
label.isHidden = false
label.text = tags[index]
}
}
}
}
override func layoutSubviews() {
super.layoutSubviews()
// 如果视图的right已经超过最大宽度,则隐藏
for (index, label) in self.subviews.enumerated() where label.frame.maxX > self.maxWidth {
// 经过产品王昕 确认iOS 如果超出显示...
if index > 0 {
label.isHidden = true
} else {
label.width = self.maxWidth
}
}
}
/// 在初始化方法
///
/// - Parameters:
/// - maxWidth: 最大宽度
/// - maxTagCount: 预估的tag label最大个数
/// - tagSpace: tag之间的距离
/// - customLabelUI: 自定义tag label的UI
convenience init(maxWidth: CGFloat, maxTagCount: Int, tagSpace: CGFloat, customLabelUI: ((GMLabel) -> Void)?) {
self.init()
self.maxWidth = maxWidth
// self不剪切,以显示全部的Label内容。见下面label.centerY的约束
clipsToBounds = false
var preLabel: UIView! = nil
for i in 0..<maxTagCount {
let label = GMLabel(frame: CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
label.textColor = UIColor.mainVisual
label.font = UIFont.gmFont(13)
label.backgroundColor = UIColor.clear
label.layer.masksToBounds = true
addSubview(label)
label.snp.makeConstraints({ (make) in
if i == 0 {
make.left.equalTo(0)
} else {
make.left.equalTo(preLabel.snp.right).offset(tagSpace)
}
make.centerY.equalToSuperview()
})
preLabel = label
customLabelUI?(label)
}
// 点击手势:注意,是加在self上的,而不是每个label一个
let tap = UITapGestureRecognizer(target: self, action: #selector(WYTagView.tapAction(_:)))
addGestureRecognizer(tap)
}
@objc private func tapAction(_ tap: UITapGestureRecognizer) {
// 因为 pointInside:withEvent已经保证了是点击了tagView,那么剩下只需要判断点击的x是在哪个label范围就行了。
// 所以强制生成一个CGPoint(x: point.x, y: 0),只计算x范围
let point = tap.location(in: self)
for (i, view) in self.subviews.enumerated() {
if view.frame.minX < point.x && view.frame.maxX > point.x {
tapBlock?(i)
break
}
}
}
/// 为tag增加一点热区,主要是y方向,并且只在点击到了subview,才算作命中
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
if gm_point(inside: point, with: event) {
for view in self.subviews {
// 视图没有被隐藏,并且在视图的范围内
if !view.isHidden &&
view.frame.minX < point.x && view.frame.maxX > point.x {
return true
}
}
return false
} else {
return false
}
}
}
//
// FixHorzontalLayoutButton.swift
// Gengmei
//
// Created by wangyang on 16/5/16.
// Copyright © 2016年 更美互动信息科技有限公司. All rights reserved.
//
import UIKit
//import GMBaseSwift
/**
* @author licong, 15-09-27 17:09:12
*
* Button同时存在图片和文字时候的排列样式
*
* @since 5.2.0
*/
@objc public enum ImageTitleType: Int {
case imageLeftTitleRight = 0
case imageRightTitleLeft = 1
case imageTopTitleBottom = 2
case imageBottomTitleTop = 3
func isHorizontalLayout() -> Bool {
if self.rawValue < 2 {
return true
} else {
return false
}
}
}
/// 注意:不能在tableView或者collectionView中使用
@objcMembers
public class AllLayoutButton: GMButton {
public var type = ImageTitleType.imageRightTitleLeft
public var space: CGFloat = 5.0
public var margin = UIEdgeInsets.zero
public convenience init(image: UIImage?, title: String?, titleColor: UIColor?, font: UIFont?) {
self.init()
setTitle(title, for: .normal)
setImage(image, for: .normal)
setTitleColor(titleColor, for: .normal)
if font != nil {
titleLabel?.font = font
}
}
override public func layoutSubviews() {
super.layoutSubviews()
self.titleEdgeInsets = UIEdgeInsets.zero
self.imageEdgeInsets = UIEdgeInsets.zero
/* 以水平模式来讲解。把titleLabel、image和space看作一个整体square,这个整体的大小正好可以用 intrinsicContentSize 来表示。
centerX是button的中心点
|----------------------------------|
| |
| |--------------| |-------| |
| | Label | s| Image | |
| | | | | |
| |--------------| |-------| |
| |
|----------------------------------|
|centerX
| squareSize |
*/
let buttonWidth = bounds.size.width
let buttonHeight = bounds.size.height
let centerX = buttonWidth / 2.0
let centerY = buttonHeight / 2.0
let squareSize = intrinsicContentSize
let squareLeft = floor(centerX - squareSize.width / 2)
let squareRight = floor(centerX + squareSize.width / 2)
let spuareTop = floor(centerY - squareSize.height / 2)
let spuareBottom = floor(centerY + squareSize.height / 2)
switch type {
case .imageLeftTitleRight:
imageView?.left = squareLeft + margin.left
titleLabel?.right = squareRight - margin.right
case .imageRightTitleLeft:
titleLabel?.left = squareLeft + margin.left
imageView?.right = squareRight - margin.right
case .imageTopTitleBottom:
imageView?.top = spuareTop + margin.top
imageView?.centerX = centerX
titleLabel?.bottom = spuareBottom - margin.bottom
titleLabel?.width = squareSize.width
titleLabel?.centerX = centerX
case .imageBottomTitleTop:
titleLabel?.top = spuareTop + margin.top
titleLabel?.width = squareSize.width
titleLabel?.centerX = centerX
imageView?.bottom = spuareBottom - margin.bottom
imageView?.centerX = centerX
}
}
override public var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
size.width += margin.left + margin.right
size.height += margin.top + margin.bottom
let labelHeight = titleLabel?.intrinsicContentSize.height
let labelWidth = titleLabel?.intrinsicContentSize.width
let imageViewHeight = imageView?.intrinsicContentSize.height
let imageViewWidth = imageView?.intrinsicContentSize.width
if type.isHorizontalLayout() {
size.width += space
} else {
//竖直image和title竖直排列
size.height = labelHeight! + imageViewHeight! + space
if labelWidth != nil && imageViewWidth != nil && labelWidth! > imageViewWidth! {
size.width = labelWidth!
} else {
size.width = imageViewWidth!
}
}
return size
}
// 强制刷新,否则 layout会不正确
override public func setTitle(_ title: String?, for state: UIControl.State) {
super.setTitle(title, for: state)
self.layoutIfNeeded()
}
// 强制刷新,否则 layout会不正确
override public func setImage(_ image: UIImage?, for state: UIControl.State) {
super.setImage(image, for: state)
self.layoutIfNeeded()
}
}
//
// GMActionSheet.swift
// Gengmei
//
// Created by 汪俊 on 2017/2/7.
// Copyright © 2017年 更美互动信息科技有限公司. All rights reserved.
//
import UIKit
//import GMBaseSwift
/// item文字的颜色
@objc public enum GMActionSheetItemType: Int {
case `default` // 默认
case cancel // 取消
}
/// alertView item的数据
public struct GMActionSheetItemObject {
var title: String
var type: GMActionSheetItemType
var color: UIColor
var handler: ItemClickBlock?
init(title: String, type: GMActionSheetItemType, color: UIColor, block: ItemClickBlock?) {
self.title = title
self.type = type
self.color = color
self.handler = block
}
}
public typealias ItemClickBlock = (_ index: Int) -> Void
/// 日记本详情页编辑日记本弹出的自定义的一个alertView,从底部向上弹出
@objcMembers
public class GMActionSheet: GMView {
fileprivate let tableView = GMTableView(frame: .zero, style: .grouped)
fileprivate var itemObjs = [GMActionSheetItemObject]()
fileprivate let coverBackView = GMView()
fileprivate var alertTitle = ""
fileprivate var cancelItemObj = GMActionSheetItemObject(title: "", type: .cancel, color: UIColor.headlineText, block: nil)
public override func setup() {
super.setup()
self.backgroundColor = UIColor.white
coverBackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(coverBackTapAction)))
coverBackView.backgroundColor = UIColor.black
coverBackView.alpha = 0
coverBackView.frame = Constant.screenBounds
tableView.registerCell(GMActionSheetCell.classForCoder())
tableView.isScrollEnabled = false
backgroundColor = UIColor.background
addSubview(tableView)
tableView.frame = CGRect(x: 0, y: 0, width: Constant.screenWidth, height: 0)
tableView.rowHeight = 49
tableView.delegate = self
tableView.dataSource = self
}
// 添加alert title
public func addAlertTitle(_ title: String, color: UIColor) {
alertTitle = title
if title.isEmpty {
return
}
let header = GMView(frame: CGRect(x: 0, y: 0, width: Constant.screenWidth, height: 49))
header.backgroundColor = UIColor.white
let headerTitle = GMLabel(textColor: color, fontSize: 16)
headerTitle.textAlignment = .center
headerTitle.frame = CGRect(x: 0, y: 0, width: Constant.screenWidth, height: 49)
headerTitle.text = title
header.addSubview(headerTitle)
header.addBottomLine()
self.tableView.tableHeaderView = header
}
public func addItem(title: String, style: GMActionSheetItemType, color: UIColor, handle: ItemClickBlock? = nil) {
if style == .default {
let itemObj = GMActionSheetItemObject(title: title, type: style, color: color, block: handle)
itemObjs.append(itemObj)
} else {
cancelItemObj = GMActionSheetItemObject(title: title, type: style, color: color, block: handle)
}
}
}
// MARK: - UITableViewDataSource, UITableViewDelegate
@objc extension GMActionSheet: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return itemObjs.count
}
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeue(cell: GMActionSheetCell.classForCoder(), for: indexPath) as! GMActionSheetCell
if indexPath.section == 0 {
let obj = itemObjs[indexPath.row]
cell.label.text = obj.title
cell.label.textColor = obj.color
cell.bottomLine.isHidden = (indexPath.row == itemObjs.count - 1)
} else {
cell.label.text = cancelItemObj.title
cell.label.textColor = cancelItemObj.color
}
return cell
}
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if section == 0 {
return UIView()
} else {
return nil
}
}
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if section == 0 {
return 10
} else {
return 0
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return UIView()
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.01
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if itemObjs[indexPath.row].handler != nil {
itemObjs[indexPath.row].handler!(indexPath.row)
}
hide()
} else {
if cancelItemObj.handler != nil {
cancelItemObj.handler!(indexPath.row)
}
hide()
}
}
override public func willMove(toSuperview newSuperview: UIView?) {
if newSuperview != nil {
tableView.reloadData()
var itemCout = 0
if alertTitle.isEmpty {
itemCout = itemObjs.count + 1
} else {
itemCout = itemObjs.count + 2
}
let alertViewHeight = itemCout * 49 + 10
tableView.height = CGFloat(alertViewHeight)
var resizedHeight = CGFloat(alertViewHeight)
if #available(iOS 11.0, *) {
resizedHeight += newSuperview!.safeAreaInsets.bottom
}
self.frame = CGRect(x: 0, y: Constant.screenHeight, width: Constant.screenWidth, height: resizedHeight)
UIView.animate(withDuration: 0.25, animations: {
self.top = Constant.screenHeight - self.height
self.coverBackView.alpha = 0.5
})
}
}
}
// MARK: - action
@objc extension GMActionSheet {
public dynamic func show() {
UIApplication.shared.keyWindow?.addSubview(coverBackView)
UIApplication.shared.keyWindow?.addSubview(self)
}
public dynamic func hide() {
UIView.animate(withDuration: 0.25, animations: {
self.top = Constant.screenHeight
self.coverBackView.alpha = 0
}, completion: { (_) in
self.removeFromSuperview()
self.coverBackView.removeFromSuperview()
})
}
@objc dynamic func coverBackTapAction() {
hide()
}
}
@objcMembers
class GMActionSheetCell: GMTableViewCell {
var label = GMLabel(textAlignment: .center, backgroundColor: UIColor.clear, textColor: UIColor.headlineText, fontSize: 15)
override func setup() {
super.setup()
contentView.addSubview(label)
label.snp.makeConstraints { (make) in
make.edges.equalTo(UIEdgeInsets.zero)
}
}
}
//
// GMAdLabel.swift
// Gengmei
//
// Created by Terminator on 2016/12/22.
// Copyright © 2016年 更美互动信息科技有限公司. All rights reserved.
//
import UIKit
//import GMBaseSwift
@objcMembers
public class GMAdLabel: GMLabel {
var ADText = "" {
didSet {
adLabel.text = ADText
}
}
var adLabel = GMLabel()
var isSHowAD = false {
didSet {
if isSHowAD {
adLabel.isHidden = false
} else {
adLabel.isHidden = true
}
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
public var adLabelTop = 0 {
didSet {
adLabel.snp.remakeConstraints { (make) in
make.left.equalTo(0)
make.top.equalTo(adLabelTop)
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
adLabel.isHidden = true
adLabel.layer.cornerRadius = 3
adLabel.clipsToBounds = true
adLabel.layer.borderColor = UIColor.secondaryVisual.cgColor
adLabel.layer.borderWidth = Constant.onePixel
adLabel.backgroundColor = UIColor.white
adLabel.font = UIFont.gmFont(10)
adLabel.textColor = UIColor.secondaryVisual
// 设计图是(2, 5, 2, 5),但是由于字体本身上下有空白,所以改为
adLabel.paddingEdge = UIEdgeInsets(top: 1, left: 5, bottom: 1, right: 5)
adLabel.textAlignment = .center
addSubview(adLabel)
}
}
//
// GMCommonAlertView.swift
// Gengmei
//
// Created by Terminator on 2017/7/5.
// Copyright © 2017年 更美互动信息科技有限公司. All rights reserved.
//
import UIKit
import GMKit
//TODO: 待考虑 SNAPKIT
@objc public protocol GMCommonAlertViewDelegate: class {
func didClickCommonAlert(buttonIndex: Int, alertView: GMCommonAlertView)
}
@objcMembers
public class GMCommonAlertView: GMView {
public let alertView = GMView()
public let titleLabel = GMLabel(textColor: UIColor.headlineText, fontSize: 16)
public let subLabel = GMLabel(textColor: UIColor.auxiliaryTextLight, fontSize: 14)
public var button1 = GMGradualButton()
public var button2: GMButton!
public var button3: GMButton!
public weak var delegate: GMCommonAlertViewDelegate?
override public func setup() {
super.setup()
setupSubviews()
}
private func setupSubviews() {
backgroundColor = UIColor.black.withAlphaComponent(0.6)
addSubview(alertView)
alertView.backgroundColor = UIColor.white
alertView.layer.cornerRadius = 10
alertView.layer.masksToBounds = true
// alertView.snp.makeConstraints { (make) in
// make.centerY.equalToSuperview()
// make.centerX.equalToSuperview()
// make.size.equalTo(CGSize(width: 270, height: 280))
// }
let headerImage = GMImageView(image: UIImage(named: "notify_bell"))
addSubview(headerImage)
// headerImage.snp.makeConstraints { (make) in
// make.centerX.equalToSuperview()
// make.top.equalTo(alertView.snp.top).offset(-38)
// }
titleLabel.text = "打开推送通知"
alertView.addSubview(titleLabel)
// titleLabel.snp.makeConstraints { (make) in
// make.centerX.equalToSuperview()
// make.top.equalTo(70)
// }
subLabel.text = "第一时间接受TA的日记更新?"
alertView.addSubview(subLabel)
// subLabel.snp.makeConstraints { (make) in
// make.centerX.equalToSuperview()
// make.top.equalTo(titleLabel.snp.bottom).offset(5)
// }
button1.setTitleColor(UIColor.white, for: UIControl.State.normal)
button1.setTitle("确定打开", for: UIControl.State.normal)
button1.gradualColor = [UIColor(hex: 0x51D3E7), UIColor(hex: 0x3BE7DA)]
button1.layer.cornerRadius = 20
button1.layer.masksToBounds = true
button1.titleLabel?.font = UIFont.gmFont(16)
button1.tag = 1001
button1.addTarget(self, action: #selector(GMCommonAlertView.buttonTapAction(_:)), for: UIControl.Event.touchUpInside)
alertView.addSubview(button1)
// button1.snp.makeConstraints { (make) in
// make.centerX.equalToSuperview()
// make.top.equalTo(subLabel.snp.bottom).offset(15)
// make.size.equalTo(CGSize(width: 200, height: 40))
// }
// button1.setGradualChangeColor(colors: [UIColor(hex: 0x23E3E1), UIColor(hex: 0x2CD7E6)])
button2 = addButtonWith("不用打开", backgroundColor: UIColor.clear, titleColor: UIColor.mainVisual, borderColor: UIColor.mainVisual)
button2.addTarget(self, action: #selector(GMCommonAlertView.buttonTapAction(_:)), for: UIControl.Event.touchUpInside)
button2.tag = 1002
button2.titleLabel?.font = UIFont.gmFont(16)
alertView.addSubview(button2)
// button2.snp.makeConstraints { (make) in
// make.centerX.equalToSuperview()
// make.top.equalTo(button1.snp.bottom).offset(15)
// make.size.equalTo(CGSize(width: 200, height: 40))
// }
button3 = addButtonWith("不再提醒", backgroundColor: UIColor.clear, titleColor: UIColor.auxiliaryTextLight, borderColor: UIColor.clear)
button3.addTarget(self, action: #selector(GMCommonAlertView.buttonTapAction(_:)), for: UIControl.Event.touchUpInside)
button3.tag = 1003
alertView.addSubview(button3)
button3.titleLabel?.font = UIFont.gmFont(14)
// button3.snp.makeConstraints { (make) in
// make.centerX.equalToSuperview()
// make.top.equalTo(button2.snp.bottom).offset(10)
// make.size.equalTo(CGSize(width: 200, height: 30))
// }
}
// MARK: - 添加button
private func addButtonWith(_ title: String, backgroundColor: UIColor, titleColor: UIColor, borderColor: UIColor) -> GMButton {
let button = GMButton(title: title, backgroundColor: backgroundColor, titleFontSize: 14, titleColor: titleColor)
button.layer.cornerRadius = 20
button.layer.borderWidth = 0.5
button.layer.borderColor = borderColor.cgColor
button.layer.masksToBounds = true
return button
}
@objc func buttonTapAction(_ sender: GMButton) {
let index = sender.tag - 1000
delegate?.didClickCommonAlert(buttonIndex: index, alertView: self)
}
@objc public func show() {
UIApplication.shared.keyWindow?.addSubview(self)
self.snp.makeConstraints { (make) in
make.edges.equalTo(UIEdgeInsets.zero)
}
}
@objc public func dismiss() {
self.perform(#selector(removeFromSuperview))
}
}
//
// GMGradualButton.swift
// Gengmei
//
// Created by Terminator on 2017/7/12.
// Copyright © 2017年 更美互动信息科技有限公司. All rights reserved.
//
import UIKit
@objcMembers
open class GMGradualButton: GMButton {
// 如果gradualColor 和 disableColor同时有值,他们的count要是一样的
var gradualColor = [UIColor]()
var disableColor = [UIColor]()
var intriSize = CGSize.zero
override open var intrinsicContentSize: CGSize {
intriSize = super.intrinsicContentSize
if gradualColor.count > 0 {
setBackgroundImage(setGradualChangeColor(colors: gradualColor), for: .normal)
}
if disableColor.count > 0 {
setBackgroundImage(setGradualChangeColor(colors: disableColor), for: .disabled)
}
return intriSize
}
private func setGradualChangeColor(colors: [UIColor]) -> UIImage? {
let cfColors = colors.map { $0.cgColor }
UIGraphicsBeginImageContextWithOptions(super.intrinsicContentSize, true, 0)
let context = UIGraphicsGetCurrentContext()
let colorSpace = CGColorSpaceCreateDeviceRGB()
let gradient = CGGradient(colorsSpace: colorSpace, colors: cfColors as CFArray, locations: nil)
let start = CGPoint(x: self.intriSize.width, y: 0)
let end = CGPoint(x: 0, y: self.intriSize.height)
context?.drawLinearGradient(gradient!, start: start, end: end, options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
//
// UIVIewController+GoSettings.swift
// Gengmei
//
// Created by Terminator on 2017/7/4.
// Copyright © 2017年 更美互动信息科技有限公司. All rights reserved.
//
import UIKit
@objc public extension UIViewController {
//跳转到APP设置界面 打开通知以及其其他的
@objc public func goToAppSystemSettings() {
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(URL(string: UIApplication.openSettingsURLString)!)
}
}
}
//
// WMTagView.swift
// test
//
// Created by wangyang on 15/12/8.
// Copyright © 2015年 Wanmiechuangyi. All rights reserved.
//
import UIKit
import SnapKit
//import GMBaseSwift
/*
高效的,可以在table及collectionView中使用的tagView。
现在用在了日记本卡片中,以保证首页流畅度。其它地方涉及需求时再改动
*/
@objcMembers
class WYTagView: GMView {
/// 在layoutsubview中使用该属性最后设置self的宽度
var maxWidth: CGFloat = Constant.screenWidth
var tapBlock: ((_ index: Int) -> Void)?
var tags: [String] = [] {
didSet {
for (index, view) in self.subviews.enumerated() {
let label = view as! GMLabel
// 如果视图的index超过数据的长度,则隐藏
if index >= tags.count {
label.isHidden = true
} else {
label.isHidden = false
label.text = tags[index]
}
}
}
}
override func layoutSubviews() {
super.layoutSubviews()
// 如果视图的right已经超过最大宽度,则隐藏
for (index, label) in self.subviews.enumerated() where label.frame.maxX > self.maxWidth {
// 经过产品王昕 确认iOS 如果超出显示...
if index > 0 {
label.isHidden = true
} else {
label.width = self.maxWidth
}
}
}
/// 在初始化方法
///
/// - Parameters:
/// - maxWidth: 最大宽度
/// - maxTagCount: 预估的tag label最大个数
/// - tagSpace: tag之间的距离
/// - customLabelUI: 自定义tag label的UI
convenience init(maxWidth: CGFloat, maxTagCount: Int, tagSpace: CGFloat, customLabelUI: ((GMLabel) -> Void)?) {
self.init()
self.maxWidth = maxWidth
// self不剪切,以显示全部的Label内容。见下面label.centerY的约束
clipsToBounds = false
var preLabel: UIView! = nil
for i in 0..<maxTagCount {
let label = GMLabel(frame: CGRect(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height))
label.textColor = UIColor.mainVisual
label.font = UIFont.gmFont(13)
label.backgroundColor = UIColor.clear
label.layer.masksToBounds = true
addSubview(label)
label.snp.makeConstraints({ (make) in
if i == 0 {
make.left.equalTo(0)
} else {
make.left.equalTo(preLabel.snp.right).offset(tagSpace)
}
make.centerY.equalToSuperview()
})
preLabel = label
customLabelUI?(label)
}
// 点击手势:注意,是加在self上的,而不是每个label一个
let tap = UITapGestureRecognizer(target: self, action: #selector(WYTagView.tapAction(_:)))
addGestureRecognizer(tap)
}
@objc private func tapAction(_ tap: UITapGestureRecognizer) {
// 因为 pointInside:withEvent已经保证了是点击了tagView,那么剩下只需要判断点击的x是在哪个label范围就行了。
// 所以强制生成一个CGPoint(x: point.x, y: 0),只计算x范围
let point = tap.location(in: self)
for (i, view) in self.subviews.enumerated() {
if view.frame.minX < point.x && view.frame.maxX > point.x {
tapBlock?(i)
break
}
}
}
/// 为tag增加一点热区,主要是y方向,并且只在点击到了subview,才算作命中
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
if gm_point(inside: point, with: event) {
for view in self.subviews {
// 视图没有被隐藏,并且在视图的范围内
if !view.isHidden &&
view.frame.minX < point.x && view.frame.maxX > point.x {
return true
}
}
return false
} else {
return false
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment