Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to overlap UINavigationBar with animation?

I have a UIImageView that grows its bounds with the following animation:

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

however the navigationBar covers some of the UIImageview when its animated. How do I somehow overlap the navigationBar with the UIImageview when it's animated? I would just move the hierarchy position of the items in the document outline, but I don't know how to do so with a navigationBar.. So any suggestions?

like image 920
John Jackson Avatar asked Feb 21 '26 19:02

John Jackson


1 Answers

SOLUTION 1

In the beginning I thought it would be possible to move the imageView to the KeyWindow -> Animate it -> Put it back to its original super view. However moving the imageView out of UIWindow will also make it disappear immediately. That's why I came up with the walk around below:

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

SOLUTION 2 (BETTER)

After getting back to the previous solution, I was able to find out that after moving the imageView back from the key window, if I wait a bit before restoring its previous frame, everything works just fine!

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

Here is a better version: https://gist.github.com/dobaduc/1894fc8c8e6a28c2d34c

Both solutions work ok, but the second one is much cleaner.

Hope this helps :)

like image 127
Ducky Avatar answered Feb 24 '26 09:02

Ducky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!