From ec99ea745655fece224a570a3cbdaef8968e3417 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Wed, 6 May 2015 14:35:02 +0800 Subject: [PATCH] add ConversationMessagePreviewTransitionManager --- Yep.xcodeproj/project.pbxproj | 4 + ...ationMessagePreviewTransitionManager.swift | 100 ++++++++++++++++++ .../ConversationViewController.swift | 19 +++- 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 Yep/ViewControllers/Conversation/ConversationMessagePreviewTransitionManager.swift diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index 1cb2f939..c24b58be 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -103,6 +103,7 @@ 506E241E1AE6391A00A33C1A /* TouchZoomButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 506E241D1AE6391A00A33C1A /* TouchZoomButton.swift */; }; 506EC20A1AC95BFC00CE42B3 /* UIFont+Yep.swift in Sources */ = {isa = PBXBuildFile; fileRef = 506EC2091AC95BFC00CE42B3 /* UIFont+Yep.swift */; }; 5076C56A1AC46B9F00B22952 /* FayeService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5076C5691AC46B9F00B22952 /* FayeService.swift */; }; + 5076FC5A1AF9DF6B00D7381A /* ConversationMessagePreviewTransitionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5076FC591AF9DF6B00D7381A /* ConversationMessagePreviewTransitionManager.swift */; }; 50894CF11AEA4726000981AF /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50894CF01AEA4726000981AF /* SettingsViewController.swift */; }; 50894CF51AEA4A01000981AF /* SettingsUserCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50894CF31AEA4A01000981AF /* SettingsUserCell.swift */; }; 50894CF61AEA4A01000981AF /* SettingsUserCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50894CF41AEA4A01000981AF /* SettingsUserCell.xib */; }; @@ -273,6 +274,7 @@ 506E241D1AE6391A00A33C1A /* TouchZoomButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TouchZoomButton.swift; path = Views/Buttons/TouchZoomButton.swift; sourceTree = ""; }; 506EC2091AC95BFC00CE42B3 /* UIFont+Yep.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIFont+Yep.swift"; path = "Extensions/UIFont+Yep.swift"; sourceTree = ""; }; 5076C5691AC46B9F00B22952 /* FayeService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FayeService.swift; path = Services/FayeService.swift; sourceTree = ""; }; + 5076FC591AF9DF6B00D7381A /* ConversationMessagePreviewTransitionManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ConversationMessagePreviewTransitionManager.swift; path = ViewControllers/Conversation/ConversationMessagePreviewTransitionManager.swift; sourceTree = ""; }; 50894CF01AEA4726000981AF /* SettingsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SettingsViewController.swift; path = ViewControllers/Settings/SettingsViewController.swift; sourceTree = ""; }; 50894CF31AEA4A01000981AF /* SettingsUserCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SettingsUserCell.swift; path = Views/Cells/SettingsUser/SettingsUserCell.swift; sourceTree = ""; }; 50894CF41AEA4A01000981AF /* SettingsUserCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = SettingsUserCell.xib; path = Views/Cells/SettingsUser/SettingsUserCell.xib; sourceTree = ""; }; @@ -769,6 +771,7 @@ children = ( 503CAB421AC00CB100DFE830 /* ConversationViewController.swift */, 50165C441AC2860900C7AEBE /* ConversationLayout.swift */, + 5076FC591AF9DF6B00D7381A /* ConversationMessagePreviewTransitionManager.swift */, ); name = Conversation; sourceTree = ""; @@ -1362,6 +1365,7 @@ 506EC20A1AC95BFC00CE42B3 /* UIFont+Yep.swift in Sources */, 0A0A11821AE389380008772C /* RegisterSkillsLayout.swift in Sources */, 50C0909B1ADE234F00CC6389 /* RegisterPickSkillsViewController.swift in Sources */, + 5076FC5A1AF9DF6B00D7381A /* ConversationMessagePreviewTransitionManager.swift in Sources */, 508D16BD1ADCAF0400270E3F /* PullToRefreshView.swift in Sources */, 508D17441ACCDC9F0092D666 /* VoiceRecordButton.swift in Sources */, 502AE5271AB72D06005BD199 /* YepNetworking.swift in Sources */, diff --git a/Yep/ViewControllers/Conversation/ConversationMessagePreviewTransitionManager.swift b/Yep/ViewControllers/Conversation/ConversationMessagePreviewTransitionManager.swift new file mode 100644 index 00000000..343a73d1 --- /dev/null +++ b/Yep/ViewControllers/Conversation/ConversationMessagePreviewTransitionManager.swift @@ -0,0 +1,100 @@ +// +// ConversationMessagePreviewTransitionManager.swift +// Yep +// +// Created by NIX on 15/5/6. +// Copyright (c) 2015年 Catch Inc. All rights reserved. +// + +import UIKit + +class ConversationMessagePreviewTransitionManager: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning { + + var frame = CGRectZero + + + var isPresentation = false + + // MARK: UIViewControllerTransitioningDelegate + + func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? { + isPresentation = true + + return self + } + + func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { + isPresentation = false + + return self + } + + // MARK: UIViewControllerAnimatedTransitioning + + func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval { + return 2.6 + } + + func animateTransition(transitionContext: UIViewControllerContextTransitioning) { + + if isPresentation { + presentTransition(transitionContext) + } else { + dismissTransition(transitionContext) + } + } + + func presentTransition(transitionContext: UIViewControllerContextTransitioning) { + let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) + let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) + + let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) + let toView = transitionContext.viewForKey(UITransitionContextToViewKey) + + let containerView = transitionContext.containerView() + + containerView.addSubview(toView!) + + let animatingVC = toVC! + let animatingView = toView! + + let finalFrame = transitionContext.finalFrameForViewController(animatingVC) + + animatingView.frame = frame + + UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, options: .AllowUserInteraction | .BeginFromCurrentState, animations: { () -> Void in + animatingView.frame = finalFrame + + }, completion: { (finished) -> Void in + transitionContext.completeTransition(true) + }) + } + + func dismissTransition(transitionContext: UIViewControllerContextTransitioning) { + //let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) + //let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) + + let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) + //let toView = transitionContext.viewForKey(UITransitionContextToViewKey) + + //let containerView = transitionContext.containerView() + + //containerView.addSubview(toView!) + + //let animatingVC = fromVC! + let animatingView = fromView! + + //let finalFrame = transitionContext.finalFrameForViewController(animatingVC) + + //animatingView.frame = frame + + UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, options: .AllowUserInteraction | .BeginFromCurrentState, animations: { () -> Void in + animatingView.frame = self.frame + + }, completion: { (finished) -> Void in + animatingView.removeFromSuperview() + transitionContext.completeTransition(true) + }) + } + +} \ No newline at end of file diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 3200f1ae..4bd8f2d0 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -39,6 +39,8 @@ class ConversationViewController: UIViewController { return dateFormatter }() + var messagePreviewTransitionManager: ConversationMessagePreviewTransitionManager? + var conversationCollectionViewHasBeenMovedToBottomOnce = false @@ -900,7 +902,22 @@ class ConversationViewController: UIViewController { override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "presentMessageMedia" { let vc = segue.destinationViewController as! MessageMediaViewController - vc.message = sender as? Message + if let message = sender as? Message { + vc.message = message + + let indexPath = NSIndexPath(forRow: Int(messages.indexOfObject(message)) - displayedMessagesRange.location , inSection: 0) + + if let cell = conversationCollectionView.cellForItemAtIndexPath(indexPath) { + let frame = conversationCollectionView.convertRect(cell.frame, toView: view) + vc.modalPresentationStyle = UIModalPresentationStyle.Custom + + let transitionManager = ConversationMessagePreviewTransitionManager() + transitionManager.frame = frame + vc.transitioningDelegate = transitionManager + + messagePreviewTransitionManager = transitionManager + } + } } else if segue.identifier == "presentPickLocation" { let nvc = segue.destinationViewController as! UINavigationController