使用自定义转换管理器展开时出现问题

时间:2015-03-05 22:51:20

标签: swift ios8

我构建了一个自定义转换管理器来处理在我的应用中空间移动视图。

我有三个视图控制器:MainViewControllerPastSessionsViewControllerJournalViewController

我的问题是:当用户从主要会话过去到过去会话并按下"完成"在Journal中返回Main,视图会在Main从右侧放大之前立即跳转到Past Sessions。

预期的行为是Main从屏幕左侧推入并将Journal推出屏幕右侧。

我在animationControllerForDismissedController添加了一个换行符,并注意到dismissed.descriptionPastSessionsViewController时,在按下" Done&后,从Journal移至Main时应为JournalViewController #34;

我不确定为什么会这样。我有正确连接的展开segues,prepareForSegue JournalViewController中的代码是有道理的。我不相信我应该定义解雇视图控制器,这应该像我在其他每个过渡中一样处理。

如果有人对如何解决此问题有任何想法,以便过去会话在转换之前不会跳回到用户的视图中,那将非常感激。

谢谢!

TransitionManager.swift:

import UIKit

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning , UIViewControllerTransitioningDelegate {
    var presenting = true
    var direction = "down" //from the top. accepts up/down/left/right

    // transition between pressing "Begin" and the water beginning to fill
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 0.8
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let container = transitionContext.containerView()
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

        var offScreenTop: CGAffineTransform
        var offScreenBottom: CGAffineTransform

        switch self.direction {
        case "up" :
            offScreenTop = CGAffineTransformMakeTranslation(0, container.frame.height)
            offScreenBottom = CGAffineTransformMakeTranslation(0, -container.frame.height)

            toView.transform = self.presenting ? offScreenTop : offScreenBottom

            container.addSubview(fromView)
            container.addSubview(toView)
        case "left" :
            // TODO: rename top/bottom/l/r
            offScreenTop = CGAffineTransformMakeTranslation(-container.frame.width, 0)
            offScreenBottom = CGAffineTransformMakeTranslation(container.frame.width, 0)

            toView.transform = self.presenting ? offScreenTop : offScreenBottom

            container.addSubview(fromView)
            container.addSubview(toView)
        case "right" :
            offScreenTop = CGAffineTransformMakeTranslation(container.frame.width, 0)
            offScreenBottom = CGAffineTransformMakeTranslation(-container.frame.width, 0)

            toView.transform = self.presenting ? offScreenTop : offScreenBottom

            container.addSubview(fromView)
            container.addSubview(toView)
        default:
            // down
            offScreenTop = CGAffineTransformMakeTranslation(0, -container.frame.height)
            offScreenBottom = CGAffineTransformMakeTranslation(0, container.frame.height)

            toView.transform = self.presenting ? offScreenTop : offScreenBottom

            container.addSubview(toView)
            container.addSubview(fromView)
        }

        let duration = self.transitionDuration(transitionContext)

        UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
                fromView.transform = self.presenting ? offScreenBottom : offScreenTop
                toView.transform = CGAffineTransformIdentity
            }, completion: {
                finished in transitionContext.completeTransition(true)
        })
    }

    // return the animator when presenting a viewcontroller
    // rememeber that an animator (or animation controller) is any object that adheres to the UIViewControllerAnimatedTransitioning protocol
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.presenting = true

        return self
    }

    // return the animator used when dismissing from a viewcontroller
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        self.presenting = false

        return self
    }
}

JournalViewController:

import UIKit
import CoreData

class JournalViewController: UIViewController, UITextViewDelegate {    
    let transitionManager = TransitionManager()

    var journalEntryCoreDataLocation: Int?
    var journalEntryToEdit: String?
    var journalEntryToEditTimestamp: NSDate?

    @IBOutlet weak var journalEntryLabel: UILabel!
    @IBOutlet weak var journalEntryTextArea: UITextView!

    @IBAction func doneJournalEntry(sender: AnyObject) {
        journalEntryTextArea.resignFirstResponder()

        // do some stuff in coredata
    }

    @IBAction func cancelButtonPressed(sender: AnyObject) {
        journalEntryTextArea.resignFirstResponder()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(animated: Bool) {
    }

    override func viewWillAppear(animated: Bool) {
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        let destinationVC = segue.destinationViewController as ViewController
        destinationVC.showJournalButton = false

        let transitionManager = self.transitionManager
        transitionManager.presenting = true
        transitionManager.direction = "left"
//        transitionManager.animationControllerForDismissedController(JournalViewController)

        destinationVC.transitioningDelegate = transitionManager
    }
}

0 个答案:

没有答案