Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit b5b167a

Browse files
author
Harries Chen
committed
Merge pull request #1 from teambition/develop
Feature(Avatar):add avatar click handle event
2 parents 615ae84 + 22e0d58 commit b5b167a

5 files changed

+42
-54
lines changed

MessageKit/BaseMessageCollectionViewCell.swift

+13
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public class BaseMessageCollectionViewCell<BubbleViewType where BubbleViewType:U
105105

106106
func createAvatarView() -> UIImageView! {
107107
let imageView = UIImageView()
108+
imageView.userInteractionEnabled = true
108109
return imageView
109110
}
110111

@@ -129,6 +130,11 @@ public class BaseMessageCollectionViewCell<BubbleViewType where BubbleViewType:U
129130
return tapGestureRecognizer
130131
}()
131132

133+
public private(set) lazy var tapAvatarGestureRecognizer: UITapGestureRecognizer = {
134+
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "avatarTapped:")
135+
return tapGestureRecognizer
136+
}()
137+
132138
public private (set) lazy var longPressGestureRecognizer: UILongPressGestureRecognizer = {
133139
let longpressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "bubbleLongPressed:")
134140
longpressGestureRecognizer.delegate = self
@@ -140,6 +146,7 @@ public class BaseMessageCollectionViewCell<BubbleViewType where BubbleViewType:U
140146
self.bubbleView.addGestureRecognizer(self.tapGestureRecognizer)
141147
self.bubbleView.addGestureRecognizer(self.longPressGestureRecognizer)
142148
self.avatarView = createAvatarView()
149+
self.avatarView.addGestureRecognizer(self.tapAvatarGestureRecognizer)
143150
self.timestampLabel = createTimestampView()
144151
self.contentView.addSubview(self.bubbleView)
145152
self.contentView.addSubview(self.avatarView)
@@ -253,6 +260,12 @@ public class BaseMessageCollectionViewCell<BubbleViewType where BubbleViewType:U
253260
func bubbleLongPressed() {
254261
self.onBubbleLongPressed?(cell: self)
255262
}
263+
264+
public var onAvatarTapped: ((cell: BaseMessageCollectionViewCell) -> Void)?
265+
@objc
266+
private func avatarTapped(tapGestureRecognizer: UITapGestureRecognizer) {
267+
self.onAvatarTapped?(cell: self)
268+
}
256269
}
257270

258271
struct BaseMessageLayoutModel {

MessageKit/BaseMessagePresenter.swift

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public protocol BaseMessageInteractionHandlerProtocol {
2828
func userDidTapOnFailIcon(viewModel viewModel: ViewModelT)
2929
func userDidTapOnBubble(viewModel viewModel: ViewModelT)
3030
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT)
31+
func userDidTapOnAvatar(viewModel viewModel: ViewModelT)
3132
}
3233

3334
public class BaseMessagePresenter<BubbleViewT, ViewModelBuilderT, InteractionHandlerT where
@@ -102,6 +103,10 @@ BubbleViewT: UIView, BubbleViewT:MaximumLayoutWidthSpecificable, BubbleViewT: Ba
102103
guard let sSelf = self else { return }
103104
sSelf.interactionHandler?.userDidTapOnFailIcon(viewModel: sSelf.messageViewModel)
104105
}
106+
cell.onAvatarTapped = { [weak self] (cell) in
107+
guard let sSelf = self else { return }
108+
sSelf.interactionHandler?.userDidTapOnAvatar(viewModel: sSelf.messageViewModel)
109+
}
105110
additionalConfiguration?()
106111
}, animated: animated, completion: nil)
107112
}

MessageKit/CustomBubbleView.swift

+9-4
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ public final class CustomBubbleView: UIView, MaximumLayoutWidthSpecificable, Bac
9999
// MARK: Layout
100100

101101
public override func sizeThatFits(size: CGSize) -> CGSize {
102-
return self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth).size
102+
return self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth).frame.size
103103
}
104104

105105
public override func layoutSubviews() {
106106
super.layoutSubviews()
107-
let _ = self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth)
107+
let layout = self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth)
108108

109109
self.addSubview(self.customMessageViewModel.customView)
110+
customMessageViewModel.customView.frame = layout.frame
110111
}
111112

112113
private func calculateCustomBubbleLayout(maximumWidth maximumWidth: CGFloat) -> CustomBubbleLayoutModel {
@@ -122,7 +123,7 @@ public final class CustomBubbleView: UIView, MaximumLayoutWidthSpecificable, Bac
122123
}
123124

