ios - UIView 动画立即完成,不考虑持续时间或延迟

标签 ios swift uiviewanimation

我在为我的 View 的 layoutConstraints 之一设置动画时遇到了一些困难:

UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
                self.topLayoutConstraint?.constant = self.currentYPosition
                self.layoutIfNeeded()
                }, completion: nil)

无论我使用的持续时间或延迟如何,动画都会立即完成(我的 View 从以前的位置跳到新的位置)。

我检查了所有的值,它们都是正确的。 我也检查过并调用了动画和完成。

有什么想法吗?

编辑:

@objc private func viewDragged(_ recognizer: UIPanGestureRecognizer) {

        let location = recognizer.location(in: self.superview)

        switch recognizer.state {
        case .ended:

            if (recognizer.direction == .Down) {            // Collapsing the slide panel

                currentYPosition = parentViewHeight - minimumVisibleHeight - statusBarHeight - navBarHeight
            }
            else if (recognizer.direction == .Up) {       // Expanding the slide panel

                // Substracting `minimumVisibleHeight` so that the dragable part is hidden behind the navigation bar
                currentYPosition = -minimumVisibleHeight
            }

            self.topLayoutConstraint?.constant = self.currentYPosition
            UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
                self.layoutIfNeeded()
                }, completion: nil)
            break
        case .began:
            HomeSlidePanelContainerView.draggingPreviousPosition = location.y
        default:
            self.layoutIfNeeded()

            if (location.y < (parentViewHeight - minimumVisibleHeight - statusBarHeight - navBarHeight)
                || location.y > (statusBarHeight + navBarHeight)) {

                currentYPosition -= HomeSlidePanelContainerView.draggingPreviousPosition - location.y
                topLayoutConstraint?.constant = currentYPosition
                self.layoutIfNeeded()
            }
            HomeSlidePanelContainerView.draggingPreviousPosition = location.y
            break
        }

    }

最佳答案

我已经设法找到解决方案。这可能会对其他人有所帮助,所以我要回答我自己的问题。

我做的是这样的:

self.topLayoutConstraint?.constant = self.currentYPosition

UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {               
                self.layoutIfNeeded()
}, completion: nil)

如您所见,我正在更改一个值 topLayoutConstraint,它是对 self 的约束,self 是一个 UIView

然后我尝试通过在动画闭包中调用 self.layoutIfNeeded 来为该更改设置动画。

这里的问题是 layoutIfNeeded() 适用于 subview 而不是 View 本身。所以在我的例子中,我试图为 self 的 subview 而不是 self 本身的布局设置动画。

self.layoutIfNeeded() 更改为 self.superview?.layoutIfNeeded() 瞧!

关于ios - UIView 动画立即完成,不考虑持续时间或延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40491726/

相关文章:

ios - UICollectionView scrollToItemAtIndexPath 单元格在半隐藏时消失

ios - Swift 按钮调整大小动画

ios - 将日期字符串转换为 NSDate

ios - Objective-c : Pass value from AppDelegate to UIViewController

objective-c - 使用 GestureRecognizers 操作 UIImage - NOT UIImageView

swift - 如何将 CollectionView 单元格彼此相邻对齐?

iphone - UItable 标题在滚动时始终显示

swift - 我怎样才能让两个球员在触球时都移动?

ios - 如何在 swift3 的 UICollectionView 中显示两个不同的单元格

ios - 关键帧动画失败