Add UITapGestureRecognizer to UITextView without blocking textView touches

后端 未结 3 1121
慢半拍i
慢半拍i 2020-12-05 15:31

How can I add a UITapGestureRecognizer to a UITextView but still have the touches getting through to the UITextView as normal?

Cur

3条回答
  •  心在旅途
    2020-12-05 16:23

    Swift 4.2
    The following steps allows me to escape a full-screen UITextView with a tap, whilst allowing to scroll the contents of the UITextView:

    1. Disconnected the UIGestureRecognizer from the UITableView.
    2. Made a CustomTextView: UITextView.
    3. Added a 'sender' var to the particular UIViewController with the CustomTextView.
    4. Trap for 'Touches Ended...'
    5. Call for an excape function within the sender UIViewController.

    class CustomTextView: UITextView {
        var sender: DocViewController?
    
        override func touchesEnded(_ touches: Set, with event: UIEvent?) {
            if let controller = sender {
                controller.handleSwipeGesture()
            }
        }
    }
    

    I can either scroll the contents of the UITextView or merely tap to exit.
    The 'sender' is set from the hosting UIViewController at creation.

提交回复
热议问题