【问题标题】:Swift -Position new UIWIndow in center of ScreenSwift - 将新的 UIWINdow 定位在屏幕中心
【发布时间】:2020-05-22 09:59:04
【问题描述】:

用户点击一个单元格,我得到了 touchLocation,创建了一个新的 UIWindow,然后将其动画到屏幕的中心。问题是当新窗口(紫色)出现在屏幕上而不是其中心位于屏幕中心时,它的 x,y 坐标从屏幕中心开始。

我明白了:

但我想要这个:

// the collectionView is dimmed black, that's why the background color is dark
guard let keyWindow = UIApplication.shared.keyWindow else { return }

let startingRect = CGRect(x: userTouchPointX, y: userTouchPointY, width: 10, height: 10)

let newWindow = UIWindow(frame: startingRect)
newWindow.backgroundColor = .clear
newWindow.windowLevel = UIWindow.Level.normal
newWindow.rootViewController = UINavigationController(rootViewController: PurpleVC())
newWindow.isHidden = false
newWindow.makeKey()

let endingCenterX = keyWindow.screen.bounds.midX // I also tried keyWindow.frame.width / 2 and also UIScreen.main.bounds.width / 2
let endingCenterY = keyWindow.screen.bounds.midY // I also tried keyWindow.frame.height / 2 and also UIScreen.main.bounds.height / 2
let endingWidth = keyWindow.frame.width
let endingHeight: CGFloat = 200

let endingCenter = CGPoint(x: endingCenterX, y: endingCenterY)

UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [.curveEaseIn], animations: {

    newWindow.center = endingCenter
    newWindow.frame.size.width = endingWidth
    newWindow.frame.size.height = endingHeight
}) { (_) in

    newWindow.layoutIfNeeded()
}

【问题讨论】:

    标签: ios swift cgrect uiwindow cgpoint


    【解决方案1】:

    更改您的中心设置窗口的框架后将解决您的问题......希望它会有所帮助

    // newWindow.center = endingCenter remove from here
        newWindow.frame.size.width = endingWidth
        newWindow.frame.size.height = endingHeight
        newWindow.center = endingCenter // add it here
    

    【讨论】:

    • 它不是完成处理程序先生
    • 在 UIView.animate 中你只需要更新layoutIfNeeded()
    • 什么是关键窗口中心?
    • 完成时不要调用 layoutifneeded
    • 设置框架后呼叫中心会解决问题
    猜你喜欢
    • 2012-04-09
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多