我的应用程序包含一个按钮,该按钮可以为“弹出” Collection View 设置动画,并在您触摸其外部时隐藏该 View 。动画效果很好,直到调用touchesbegan函数,然后它才会淡入。
我的触摸开始功能包括 addsubview 命令,这似乎是导致问题的原因。
为什么在读取 addsubview 命令后我的“spring in”动画不起作用?这是我重置约束的问题吗?我的动画 block 每次都会被调用。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let bgImage = UIImageView(image: processedImage)
self.viewArray.append(bgImage)
}
func showFilter(){
view.bringSubviewToFront(collectionView)
var collectionViewPos = collectionView.center.x
self.collectionView.center.x = view.frame.width + 300
UIView.animateWithDuration(0.0, delay: 0.0, options: [], animations: { () -> Void in
self.collectionView.alpha = 1
}, completion: { (complete: Bool) in
UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
self.collectionView.center.x = collectionViewPos
self.view.layoutIfNeeded()
}, completion: nil)
return
})
}
最佳答案
我的问题与限制有关。我使用了这个问题的答案How do I animate a NSLayoutConstraint in Swift?对约束变化而不是运动进行动画处理。现在可以完美运行了。
self.view.bringSubviewToFront(self.collectionView)
constraint.constant = constraint.constant + 300
UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
self.view.layoutIfNeeded()
}, completion: nil)
关于ios - swift 中的 addsubview 命令后动画未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36760247/