【问题标题】:UiView.animateWithDuration Not Animating SwiftUiView.animateWithDuration 不是动画斯威夫特
【发布时间】:2015-11-20 15:33:13
【问题描述】:

我正在尝试使用以下代码对搜索栏的显示/隐藏进行动画处理(搜索栏应从左侧开始并在 1-2 秒内向右扩展)。但是,无论我投入多少时间,它都不会设置动画,并且会立即显示 searchBar。我注意到以下内容:

  • 不遵守时长
  • 甚至不考虑延迟
  • 动画没有发生。组件立即显示

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    //code to get selected value...
    //Hide the collection view and show search bar
    
    UIView.animateWithDuration(10.0,
        delay: 0.0,
        options: UIViewAnimationOptions.TransitionCrossDissolve,
        animations: {
            self.searchBar.hidden = false
            self.searchBar.frame = CGRectMake(0, 0, 300, 44) //This doesn't work either
        },
        completion: { (finished: Bool) in
            return true
        })
    }
    

我正在使用 Xcode 7、iOS 8 和 Swift 2.0。我见过其他解决方案,但它们都不适合我。请帮忙...

更新:它适用于以下代码。但是,它使用了UIViewAnimationOptionCurveEaseInOutTransitionNone 的默认动画选项

UIView.animateWithDuration(0.7,
                animations: {
                    self.searchBar.alpha = 1.0
                    self.searchBarRect.origin.x = self.searchBarRect.origin.x + 200
                    self.searchBar.frame = self.searchBarRect
                },
                completion: { (finished: Bool) in
                    return true
            })

【问题讨论】:

  • 你在哪里将搜索栏的 alpha 设置为 0?
  • 我怀疑框架更改将在自动布局系统中起作用。
  • Alpha 在 uiview 代码之前设置为零
  • 这就是我所指出的,如果您使用自动布局,那么视图的帧更改将不起作用..
  • 该死!我的错。为了隐藏,我将hidden 设置为true,后来为了显示它,我使用了alpha。当我在两边都使用 alpha 时,动画开始工作。但是,无论我使用什么动画选项,它总是显示一个动画,好像它是“从中心弹出”

标签: ios swift animation uiviewanimationtransition ios-animations


【解决方案1】:

在 animateWithDuration 之前,将 XPosition 设置为 -200,将 YPosition 设置为 -200。

就像 Daniel Suggested 一样,您还需要将 alpha 从 1.0 以外的版本更改为“淡入”。

你在使用自动布局吗?如果是,您可能希望为约束设置动画而不是框架。

最后,你确定你已经正确连接了搜索栏吗?

编辑:PS,我会将持续时间保持在 3.0 或 5.0 左右。 10.0 将永远持续下去,并且可能会让您认为它没有做任何事情,因为它需要多长时间。将其设置为 3.0 或 5.0(最大值)仅用于测试,然后降低到您想要的位置。

【讨论】:

  • 很多不同的方法来处理它。一种方法是启动 2 个不同的动画(1 个执行移动,另一个执行淡入淡出)。将一个动画设置为在第一个动画开始后 1 秒开始偏移。另一种是使用动画 GROUP - 在这里您可以获得更细粒度的控制
  • 如果我能看到一个工作代码 sn-p,我将不胜感激
  • 我选择你的解决方案作为答案,除了少数例外:10 seconds is not like forever, I am on AutoLayout, but I don't need to animate constraints, I would still animate frame
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 2017-11-13
相关资源
最近更新 更多