1

I'm making a notification bar to fade in and fade out again at the top of my view. I've added a tap gesture recogniser so that when the user clicks the notification when it's showing, it can take him to that update or whatever.

UIView.animateWithDuration(0.8, animations: {
        self.navigationViewController.notificationView.view.alpha = 1.0
    }, completion: { finished in
        UIView.animateWithDuration(0.8, delay: 5.0, options: .CurveLinear, animations: {
            // This line below prevents the tap gesture from ever being recognized
            self.navigationViewController.notificationView.view.alpha = 0.0
        }, completion: nil)
    })
}

As you can see, the update fades in, shows for 5 seconds then fades out. When I remove the commented line, everything works fine and the gesture is recognised, but when I uncomment it, for some reason, when it's visible, the gesture recogniser won't recognise any taps.

Here's the code for defining the view and the recogniser:

// notificationView view controller:
override func viewDidLoad() {
    super.viewDidLoad()
    tap = UITapGestureRecognizer(target: self, action: "tapped")
    self.view.addGestureRecognizer(tap)
    tap.numberOfTouchesRequired = 1
    tap.numberOfTapsRequired = 1
    tap.cancelsTouchesInView = false
}

I don't know if it makes any difference or not, but the view is being displayed over a navigation view controller, so I'm not able to use the addchildviewcontroller method to add the view without it adding it as a page. I don't think this is the issue though as it works with the line uncommented.

I've also done

notificationView.view.userInteractionEnabled = true

which doesn't seem to help.

Thanks in advance!

EDIT: I've distilled the problem down to this sample project: https://github.com/magicmark/SwiftAnimationProblem

You can see the problem clearly here. Is this another swift bug maybe?

EDIT 2: I've done a version in Objective C. Same problem!

Mark
  • 12,359
  • 5
  • 21
  • 37

1 Answers1

0

I haven't pulled down your sample yet, but I suspect that if you put a watchpoint on self.navigationViewController.notificationView.view.alpha you will find that it is set to 0 at the moment the first animation is completed. Instead of using animate with delay. You will need to create an NSTimer (or use GCD) to handle the delay.

This is part of the nature of how the UIView animation system works. Just like when you change the size in an animation. The model contained within the view is changed immediately, then the view is animated to conform to the model.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72