简体   繁体   English

UiView.animateWithDuration不动画Swift

[英]UiView.animateWithDuration Not Animating Swift

I am trying to animate show/hide of search bar using below code (The search bar should come from left and expand to right within 1-2 seconds). 我试图使用下面的代码动画显示/隐藏搜索栏(搜索栏应该从左边开始,并在1-2秒内向右扩展)。 However, it doesn't animate and searchBar is immediately shown no matter how much time I put. 但是,它没有动画,无论我放了多少时间,都会立即显示searchBar。 I noticed following: 我发现以下情况:

  • Duration is not respected 持续时间不受尊重
  • Not even delay is respected 甚至延迟都没有得到尊重
  • Animation is not happening. 动画没有发生。 Component is immediately shown 立即显示组件

     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 }) } 

I am using Xcode 7, iOS 8 and Swift 2.0. 我正在使用Xcode 7,iOS 8和Swift 2.0。 I have seen at other solution, but none of them works for me. 我已经看到了其他解决方案,但它们都不适合我。 Kindly help... 请帮忙......

Update : It worked with below code. 更新 :它使用下面的代码。 However, it used default animation option of UIViewAnimationOptionCurveEaseInOut and TransitionNone 但是,它使用了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
            })

Before the animateWithDuration, set the XPosition to -200 and YPosition to -200. 在animateWithDuration之前,将XPosition设置为-200,将YPosition设置为-200。

Like Daniel Suggested, you need to also change the alpha from something other than 1.0 in order to see it "fade in". 像Daniel Suggested一样,你需要从1.0之外的其他东西改变alpha,以便看到它“淡入”。

Are you using autolayout? 你在使用autolayout吗? If you are, you may want to animate the constraints instead of the frame. 如果是,您可能希望为约束而不是框架设置动画。

Finally, are you sure you've wired up the search bar properly? 最后,您确定已正确连接搜索栏吗?

Edit: PS, I would keep the duration to something like 3.0 or 5.0. 编辑:PS,我会将持续时间保持为3.0或5.0。 10.0 will take forever and may make you think that it isn't doing anything because of how long it takes. 10.0会花费很长时间,可能会让你认为它没有做任何事情,因为需要多长时间。 Make it 3.0 or 5.0 (max) just for testing and then decrease to where you want it. 将其设为3.0或5.0(最大)仅用于测试,然后降低到您想要的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM