UiView.animateWithDuration Not Animating Swift



我正在尝试使用下面的代码动画显示/隐藏搜索栏(搜索栏应该来自左边,并在1-2秒内扩展到右边)。然而,它没有动画,无论我花多少时间,搜索栏都会立即显示。我注意到以下内容:

  • 不考虑持续时间
  • 甚至不考虑延迟
  • 动画没有发生。组件立即显示

    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。我看过其他的解决办法,但都不适合我。请帮助…

Update:它工作在下面的代码。但是,它使用了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(最大)只是为了测试,然后减少到你想要的地方。

最新更新