如何将UINavigationBar与动画重叠?

时间:2015-09-07 21:10:43

标签: 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")
        }
    }

然而,当它的动画时,navigationBar会覆盖一些UIImageview。当动画时,我如何以某种方式将navigationBar与UIImageview重叠?我只想移动文档大纲中项目的层次结构位置,但我不知道如何使用navigationBar ..所以有任何建议吗?

1 个答案:

答案 0 :(得分:1)

解决方案1 ​​

一开始我认为可以将imageView移动到KeyWindow - >动画它 - >把它放回原来的超级视图。但是,将UIView移出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

两种解决方案都可以正常工作,但第二种解决方案更清洁。

希望这会有所帮助:)