【发布时间】:2020-07-14 14:23:16
【问题描述】:
我正在尝试创建一个自定义视图类,它是一个带有 UICollectionView 的弹出窗口。我已经创建了动画进出视图的函数,但实际上只有动画出函数起作用。
我正在设置约束如下...
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit(){
...
self.addSubview(collectionView)
collectionView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
collectionView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.7).isActive = true
...
self.animateIn()
}
然后来处理我正在使用以下的动画...
func animateIn(){
self.collectionView.transform = CGAffineTransform(translationX: 0, y: self.collectionView.frame.height)
self.blurredBackgroundView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
self.blurredBackgroundView.alpha = 1
self.collectionView.transform = .identity
})
}
并且 animateOut 函数(有效)是......
func animateOut(){
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
self.collectionView.transform = CGAffineTransform(translationX: 0, y: self.collectionView.frame.height)
self.blurredBackgroundView.alpha = 0
}) { (complete) in
if complete {
self.removeFromSuperview()
}
}
}
但最初的 CGAfineTransform 不起作用,因此视图在第一次出现时出现在其最终位置。我很困惑,因为它非常适合在按下按钮关闭弹出窗口时调用的 animateOut 函数。
谁能告诉我我做错了什么?我觉得我必须缺少一些基本的东西。
谢谢
【问题讨论】:
-
您发布了 animateIn 函数,但没有发布 animateOut 函数。您也没有显示 animateOut 被调用的位置。
-
你在哪里打电话给
commonInit? -
对不起...我已经用这些细节更新了问题。
标签: swift xcode uiview cgaffinetransform