用户点击一个单元格,我得到触摸位置,创建一个新的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()
}
改变你的中心后,设置窗口的框架将解决你的问题......希望它能帮助你。
// newWindow.center = endingCenter remove from here
newWindow.frame.size.width = endingWidth
newWindow.frame.size.height = endingHeight
newWindow.center = endingCenter // add it here