【发布时间】: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