2016-04-21 104 views
0

我的应用程序包含一个按钮,它可以在您弹出“收藏夹”视图时隐藏该视图,并在您触摸外部视图时隐藏该视图。动画效果很好,直到touchesbegan功能被调用,然后它才会淡入。在swift中运行addsubview命令后不会运行动画

我的触摸开始功能包括addsubview命令,并以某种方式似乎导致问题。
为什么在读取addsubview命令后,我的“spring in”动画无法工作?这是我的约束重置问题吗?每次都会调用我的动画块。

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 
    }) 
} 

回答

0

我的问题与约束有关。我用这个问题的答案How do I animate a NSLayoutConstraint in Swift?来动画约束变化而不是动作。它现在工作完美无瑕。

self.view.bringSubviewToFront(self.collectionView) 
    constraint.constant = constraint.constant + 300 
    UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: {() -> Void in 
     self.view.layoutIfNeeded() 
    }, completion: nil)