ios - swift 中的 addsubview 命令后动画未运行

标签 ios swift animation touch

我的应用程序包含一个按钮,该按钮可以为“弹出” 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/

相关文章:

iOS 应用程序未显示在 App Store 中的 "New"类别下

swift - 快速从 NSMutableDictionary 获取值?

ios - 在 Swift 中使用 ClassName.Type 进行初始化

wpf - WPF 中的动画过渡

ios - 如何提高 iOS 汉堡菜单的 CSS 性能?

ios - TwitterKit 偶尔会为现有用户返回 nil session

iphone - UITableViewCell 重复 UIImages

ios - Xcode 在显示 "Block implicitly retains ' self 时表现得很奇怪';明确提及 'self' 以表明这是预期行为“警告

objective-c - 在 Swift 3 中从 Objective-C calloc 重构

javascript - 困难的网络动画