如何将 UINavigationBar 与动画重叠?

how to overlap UINavigationBar with animation?

我有一个 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 -> Animate it -> Put it back to its original super view。但是,将 imageView 移出 UIWindow 也会使其立即消失。这就是为什么我想出下面的走动:

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

解决方案 2(更好)

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

// 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

两种解决方案都可以,但第二种更干净。

希望这对您有所帮助:)