state machine for more MessageToolbarState

This commit is contained in:
nixzhu
2015-04-21 18:32:47 +08:00
parent 4864651a0e
commit bdde0bbd28
2 changed files with 82 additions and 24 deletions
@@ -225,17 +225,6 @@ class ConversationViewController: UIViewController {
}
}
messageToolbar.imageSendAction = { messageToolbar in
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: { () -> Void in
self.messageToolbarBottomConstraint.constant = self.moreMessageTypesViewHeightConstraintConstant
self.view.layoutIfNeeded()
}, completion: { (finished) -> Void in
})
}
// MARK: Audio Send
messageToolbar.voiceSendBeginAction = { messageToolbar in
@@ -344,6 +333,32 @@ class ConversationViewController: UIViewController {
}
}
// MARK: MessageToolbar State Transitions
messageToolbar.transitionToStateDefaultAction = { previousState in
if previousState == .MoreMessages && !self.isKeyboardVisible {
UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: { () -> Void in
self.messageToolbarBottomConstraint.constant = 0
self.view.layoutIfNeeded()
}, completion: { (finished) -> Void in
})
}
}
messageToolbar.transitionToStateMoreMessagesAction = { previousState in
UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: { () -> Void in
self.messageToolbarBottomConstraint.constant = self.moreMessageTypesViewHeightConstraintConstant
self.view.layoutIfNeeded()
}, completion: { (finished) -> Void in
})
}
// MARK: More Message Types
choosePhotoButton.tapAction = {
@@ -688,7 +703,7 @@ class ConversationViewController: UIViewController {
self.keyboardHeight = keyboardHeight
UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions(animationCurveValue << 16), animations: { () -> Void in
self.messageToolbarBottomConstraint.constant = keyboardHeight
let keyboardAndToolBarHeight = keyboardHeight + CGRectGetHeight(self.messageToolbar.bounds)
@@ -713,7 +728,7 @@ class ConversationViewController: UIViewController {
self.conversationCollectionView.setContentOffset(contentOffset, animated: false)
}
}else{
} else {
var contentOffset = self.conversationCollectionViewContentOffsetBeforeKeyboardWillShow
contentOffset.y += keyboardHeight
@@ -746,8 +761,11 @@ class ConversationViewController: UIViewController {
let keyboardHeight = keyboardEndFrame.height
UIView.animateWithDuration(animationDuration, delay: 0, options: UIViewAnimationOptions(animationCurveValue << 16), animations: { () -> Void in
self.messageToolbarBottomConstraint.constant = 0
self.view.layoutIfNeeded()
if self.messageToolbar.state != .MoreMessages {
self.messageToolbarBottomConstraint.constant = 0
self.view.layoutIfNeeded()
}
var contentOffset = self.conversationCollectionViewContentOffsetBeforeKeyboardWillHide
contentOffset.y -= keyboardHeight
@@ -890,7 +908,7 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if isKeyboardVisible {
view.endEditing(true)
messageToolbar.state = .Default
} else {
let message = messages.objectAtIndex(UInt(displayedMessagesRange.location + indexPath.item)) as! Message
+48 -8
View File
@@ -8,10 +8,24 @@
import UIKit
enum MessageToolbarState {
enum MessageToolbarState: Printable {
case Default
case TextInput
case VoiceRecord
case MoreMessages
var description: String {
switch self {
case .Default:
return "Default"
case .TextInput:
return "TextInput"
case .VoiceRecord:
return "VoiceRecord"
case .MoreMessages:
return "MoreMessages"
}
}
}
@IBDesignable
@@ -21,10 +35,21 @@ class MessageToolbar: UIToolbar {
let messageTextAttributes = [NSFontAttributeName: UIFont.systemFontOfSize(15)]
var transitionToStateDefaultAction: ((previousState: MessageToolbarState) -> Void)?
var transitionToStateMoreMessagesAction: ((previousState: MessageToolbarState) -> Void)?
var previousState: MessageToolbarState = .Default
var state: MessageToolbarState = .Default {
willSet {
previousState = state
switch newValue {
case .Default:
if let action = transitionToStateDefaultAction {
action(previousState: previousState)
}
moreButton.hidden = false
sendButton.hidden = true
@@ -54,16 +79,29 @@ class MessageToolbar: UIToolbar {
moreButton.tintColor = UIColor.messageToolBarNormalColor()
showVoiceButtonAnimation()
case .MoreMessages:
if let action = transitionToStateMoreMessagesAction {
action(previousState: previousState)
}
}
updateHeightOfMessageTextView()
}
didSet {
switch oldValue {
case .Default, .MoreMessages:
messageTextView.resignFirstResponder()
default:
break
}
}
}
var textSendAction: ((messageToolBar: MessageToolbar) -> ())?
var imageSendAction: ((messageToolBar: MessageToolbar) -> ())?
var toggleMoreMessagesAction: ((messageToolBar: MessageToolbar) -> ())?
var voiceSendBeginAction: ((messageToolBar: MessageToolbar) -> ())?
@@ -125,7 +163,7 @@ class MessageToolbar: UIToolbar {
let button = UIButton()
button.setImage(UIImage(named: "item_more"), forState: .Normal)
button.tintColor = UIColor.messageToolBarHighlightColor()
button.addTarget(self, action: "trySendImageMessage", forControlEvents: UIControlEvents.TouchUpInside)
button.addTarget(self, action: "toggleMoreMessages", forControlEvents: UIControlEvents.TouchUpInside)
return button
}()
@@ -277,12 +315,14 @@ class MessageToolbar: UIToolbar {
}
}
func trySendImageMessage() {
if let imageSendAction = imageSendAction {
imageSendAction(messageToolBar: self)
func toggleMoreMessages() {
if state != .MoreMessages {
state = .MoreMessages
} else {
state = .Default
}
}
func trySendVoiceMessageBegin() {
if let textSendAction = voiceSendBeginAction {
voiceRecordButton.backgroundColor = UIColor.lightGrayColor()