【问题标题】:Custom Segue not working if NOT set as initial window如果未设置为初始窗口,自定义 Segue 将不起作用
【发布时间】:2015-08-04 19:24:38
【问题描述】:

以下是 Swift 中自定义 segue 动画的代码摘录,仅当我将源 viewController 设置为情节提要中的初始窗口时,它才能正常工作。

class CustomSegue: UIStoryboardSegue
{
    override func perform()
    {
        var fromView = self.sourceViewController.view as UIView!
        var toView = self.destinationViewController.view as UIView!

        let offScreenHorizontalStart = CGAffineTransformMakeRotation(CGFloat(M_PI / 2))
        let offScreenHorizontalEnd = CGAffineTransformMakeRotation(CGFloat(-M_PI / 2))

        fromView.layer.anchorPoint = CGPoint(x:0, y:0)
        fromView.layer.position = CGPoint(x:0, y:0)

        UIApplication.sharedApplication().keyWindow?.insertSubview(toView, belowSubview: fromView)

        UIView.animateWithDuration(0.7, delay: 0.0, usingSpringWithDamping: 1.6, initialSpringVelocity: 0.0, options: nil, animations:
            {
                fromView.transform = offScreenHorizontalEnd
            }, completion: {
                finished in
                self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController, animated: false, completion: nil)
        })
    }
}

通过这个 segue 动画,源视图预计会随着从 x/y = 0/0 的向上旋转而消失。

谢谢。

【问题讨论】:

    标签: ios objective-c iphone swift


    【解决方案1】:

    只要您在此处明确使用 UIApplication 对象的 keyWindow 属性:

    UIApplication.sharedApplication().keyWindow?.insertSubview(toView, belowSubview: fromView)
    

    我认为它不适用于其他窗口也就不足为奇了。

    您可以改用UIApplicationwindows 属性。它以有序的NSArray 保存当前在应用程序中打开的所有UIWindow 对象(数组中的最后一个对象是最顶部的窗口)。 或者您从fromView.window 属性中选择当前窗口?

    【讨论】:

    • 这似乎很有帮助。谢谢!我还看到以下代码对我有用! var tmpView = firstVCView.superview tmpView?.insertSubview(secondVCView, belowSubview: firstVCView)
    猜你喜欢
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多