ios - swift 4 约束动画无法正常工作

标签 ios animation constraints swift4

我有自定义 tapbar,其中包含 collectionView 和 4 个项目。我有白色 View ,它指示所选项目并在点击另一个 collectionView 项目时更改其位置(带动画)。带约束的白色 View 定位,我将左 anchor 约束更改为动画

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let x = CGFloat(indexPath.item) * frame.width / 4
        self.horizontalBarLeftAnchorConstraint?.constant = x
        UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
            self.layoutIfNeeded()
        }, completion: nil)
    }

动画无法正常工作,一开始它会跳转到当前位置之前 1 个宽度(白色 View )的位置,并从该位置开始动画

我发现了问题// 我有这个约束的另一个动画来响应滚动 Collection View ,这里我有产生这种跳跃的双重动画,我删除了第二个动画,现在一切看起来都很好

最佳答案

你没有设置任何动画条件,也许我不明白你的问题。但是你应该使用:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    // BEFORE ANIMATION CONTEXT

    UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {

    // ANIMATION TILL END CONTEXT
    self.layoutIfNeeded

    }, completion: {

    // AFTER ANIMATION CONTEXT

    })
}

关于ios - swift 4 约束动画无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50677207/

相关文章:

ios - Swift 3 和 lldb : Print frame of UIView, 给定十六进制地址

c# - 简单的按钮动画

ios - 我不知道这个 iOS 元素对于 xcode 是什么

ios - 通过代码在sqlite3中启用foreign_keys=ON

css - 单击以缩放使用 ng-repeat 创建的 div

c# - 为什么具有 "where T : class"约束的 Generic<T> 方法接受接口(interface)

sql - 自引用外键约束和删除

java - JPA 中复合键的唯一约束

ios - Objective-C:如何声明/定义 tableView 上使用的方法

javascript - 如何衡量 jQuery 动画与 CSS3 转换的性能?