124125
private class CustomBubbleLayoutModel {
125-
var size: CGSize = CGSize.zero
126+
var frame: CGRect = CGRect.zero
126127

127128
struct LayoutContext {
128129
let customViewSize: CGSize
@@ -140,6 +141,10 @@ private class CustomBubbleLayoutModel {
140141
}
141142

142143
func calculateLayout() {
143-
size = layoutContext.customViewSize
144+
//adjust X
145+
var currentX: CGFloat = 0.0
146+
currentX += 8
147+
frame.origin.x = currentX
148+
frame.size = layoutContext.customViewSize
144149
}
145150
}

MessageKitDemo/MessageKitDemo/TableViewController.swift

-50
Original file line numberDiff line numberDiff line change
@@ -26,66 +26,16 @@ class TableViewController: UITableViewController, UISplitViewControllerDelegate
2626
// MARK: - Table view data source
2727

2828
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
29-
// #warning Incomplete implementation, return the number of sections
3029
return 1
3130
}
3231

3332
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
34-
// #warning Incomplete implementation, return the number of rows
3533
return 3
3634
}
3735

3836
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
3937
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
4038
cell.textLabel?.text = "你好"
41-
// Configure the cell...
42-
4339
return cell
4440
}
45-
46-
/*
47-
// Override to support conditional editing of the table view.
48-
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
49-
// Return false if you do not want the specified item to be editable.
50-
return true
51-
}
52-
*/
53-
54-
/*
55-
// Override to support editing the table view.
56-
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
57-
if editingStyle == .Delete {
58-
// Delete the row from the data source
59-
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
60-
} else if editingStyle == .Insert {
61-
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
62-
}
63-
}
64-
*/
65-
66-
/*
67-
// Override to support rearranging the table view.
68-
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
69-
70-
}
71-
*/
72-
73-
/*
74-
// Override to support conditional rearranging of the table view.
75-
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
76-
// Return false if you do not want the item to be re-orderable.
77-
return true
78-
}
79-
*/
80-
81-
/*
82-
// MARK: - Navigation
83-
84-
// In a storyboard-based application, you will often want to do a little preparation before navigation
85-
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
86-
// Get the new view controller using segue.destinationViewController.
87-
// Pass the selected object to the new view controller.
88-
}
89-
*/
90-
9141
}

MessageKitDemo/MessageKitDemo/ViewController.swift

+15
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class TextMessageTestHandler: BaseMessageInteractionHandlerProtocol {
2424
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) {
2525
print("长按bubble")
2626
}
27+
28+
func userDidTapOnAvatar(viewModel viewModel: ViewModelT) {
29+
print("点击头像")
30+
}
2731
}
2832

2933
class PhotoMessageTestHandler: BaseMessageInteractionHandlerProtocol {
@@ -40,6 +44,9 @@ class PhotoMessageTestHandler: BaseMessageInteractionHandlerProtocol {
4044
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) {
4145
print("长按图片")
4246
}
47+
func userDidTapOnAvatar(viewModel viewModel: ViewModelT) {
48+
print("点击头像")
49+
}
4350
}
4451

4552
class FileMessageTestHandler: BaseMessageInteractionHandlerProtocol {
@@ -56,6 +63,9 @@ class FileMessageTestHandler: BaseMessageInteractionHandlerProtocol {
5663
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) {
5764

5865
}
66+
func userDidTapOnAvatar(viewModel viewModel: ViewModelT) {
67+
print("点击头像")
68+
}
5969
}
6070

6171
class CustomMessageTestHandler: BaseMessageInteractionHandlerProtocol {
@@ -72,6 +82,9 @@ class CustomMessageTestHandler: BaseMessageInteractionHandlerProtocol {
7282
func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) {
7383

7484
}
85+
func userDidTapOnAvatar(viewModel viewModel: ViewModelT) {
86+
print("点击头像")
87+
}
7588
}
7689

7790
class FakeDataSource: MessageDataSourceProtocol {
@@ -170,6 +183,8 @@ class ViewController: MessageViewController {
170183
testModel(uid:"19", sid: "dd", type: .File, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true),
171184
testModel(uid:"19", sid: "dd", type: .File, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true),
172185
testModel(uid:"18", sid: "dd", type: .Text, coming: false, text: "dsfsd", isSuccess: true),
186+
testModel(uid:"19", sid: "dd", type: .Text, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true),
187+
testModel(uid:"18", sid: "dd", type: .Text, coming: true, text: "dsfsd", isSuccess: true),
173188
testModel(uid:"19", sid: "dd", type: .Custom, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true)
174189
]
175190

0 commit comments

Comments
 (0)