ios - UiView.animateWithDuration 不是动画 Swift

标签 ios swift animation uiviewanimationtransition ios-animations

我正在尝试使用以下代码动画显示/隐藏搜索栏(搜索栏应从左侧开始并在 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
            })

最佳答案

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

像 Daniel 建议的那样,您还需要将 alpha 从 1.0 以外的值更改为“淡入”。

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

最后,您确定已正确连接搜索栏吗?

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

关于ios - UiView.animateWithDuration 不是动画 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32229252/

相关文章:

iphone - 带有当前时间的 UILabel

ios - sinch api可以和swift一起使用吗

ios - Swift 中的电子邮件和电话验证

iOS 8.1 : Type 'ViewController' does not conform to protocol 'UICollectionViewDataSource'

c++ - 如何从head tail和roll获取骨骼变换矩阵

ios - 在SwiftUI中从摇动手势(使用UIKit)设置EnvironmentObject不会更新View

ios - 按多个键值对数据进行排序

css - 将 CSS 叠加动画化为淡入淡出

java - 如何在 Android 中简单地为 View 添加动画效果

iphone - iPhone SDK 中的 Movieplayer 导航栏框架问题