ios - 如何将 UINavigationBar 与动画重叠?

标签 ios swift animation uiimageview uinavigationbar

我有一个 UIImageView,它通过以下动画扩大其边界:

override func viewDidAppear(animated: Bool) {

        var longPress = UILongPressGestureRecognizer(target: self, action: Selector("longPress:"))

        imageView.addGestureRecognizer(longPress)
        imageView.userInteractionEnabled = true
    }

    func longPress(gesture:UILongPressGestureRecognizer)
    {
        if gesture.state == UIGestureRecognizerState.Began
        {
            oldbounds = self.imageView.bounds

            let bounds = self.imageView.bounds
            UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
                self.imageView.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 50, height: bounds.size.height + 50)
            }, completion: nil)

            println("user pressed on image")
        }
        else if gesture.state == UIGestureRecognizerState.Changed
        {
            gesture.state == UIGestureRecognizerState.Began
        }
        else
        {
            let bounds = self.imageView.bounds
            UIView.animateWithDuration(0.2, animations: {
                self.imageView.bounds = self.oldbounds
            })

            println("user release on image")
        }
    }

然而,导航栏在其动画时覆盖了一些 UIImageview。动画时如何以某种方式将 navigationBar 与 UIImageview 重叠?我只想移动文档大纲中项目的层次结构位置,但我不知道如何使用导航栏这样做。那么有什么建议吗?

最佳答案

解决方案 1

一开始我认为可以将 imageView 移动到 KeyWindow -> 动画它 -> 将它放回原来的 super View 。但是,将 imageView 移出 UIWindow 也会使其立即消失。这就是为什么我想到了下面的走动:

https://gist.github.com/dobaduc/79374c42d3af3756e345

解决方案 2(更好)

回到之前的解决方案后,我发现在将 imageView 从关键窗口移回后,如果我稍等片刻再恢复之前的帧,一切正常!

// This method is to ensure that the imageView will appear exactly at the point you want in the key window
func bringImageViewToWindow() {
    let window = UIApplication.sharedApplication().keyWindow!
    let origin = imageView.superview!.convertPoint(imageView.frame.origin, toView: window)

    frameInSuperView = imageView.frame
    frameInWindow = CGRect(origin: origin, size: frameInSuperView.size)
    imageView.frame = frameInWindow

    window.addSubview(imageView)
}

func bringImageViewBack() {
    view.addSubview(imageView)

    // Without this block, the imageView will `disappear` magically for some reasons :-)
    let delay = 0.01 * Double(NSEC_PER_SEC)
    let time  = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue(), {
      self.imageView.frame = self.frameInSuperView
    })
}

这是一个更好的版本: https://gist.github.com/dobaduc/1894fc8c8e6a28c2d34c

这两种解决方案都可以,但第二种解决方案更简洁。

希望这有帮助:)

关于ios - 如何将 UINavigationBar 与动画重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446076/

相关文章:

ios - 分组图像过滤器和链接图像过滤器之间的区别

ios - tableView + CoreData 上的空单元格

ios - 将 LinkingObjects 视为列表

ios - layoutIfNeeded 内的代码被调用两次

ios - 使用 CoreBluetooth 检测 Apple Watch

ios - 自定义委托(delegate)方法不适用于 iOS

javascript - CSS 过渡以在单击时将点分页移动到左/右

objective-c - 对 uiview 的 socket 集合应用许多约束并为其设置动画

ios - 如何将 ImageSlideshow 库与 sdwebimage 库一起使用

ios - swift UIView.animate 跳过动画