-
Notifications
You must be signed in to change notification settings - Fork 792
/
TSMessageActionFloatView.swift
executable file
·171 lines (146 loc) · 5.51 KB
/
TSMessageActionFloatView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// TSMessageActionFloatView.swift
// TSWeChat
//
// Created by Hilen on 3/8/16.
// Copyright © 2016 Hilen. All rights reserved.
//
import UIKit
import SnapKit
import RxSwift
import Dollar
private let kActionViewWidth: CGFloat = 140 //container view width
private let kActionViewHeight: CGFloat = 190 //container view height
private let kActionButtonHeight: CGFloat = 44 //button height
private let kFirstButtonY: CGFloat = 12 //the first button Y value
class TSMessageActionFloatView: UIView {
weak var delegate: ActionFloatViewDelegate?
let disposeBag = DisposeBag()
override init (frame: CGRect) {
super.init(frame : frame)
self.initContent()
}
convenience init () {
self.init(frame:CGRect.zero)
self.initContent()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
fileprivate func initContent() {
self.backgroundColor = UIColor.clear
let actionImages = [
TSAsset.Contacts_add_newmessage.image,
TSAsset.Barbuttonicon_add_cube.image,
TSAsset.Contacts_add_scan.image,
TSAsset.Receipt_payment_icon.image,
]
let actionTitles = [
"发起群聊",
"添加朋友",
"扫一扫",
"收付款",
]
//Init containerView
let containerView : UIView = UIView()
containerView.backgroundColor = UIColor.clear
self.addSubview(containerView)
containerView.snp.makeConstraints { (make) -> Void in
make.top.equalTo(self.snp.top).offset(3)
make.right.equalTo(self.snp.right).offset(-5)
make.width.equalTo(kActionViewWidth)
make.height.equalTo(kActionViewHeight)
}
//Init bgImageView
let stretchInsets = UIEdgeInsets.init(top: 14, left: 6, bottom: 6, right: 34)
let bubbleMaskImage = TSAsset.MessageRightTopBg.image.resizableImage(withCapInsets: stretchInsets, resizingMode: .stretch)
let bgImageView: UIImageView = UIImageView(image: bubbleMaskImage)
containerView.addSubview(bgImageView)
bgImageView.snp.makeConstraints { (make) -> Void in
make.edges.equalTo(containerView)
}
//init custom buttons
var yValue = kFirstButtonY
for index in 0 ..< actionImages.count {
let itemButton: UIButton = UIButton(type: .custom)
itemButton.backgroundColor = UIColor.clear
itemButton.titleLabel!.font = UIFont.systemFont(ofSize: 17)
itemButton.setTitleColor(UIColor.white, for: UIControl.State())
itemButton.setTitleColor(UIColor.white, for: .highlighted)
let title = Dollar.fetch(actionTitles, index, orElse: "")
itemButton.setTitle(title, for: .normal)
itemButton.setTitle(title, for: .highlighted)
let image = Dollar.fetch(actionImages, index, orElse: nil)
itemButton.setImage(image, for: .normal)
itemButton.setImage(image, for: .highlighted)
itemButton.addTarget(self, action: #selector(TSMessageActionFloatView.buttonTaped(_:)), for: UIControl.Event.touchUpInside)
itemButton.contentHorizontalAlignment = .left
itemButton.contentEdgeInsets = UIEdgeInsets.init(top: 0, left: 12, bottom: 0, right: 0)
itemButton.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)
itemButton.tag = index
containerView.addSubview(itemButton)
itemButton.snp.makeConstraints { (make) -> Void in
make.top.equalTo(containerView.snp.top).offset(yValue)
make.right.equalTo(containerView.snp.right)
make.width.equalTo(containerView.snp.width)
make.height.equalTo(kActionButtonHeight)
}
yValue += kActionButtonHeight
}
//add tap to view
let tap = UITapGestureRecognizer()
self.addGestureRecognizer(tap)
tap.rx.event.subscribe { _ in
self.hide(true)
}.disposed(by:self.disposeBag)
self.isHidden = true
}
@objc func buttonTaped(_ sender: UIButton!) {
guard let delegate = self.delegate else {
self.hide(true)
return
}
let type = ActionFloatViewItemType(rawValue:sender.tag)!
delegate.floatViewTapItemIndex(type)
self.hide(true)
}
/**
Hide the float view
- parameter hide: is hide
*/
func hide(_ hide: Bool) {
if hide {
self.alpha = 1.0
UIView.animate(withDuration: 0.2 ,
animations: {
self.alpha = 0.0
},
completion: { finish in
self.isHidden = true
self.alpha = 1.0
})
} else {
self.alpha = 1.0
self.isHidden = false
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
/**
* TSMessageViewController Float view delegate methods
*/
protocol ActionFloatViewDelegate: class {
/**
Tap the item with index
*/
func floatViewTapItemIndex(_ type: ActionFloatViewItemType)
}
enum ActionFloatViewItemType: Int {
case groupChat = 0, addFriend, scan, payment
}