Swift iOS-第二个UIWindow有时会从错误的坐标进行动画处理

时间:2018-07-07 01:47:19

标签: ios swift uiviewanimation uiwindow

我在vc中有一个按钮。当我按下该按钮时,我将从屏幕的右下角启动第二个UIWindow。由于某种原因,它有时会像预期那样从右下角启动,但有时它是从左上角或屏幕中间启动。我尝试在动画中调用window.layoutIfNeeded(),但实际上弄乱了整个布局。

为什么不能始终从屏幕右下角开始?

第二个窗口从屏幕右下角的一个10x10 w / h小帧开始,然后动画到x / y 0,0位置以填充整个屏幕。

@IBAction fileprivate func launchSecondWindowButton(_ sender: UIButton) {

    guard let mainWindow = UIApplication.shared.keyWindow else { return }

    let myVC = MyController()
    let nav = UINavigationController(rootViewController: myVC)
    nav.navigationBar.isHidden = true

    // starts from a small 10x10 w/h frame in the lower right hand corner of the screen
    let startingFrame = CGRect(x: mainWindow.frame.width - 10, y: mainWindow.frame.height - 10, width: 10, height: 10)

    let secondWindow = UIWindow(frame: startingFrame)
    secondWindow.windowLevel = UIWindowLevelStatusBar
    secondWindow.rootViewController = nav
    secondWindow.makeKeyAndVisible()

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

        secondWindow.frame = mainWindow.frame // this is the entire screen
        // secondWindow.layoutIfNeeded() didn't help
    }
}

0 个答案:

没有答案