Ios UISegue定位UIView

Ios UISegue定位UIView,ios,swift,uiview,segue,Ios,Swift,Uiview,Segue,我试图使用一个自定义转换来定位UIView,这样它就可以从底部向上滑动,但顶部会在屏幕的一半位置停止。我已经开始工作了,但在中途短暂停止后,ui视图将打开剩下的部分并占据整个屏幕。这就是我做这件事的代码 override func perform() { //Coming in here, we already have this information. The segue knows the source //and destination view controllers

我试图使用一个自定义转换来定位
UIView
,这样它就可以从底部向上滑动,但顶部会在屏幕的一半位置停止。我已经开始工作了,但在中途短暂停止后,
ui视图将打开剩下的部分并占据整个屏幕。这就是我做这件事的代码

override func perform() {
    //Coming in here, we already have this information.  The segue knows the source
    //and destination view controllers.
    var firstView = self.sourceViewController.view as UIView!
    var secondView = self.destinationViewController.view as UIView!

    //Get the screen width and height, for calculations.
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    //Specify the initial position of the second view.  We're sliding from
    //bottom to top, so put it off the bottom of the screen.
    secondView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)

    //Insert the second view above the first view. The windows are all in a stack.  Doing
    //this inserts the second view at the top of the statck.  It is not yet visible.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondView, aboveSubview: firstView)

    //Animate the transition.
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        secondView.frame = CGRectOffset(secondView.frame, 0.0, -screenHeight/2)

        }) { (Finished) -> Void in
            self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
                animated: false,
                completion: nil)
    }
}

我在Finished中设置了一个断点,这就是导致第二个视图占据整个屏幕的代码。如何使第二个视图只占据屏幕的下半部分。

使用“执行”方法来设置从源控制器到目标控制器的转换动画,但最后一步:

self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController,
            animated: false,
            completion: nil)
显示目标控制器,据iOS所知,它是一个全屏控制器,这就是为什么您会在末尾看到跳转

您有两个选择:

1。假装(选项A):

  • 在故事板中选择您的片段,并将演示选项从“默认”(默认值为“全屏”)更改为“当前上下文”
  • 在目标控制器中,将根视图背景色设置为“清除”
  • 添加一个占据视图一半高度的新视图,并将所有人转移到该视图中
这只适用于非常简单的布局,如果您

2。假装(选项B)

不要使用视图控制器-仅使用从屏幕底部设置动画的视图

如果您使用的是Xcode 7,您可以将UIView放到场景顶部的栏中,当您点击它时,Xcode会将其展开,以便您可以添加子视图等。如果您的目标视图是一个简单的瞬态视图(如菜单),并且您已经准备好使用Xcode 7,这可能是一个不错的选择

3。使用UIPresentationController(推荐)

切换到使用UIPresentationController,而不是使用自定义segue。这可能涉及到很多锅炉板,但实际上非常简单

下面是一个带有演示控制器的示例,其简单程度如下:

class HalfScreenPresentation: UIPresentationController {

    override func frameOfPresentedViewInContainerView() -> CGRect {

        let containerFrame = self.containerView!.frame

        return CGRect(x: 0, y: containerFrame.height/2, width: containerFrame.width, height: containerFrame.height/2)

    }
}
这是因为如果要更改此动画(例如,从顶部或侧面滑动,或其他一些过渡),动画的控制器将从屏幕底部显示/取消(这是默认的过渡!)您还需要提供一个满足
UIViewControllerAnimatedTransitioning
要求的对象(在大多数情况下,这与在perform方法中复制和粘贴代码一样简单)


我真的很喜欢
UIPresentationController
API,希望你也喜欢

对于那些希望演示从左边开始,或者希望y位置变化的人,我发现了一个简单的解决方案:

class HalfScreenPresentation: UIPresentationController {

override var frameOfPresentedViewInContainerView : CGRect {

    let containerFrame = self.containerView!.frame

    return CGRect(x: 0, y: 0, width: containerFrame.width/1.5, height: containerFrame.height) //From the left
    return CGRect(x: containerFrame.width/3, y: 0, width: containerFrame.width/1.5, height: containerFrame.height)/from the right
}

谢谢,这个示例项目解决了我的大部分需求——只是想找到一种滑动俯视图的方法。我认为手势识别器可以完成这项工作。但是这个示例项目非常棒,我唯一突出的需要是从左到右“推”源屏幕,而不是覆盖其中的一部分。有什么建议吗?