0

So I have a tableviewController called SettingsViewController, and it has the following touchesEnded function:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        print("yoyoyoyoyoyoyoyQVEWEVIWNE")
        let touchLocation = touch.location(in: view)

        // 290 because the width of the view is 414, and the SettingsViewController width gets set to 0.7 * the view width in SlideInTransition.  0.7 * 414 is 289.8
        if touchLocation.x > 200 {
            dismiss(animated: true)
        }
    }
}

I made the print statement to see if it was being called, which it is not. This view controller is presented with a 'menu-esque' slide in custom transition. I have a suspicion that the bounds of the UIView is the problem somehow. Here's the custom transition code:

class SlideInTransition: NSObject, UIViewControllerAnimatedTransitioning {

var isPresenting: Bool = false

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 0.5
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    
    // Make sure they exist
    
    // The view controller being transitioned from, using the context (ex: here it's the MapViewController)
    guard let fromViewController = transitionContext.viewController(forKey: .from),
    // The view controller being transitioned to, using the context (ex: here it's the SettingsTableViewController)
        let toViewController = transitionContext.viewController(forKey: .to) else {return}
    
    let containerView = transitionContext.containerView
    
    // Constants for appearance of SettingsViewController
    
    let vcWidth = toViewController.view.bounds.width * 0.7
    let vcHeight = toViewController.view.bounds.height
    
    if isPresenting {
        // Add SettingsViewController to container
        containerView.addSubview(toViewController.view)
        
        // Initial frame for view controller, off the screen to the left to start, that way it appears to slide in
        toViewController.view.frame = CGRect(x: -vcWidth, y: 0, width: vcWidth, height: vcHeight)
    }
    
    // Animate view controller onto the screen, sliding in from left
    let transform = {
        toViewController.view.transform = CGAffineTransform(translationX: vcWidth, y: 0)
        }
    
    // Animate back off screen
    let identity = {
        // .identity returns the vc to the initial frame, as created above in the isPresenting if statement
        fromViewController.view.transform = .identity
    }
    
    // Animation of the transition
    let duration = transitionDuration(using: transitionContext)
    let isCancelled = transitionContext.transitionWasCancelled
    UIView.animate(withDuration: duration, animations: {
        // If presenting, transform SettingsViewController (to) onto screen, otherwise set it back off the screen.
        self.isPresenting ? transform() : identity()
    }) { (Bool) in
        transitionContext.completeTransition(!isCancelled)
    }
    
}

}

I made my touchesEnded code so that when the user touches outside the viewController, it dismisses, (the view controller only 70% the width of the screen) but it simply doesn't get called, regardless of where on the screen I tap. Any idea why? Thanks.

1 Answers1

0

https://developer.apple.com/documentation/uikit/uiresponder/1621084-touchesended

«If you override this method without calling super (a common use pattern), you must also override the other methods for handling touch events, even if your implementations do nothing.»

This would be a start.

cora
  • 1,916
  • 1
  • 10
  • 18
  • how do I call it with super? Not sure where to put the 'super' –  Sep 10 '20 at 00:30
  • override func touchesEnded(_ touches: Set, with event: UIEvent?) { super.touchesEnded(touches:touches, event: event) – cora Sep 10 '20 at 00:48
  • Thanks cora, but putting this in the touchesEnded function hasn't changed it. If you have any other ideas, lmk. thanks –  Sep 10 '20 at 02:26
  • Well, then I recommend these links for you. – cora Sep 10 '20 at 02:39
  • https://www.youtube.com/watch?v=Fj-08kQmcY4 - TableView with diffable datasource, which has built in previews and menus. – cora Sep 10 '20 at 02:41
  • https://stackoverflow.com/questions/22127451/touches-ended-not-being-called - Answers to similar problem which involves adding a gesture recognizer. – cora Sep 10 '20 at 02:42