add audioBubbleTapAction to ChatLeftAudioCell & ChatRightAudioCell for only tap on Audio Bubble to play or pause

This commit is contained in:
nixzhu
2015-05-27 16:37:29 +08:00
parent 9f3004ed80
commit de372199ce
3 changed files with 83 additions and 4 deletions
@@ -907,6 +907,49 @@ class ConversationViewController: BaseViewController {
}
}
func playMessageAudioWithMessage(message: Message) {
if let audioPlayer = YepAudioService.sharedManager.audioPlayer {
if let playingMessage = YepAudioService.sharedManager.playingMessage {
if audioPlayer.playing {
audioPlayer.pause()
if let playbackTimer = YepAudioService.sharedManager.playbackTimer {
playbackTimer.invalidate()
}
if let sender = playingMessage.fromFriend, playingMessageIndex = messages.indexOf(playingMessage) {
let indexPath = NSIndexPath(forItem: playingMessageIndex - displayedMessagesRange.location, inSection: 0)
if sender.friendState != UserFriendState.Me.rawValue {
if let cell = conversationCollectionView.cellForItemAtIndexPath(indexPath) as? ChatLeftAudioCell {
cell.playing = false
}
} else {
if let cell = conversationCollectionView.cellForItemAtIndexPath(indexPath) as? ChatRightAudioCell {
cell.playing = false
}
}
}
if message.messageID == playingMessage.messageID {
return
}
}
}
}
let audioPlayedDuration = audioPlayedDurationOfMessage(message) as NSTimeInterval
YepAudioService.sharedManager.playAudioWithMessage(message, beginFromTime: audioPlayedDuration, delegate: self) {
let playbackTimer = NSTimer.scheduledTimerWithTimeInterval(0.02, target: self, selector: "updateAudioPlaybackProgress:", userInfo: nil, repeats: true)
YepAudioService.sharedManager.playbackTimer = playbackTimer
}
}
// MARK: Keyboard
func handleKeyboardWillShowNotification(notification: NSNotification) {
@@ -1247,6 +1290,8 @@ class ConversationViewController: BaseViewController {
}
}
}
}
// MARK: UIGestureRecognizerDelegate
@@ -1336,7 +1381,11 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatLeftAudioCellIdentifier, forIndexPath: indexPath) as! ChatLeftAudioCell
let audioPlayedDuration = audioPlayedDurationOfMessage(message)
cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration)
cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration, audioBubbleTapAction: { message in
self.playMessageAudioWithMessage(message)
})
return cell
@@ -1382,7 +1431,11 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(chatRightAudioCellIdentifier, forIndexPath: indexPath) as! ChatRightAudioCell
let audioPlayedDuration = audioPlayedDurationOfMessage(message)
cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration)
cell.configureWithMessage(message, audioPlayedDuration: audioPlayedDuration, audioBubbleTapAction: { message in
self.playMessageAudioWithMessage(message)
})
return cell
@@ -43,6 +43,9 @@ class ChatLeftAudioCell: UICollectionViewCell {
@IBOutlet weak var playButton: UIButton!
typealias AudioBubbleTapAction = (message: Message) -> Void
var audioBubbleTapAction: AudioBubbleTapAction?
override func awakeFromNib() {
super.awakeFromNib()
@@ -56,9 +59,19 @@ class ChatLeftAudioCell: UICollectionViewCell {
playButton.userInteractionEnabled = false
playButton.tintColor = UIColor.darkGrayColor()
bubbleImageView.userInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: "tapMediaView")
bubbleImageView.addGestureRecognizer(tap)
}
func configureWithMessage(message: Message, audioPlayedDuration: Double) {
func tapMediaView() {
audioBubbleTapAction?(message: message)
}
func configureWithMessage(message: Message, audioPlayedDuration: Double, audioBubbleTapAction: AudioBubbleTapAction?) {
self.audioBubbleTapAction = audioBubbleTapAction
self.message = message
@@ -42,6 +42,9 @@ class ChatRightAudioCell: UICollectionViewCell {
@IBOutlet weak var playButton: UIButton!
typealias AudioBubbleTapAction = (message: Message) -> Void
var audioBubbleTapAction: AudioBubbleTapAction?
override func awakeFromNib() {
super.awakeFromNib()
@@ -54,9 +57,19 @@ class ChatRightAudioCell: UICollectionViewCell {
audioDurationLabel.textColor = UIColor.whiteColor()
playButton.userInteractionEnabled = false
bubbleImageView.userInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: "tapMediaView")
bubbleImageView.addGestureRecognizer(tap)
}
func configureWithMessage(message: Message, audioPlayedDuration: Double) {
func tapMediaView() {
audioBubbleTapAction?(message: message)
}
func configureWithMessage(message: Message, audioPlayedDuration: Double, audioBubbleTapAction: AudioBubbleTapAction?) {
self.audioBubbleTapAction = audioBubbleTapAction
self.message = message