如何在覆盖 UIView 之前停止幻灯片过渡到 Second VC? [英] How to stop slide transition into Second VC before it covers UIView?

查看:35
本文介绍了如何在覆盖 UIView 之前停止幻灯片过渡到 Second VC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我在屏幕顶部有一个 uiview.当我点击那个 UIView 时,我有第二个视图控制器向上滑动.我想要完成的是,当第二个视图控制器滑入屏幕时,它会在覆盖 UIView 之前停止

我想要完成的是有一个视图向上滑动,其中包含商店信息,例如营业时间、地址、电话号码等.所以我想我可以有另一个视图控制器来保存所有这些信息.唯一的部分是我想阻止它向上滑动,使其与顶部的 uiview 栏齐平.

//点击调出第二个视图控制器@IBAction func showInfoVC(sender: AnyObject) {self.performSegueWithIdentifier("showSecondVC", 发件人:self)}

解决方案

听起来您的目标是:

  1. 有一个基本视图
  2. 在第一个视图上滑动第二个视图

假设是这种情况,您可以通过多种方式实现此目的,但 Apple 可能会推荐

*注意:此解决方案是在没有 Interface Builder 的情况下以编程方式实现的.我个人以这种方式完全用代码构建我的所有应用程序.如果您想使用 Interface Builder,您可以使用 Storyboard 和自定义 segue 来完成同样的事情.这是一个相关的教程:iOS 8 中动画自定义 Segue 的初学者指南

In my application I have a uiview at the top of the screen. When I tap that UIView I have a second View Controller slides up. What I would like to accomplish is that when the second View Controller slides into the screen it stops right before it covers up the UIView

What I am trying to accomplish is have a view slide up that contains store information such as hours open, address, phone number etc. So I was thinking that I could just have another view controller that holds all of this information. Only part is I want to stop it sliding up so it is flush with the uiview bar on top.

//tap to bring up the second view controller
@IBAction func showInfoVC(sender: AnyObject) {
    self.performSegueWithIdentifier("showSecondVC", sender: self)
}

解决方案

It sounds like your goals are:

  1. Have a base View
  2. Slide a second View part way up on the first

Assuming this is the case, there are multiple ways you could accomplish this, but Apple would probably recommend View Controller Containment. To accomplish this, you will have:

  1. A SlidingContainerViewController.
    • This is a custom container View Controller that will hold our other two View Controllers
  2. Some background View Controller
  3. Some foreground View Controller

Here is a basic implementation of a custom SlidingContainerViewController

// SlidingContainerViewController.swift

import UIKit

class SlidingContainerViewController: UIViewController {

    //MARK: - Init
    init(frontViewController: UIViewController, backViewController: UIViewController) {
        super.init(nibName: nil, bundle: nil)
        frontViewContoller = frontViewController
        backViewContoller = backViewController
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: - Public
    var frontViewContoller: UIViewController = UIViewController()
    var backViewContoller: UIViewController = UIViewController()
    var splitOriginY: CGFloat = 160.0

    func toggleFrontView() {
        if frontIsVisible {
            UIView.animateWithDuration(0.4) {
                self.frontViewContoller.view.frame.origin.y = self.view.frame.height
                self.frontIsVisible = false
            }
        } else {
            UIView.animateWithDuration(0.4) {
                self.frontViewContoller.view.frame.origin.y = self.splitOriginY
                self.frontIsVisible = true
            }

        }
    }

    //MARK: - ViewLifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        addChild(backViewContoller)
        addChild(frontViewContoller)
        self.frontViewContoller.view.frame.origin.y = self.view.frame.height
    }

    //MARK: - Private
    var frontIsVisible = false

    private func addChild(viewController: UIViewController) {
        addChildViewController(viewController)
        view.addSubview(viewController.view)
        viewController.view.frame = view.bounds
        viewController.didMoveToParentViewController(self)
    }

}

You can then put any custom View Controllers that you want into this container View Controller. The bottom View Controller just needs to call toggleFrontView() on the container View Controller whenever it wants the slide to occur.

Below I've added two sample View Controllers for demonstrations purposes. You can view the whole project on github: SlidingVC

*Note: This solution is implemented programmatically without Interface Builder. I personally build all of my apps completely in code this way. If you desired to use Interface Builder, you could accomplish the same thing using a Storyboard and custom segues. Here is a related tutorial: A Beginner’s Guide to Animated Custom Segues in iOS 8

这篇关于如何在覆盖 UIView 之前停止幻灯片过渡到 Second VC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