Marta Tenés
Marta Tenés

Reputation: 2100

Allow UITapGestureRecognizer inside UIAnimations

I'm trying to show a custom view when I receive a notification from parse. This view is showed with an animation and its hidden with another animation. This view also has a uitapgesturerecognizer that needs to be fired when the user taps the view. My problem is that when the second animation gets fired the custom view's uitapgesture doesn't work :\

Any ideas? I paste the code. Thanks!

func doManageObjectNotification(notification: COPushNotification){
    mainView = UIApplication.sharedApplication().keyWindow!
    customView = CustomNotificationView()
    let height = customView.calculateViewHeight()

    customView.frame = CGRectMake(0, -height, mainView.frame.width, height)
    customView.targetMethod = notificationWasTapped
    customView.setContent(notification)
    customView.alpha = 0
    mainView.addSubview(customView)

    UIView.animateWithDuration(0.75, delay: 0, options: [UIViewAnimationOptions.CurveEaseInOut, UIViewAnimationOptions.AllowUserInteraction], animations: { () -> Void in
       // Show the view
        self.customView.frame.origin.y = 0
        self.customView.alpha = 1
    }) { (Bool) -> Void in
        UIView.animateWithDuration(0.75, delay: 5, options: [UIViewAnimationOptions.CurveEaseInOut, UIViewAnimationOptions.AllowUserInteraction], animations: {
            // Hide the view
            self.customView.alpha = 0
            }, completion: { finished in
                self.customView.removeFromSuperview()
                self.customView = nil
        })
    }

}

Upvotes: 2

Views: 72

Answers (2)

Alessandro Ornano
Alessandro Ornano

Reputation: 35402

Im agree with Lion answer, but I want also focus your attention about customView.frame.origin.y = 0 during animation: if you use autolayout and you try to change frame dimensions or positions instead the correct constraints involved, you can disable you constraints effect causing warnings and unexpected view dimensions and movements. When you have this issue many times the UITapGestureRecognizer stop to responding.

The best way to do it is to create IBOutlets constraints and working with it (for example):

@IBOutlet weak var customViewTopConstraint: NSLayoutConstraint

...
UIView.animateWithDuration(0.75, delay: 0, options: [UIViewAnimationOptions.CurveEaseInOut, UIViewAnimationOptions.AllowUserInteraction], animations: { () -> Void in
       // Show the view
        self.customViewTopConstraint.constant = 0

Upvotes: 2

Ketan Parmar
Ketan Parmar

Reputation: 27438

For handling tap during animation you should implement touchesBegan method. by this you can detect touch and then check that touch is from your view,if yes then you can perform your desired task.

Hope this will help :)

Upvotes: 1

Related Questions