自定义展开segue行为

时间:2016-10-12 16:55:55

标签: ios swift xcode unwind-segue

我是xcode和swift的新手,正在对现有应用进行更改。在我的主视图控制器之外,我使用自定义segue来展开以实现从左侧向后滑出的菜单页面视图。我的两个菜单项,在触摸时,会将您带回主视图控制器而不是堆栈中的视图,并嵌入新屏幕或显示新信息。当我使用unwind segue时,它会快速闪烁它应该快速的内容,然后转到我的主控制器的默认值,我想知道如何解决这个问题。我自定义segue的代码:

class MHLeftSlideSegue: UIStoryboardSegue {

    override func perform() {

        let src = self.sourceViewController
        let dst = self.destinationViewController

        src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
        dst.view.transform = CGAffineTransformMakeTranslation(-src.view.frame.size.width, 0)

        UIView.animateWithDuration(0.25, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            dst.view.transform = CGAffineTransformMakeTranslation(0, 0)
        }, completion: { finished in
            src.presentViewController(dst, animated: false, completion: nil)
        })
    }
}

class UIStoryboardUnwindLeftSlideSegue: UIStoryboardSegue {

    override func perform() {

        let src = self.sourceViewController as UIViewController
        let dst = self.destinationViewController as UIViewController

        src.view.superview?.insertSubview(dst.view, belowSubview: src.view)
        src.view.transform = CGAffineTransformMakeTranslation(0, 0)

        UIView.animateWithDuration(0.25, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            src.view.transform = CGAffineTransformMakeTranslation(-src.view.frame.size.width, 0)
        }, completion: { finished in
            src.dismissViewControllerAnimated(false, completion: nil)
        })
    }
}

我正在使用第一部分从我的主VC中移动一个移动菜单按钮到我的菜单VC,并且展开将通过关闭按钮返回到我的主VC。我遇到的问题涉及"搜索"和#34;热门搜索"链接回MainVC。搜索信息在主VC中显示此代码:

if MHMainVC.fromSearchMenu == true {
    MHMainVC.fromSearchMenu = false
    setSearchState(!isSearching)
}

@IBAction func onSearch(sender: AnyObject!) {
    setSearchState(!isSearching)
}

func setSearchState(search: Bool) -> Void {
    isSearching = search

    if isSearching == false {
        self.txtSearch.text = nil
        self.txtSearch.resignFirstResponder()
        self.txtSearch.hidden = true
        self.btnSearch.setImage(UIImage(named: "btnSearch"), forState: UIControlState.Normal)
        self.constraintLeftSpaceOfSearchButton.constant = 56
        self.btnCalendar.hidden = false
        self.tblSearchResult.hidden = true
    } else {
        self.txtSearch.becomeFirstResponder()
        self.txtSearch.hidden = false
        self.btnSearch.setImage(UIImage(named: "btnClose"), forState: UIControlState.Normal)
        self.constraintLeftSpaceOfSearchButton.constant = 8
        self.btnCalendar.hidden = true
        self.tblSearchResult.hidden = false
    }

    updateConstraint(true)
}

和#34;热门搜索"代码来自MHTopSearchesVC转到嵌入主VC的Container VC,代码是

class MHContainerVC: UIViewController {

    var currentSegueIdentifier  : String?
    var firstViewController     : MHSplashFeaturesVC?
    var secondViewController    : MHTopSearchesVC?
    var thirdViewController     : ViewController?
    var transitionInProgress    : Bool?

    static var SegueNameKey: String!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let name = MHContainerVC.SegueNameKey {
            self.currentSegueIdentifier = name
            performSegueWithIdentifier(name, sender: nil)

            MHContainerVC.SegueNameKey = "embedSplashFeaturesVC"
        } else {
            self.currentSegueIdentifier = "embedSplashFeaturesVC"
            performSegueWithIdentifier("embedSplashFeaturesVC", sender: nil)
        }

        self.transitionInProgress = false
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "embedSplashFeaturesVC" {
            self.firstViewController = segue.destinationViewController as? MHSplashFeaturesVC
        }

        if segue.identifier == "embedTopSearchesVC" {
            self.secondViewController = segue.destinationViewController as? MHTopSearchesVC
        }

        if segue.identifier == "embedBecomeAHelper" {
            self.thirdViewController = segue.destinationViewController as? ViewController
        }

        if segue.identifier == "embedSplashFeaturesVC" {
            if self.childViewControllers.count > 0 {
                self.swapFromViewController(self.childViewControllers[0], toViewController: self.firstViewController!)
            } else {
                self.addChildViewController(segue.destinationViewController)

                let destView: UIView = (segue.destinationViewController).view
                destView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
                self.view.addSubview(destView)

                segue.destinationViewController.didMoveToParentViewController(self)
            }
        } else if segue.identifier == "embedTopSearchesVC" {

            if self.childViewControllers.count > 0 {
                self.swapFromViewController(self.childViewControllers[0] , toViewController: self.secondViewController!)
            } else {
                self.addChildViewController(segue.destinationViewController)

                let destView: UIView = (segue.destinationViewController).view
                destView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
                self.view.addSubview(destView)

                segue.destinationViewController.didMoveToParentViewController(self)

            }

        } else if segue.identifier == "embedBecomeAHelper"{
            if self.childViewControllers.count > 0 {
                self.swapFromViewController(self.childViewControllers[0], toViewController: self.thirdViewController!)
            } else {
                self.addChildViewController(segue.destinationViewController)

                let destView: UIView = (segue.destinationViewController).view
                destView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
                self.view.addSubview(destView)

                segue.destinationViewController.didMoveToParentViewController(self)

            }
        }
    }

    func swapFromViewController(fromViewController: UIViewController, toViewController: UIViewController) -> Void {
        toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
        fromViewController.willMoveToParentViewController(nil)

        self.addChildViewController(toViewController)

        self.transitionFromViewController(fromViewController, toViewController: toViewController, duration: 0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: nil, completion: { finished in  fromViewController.removeFromParentViewController()
            toViewController.didMoveToParentViewController(self)
            self.transitionInProgress = false
        })
    }
}

0 个答案:

没有答案