From 22e0d588a7a1468af7f2196460980c2673b11d93 Mon Sep 17 00:00:00 2001 From: HarriesChen Date: Thu, 17 Mar 2016 14:12:54 +0800 Subject: [PATCH] Feature(Avatar):add avatar click handle event --- .../BaseMessageCollectionViewCell.swift | 13 +++++ MessageKit/BaseMessagePresenter.swift | 5 ++ MessageKit/CustomBubbleView.swift | 13 +++-- .../MessageKitDemo/TableViewController.swift | 50 ------------------- .../MessageKitDemo/ViewController.swift | 15 ++++++ 5 files changed, 42 insertions(+), 54 deletions(-) diff --git a/MessageKit/BaseMessageCollectionViewCell.swift b/MessageKit/BaseMessageCollectionViewCell.swift index 7eaad7b..c38209a 100644 --- a/MessageKit/BaseMessageCollectionViewCell.swift +++ b/MessageKit/BaseMessageCollectionViewCell.swift @@ -105,6 +105,7 @@ public class BaseMessageCollectionViewCell UIImageView! { let imageView = UIImageView() + imageView.userInteractionEnabled = true return imageView } @@ -129,6 +130,11 @@ public class BaseMessageCollectionViewCell Void)? + @objc + private func avatarTapped(tapGestureRecognizer: UITapGestureRecognizer) { + self.onAvatarTapped?(cell: self) + } } struct BaseMessageLayoutModel { diff --git a/MessageKit/BaseMessagePresenter.swift b/MessageKit/BaseMessagePresenter.swift index 5860b44..617f251 100644 --- a/MessageKit/BaseMessagePresenter.swift +++ b/MessageKit/BaseMessagePresenter.swift @@ -28,6 +28,7 @@ public protocol BaseMessageInteractionHandlerProtocol { func userDidTapOnFailIcon(viewModel viewModel: ViewModelT) func userDidTapOnBubble(viewModel viewModel: ViewModelT) func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) + func userDidTapOnAvatar(viewModel viewModel: ViewModelT) } public class BaseMessagePresenter CGSize { - return self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth).size + return self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth).frame.size } public override func layoutSubviews() { super.layoutSubviews() - let _ = self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth) + let layout = self.calculateCustomBubbleLayout(maximumWidth: self.preferredMaxLayoutWidth) self.addSubview(self.customMessageViewModel.customView) + customMessageViewModel.customView.frame = layout.frame } private func calculateCustomBubbleLayout(maximumWidth maximumWidth: CGFloat) -> CustomBubbleLayoutModel { @@ -122,7 +123,7 @@ public final class CustomBubbleView: UIView, MaximumLayoutWidthSpecificable, Bac } private class CustomBubbleLayoutModel { - var size: CGSize = CGSize.zero + var frame: CGRect = CGRect.zero struct LayoutContext { let customViewSize: CGSize @@ -140,6 +141,10 @@ private class CustomBubbleLayoutModel { } func calculateLayout() { - size = layoutContext.customViewSize + //adjust X + var currentX: CGFloat = 0.0 + currentX += 8 + frame.origin.x = currentX + frame.size = layoutContext.customViewSize } } diff --git a/MessageKitDemo/MessageKitDemo/TableViewController.swift b/MessageKitDemo/MessageKitDemo/TableViewController.swift index cb87c39..eb1833e 100644 --- a/MessageKitDemo/MessageKitDemo/TableViewController.swift +++ b/MessageKitDemo/MessageKitDemo/TableViewController.swift @@ -26,66 +26,16 @@ class TableViewController: UITableViewController, UISplitViewControllerDelegate // MARK: - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int { - // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - // #warning Incomplete implementation, return the number of rows return 3 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) cell.textLabel?.text = "你好" - // Configure the cell... - return cell } - - /* - // Override to support conditional editing of the table view. - override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { - // Return false if you do not want the specified item to be editable. - return true - } - */ - - /* - // Override to support editing the table view. - override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { - if editingStyle == .Delete { - // Delete the row from the data source - tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) - } else if editingStyle == .Insert { - // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view - } - } - */ - - /* - // Override to support rearranging the table view. - override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { - - } - */ - - /* - // Override to support conditional rearranging of the table view. - override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { - // Return false if you do not want the item to be re-orderable. - return true - } - */ - - /* - // MARK: - Navigation - - // In a storyboard-based application, you will often want to do a little preparation before navigation - override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { - // Get the new view controller using segue.destinationViewController. - // Pass the selected object to the new view controller. - } - */ - } diff --git a/MessageKitDemo/MessageKitDemo/ViewController.swift b/MessageKitDemo/MessageKitDemo/ViewController.swift index 84b3638..e6547b7 100644 --- a/MessageKitDemo/MessageKitDemo/ViewController.swift +++ b/MessageKitDemo/MessageKitDemo/ViewController.swift @@ -24,6 +24,10 @@ class TextMessageTestHandler: BaseMessageInteractionHandlerProtocol { func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) { print("长按bubble") } + + func userDidTapOnAvatar(viewModel viewModel: ViewModelT) { + print("点击头像") + } } class PhotoMessageTestHandler: BaseMessageInteractionHandlerProtocol { @@ -40,6 +44,9 @@ class PhotoMessageTestHandler: BaseMessageInteractionHandlerProtocol { func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) { print("长按图片") } + func userDidTapOnAvatar(viewModel viewModel: ViewModelT) { + print("点击头像") + } } class FileMessageTestHandler: BaseMessageInteractionHandlerProtocol { @@ -56,6 +63,9 @@ class FileMessageTestHandler: BaseMessageInteractionHandlerProtocol { func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) { } + func userDidTapOnAvatar(viewModel viewModel: ViewModelT) { + print("点击头像") + } } class CustomMessageTestHandler: BaseMessageInteractionHandlerProtocol { @@ -72,6 +82,9 @@ class CustomMessageTestHandler: BaseMessageInteractionHandlerProtocol { func userDidLongPressOnBubble(viewModel viewModel: ViewModelT) { } + func userDidTapOnAvatar(viewModel viewModel: ViewModelT) { + print("点击头像") + } } class FakeDataSource: MessageDataSourceProtocol { @@ -170,6 +183,8 @@ class ViewController: MessageViewController { testModel(uid:"19", sid: "dd", type: .File, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true), testModel(uid:"19", sid: "dd", type: .File, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true), testModel(uid:"18", sid: "dd", type: .Text, coming: false, text: "dsfsd", isSuccess: true), + testModel(uid:"19", sid: "dd", type: .Text, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true), + testModel(uid:"18", sid: "dd", type: .Text, coming: true, text: "dsfsd", isSuccess: true), testModel(uid:"19", sid: "dd", type: .Custom, coming: true, text: "标题位置可以分别设置为上下左右,4个位置", isSuccess: true) ]