Subview Gesture Recognizer Not Being Called

Subview Gesture Recognizer not being called

For anyone curious, the issue was that the subview with gesture recognizer was outside the frame of the superview. This means even though the view was being drawn, the gestures were not detected

Swipe gesture is not being called on a subview inside a present view controller

Add Gesture only in view and you can also add any direction on gesture. Here i put Small example, you can refer this code for your problem.

Get 1 for Right Direction and 2 For Left Direction

Example :

import UIKit

class ViewController: UIViewController,UIGestureRecognizerDelegate{

@IBOutlet weak var viewGray: UIView!
@IBOutlet weak var viewRed: UIView!

override func viewDidLoad() {
super.viewDidLoad()

self.navigationController?.interactivePopGestureRecognizer?.delegate = self

let directions: [UISwipeGestureRecognizer.Direction] = [.right, .left]
for direction in directions {
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
gesture.direction = direction
gesture.delegate = self
self.viewRed.addGestureRecognizer(gesture)
}
}

@objc func handleSwipe(sender: UISwipeGestureRecognizer) {
print(sender.direction)
}

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}

Sample Image

View is drawn but gesture recognizer are not detected

I think you need to correctly set the contentView's size. Right now you set the contentView's frame to the size of the scrollView's frame, which is 1/4 the total size. Notice the last line in viewDidLoad where we set the contentView's frame to be the scrollView's content size.

    class DummyVC: UIViewController, UIScrollViewDelegate {

let scrollView = UIScrollView(frame: CGRect(x:0, y:0, width:320,height: 300))
var contentView: UIView!
var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)

override func viewDidLoad() {
super.viewDidLoad()

var contentFrame = scrollView.frame
contentFrame.size.width *= 4
contentView = UIView(frame: .zero)
scrollView.addSubview(contentView)

self.view.addSubview(scrollView)

for index in 0..<4 {

frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.size = self.scrollView.frame.size
self.scrollView.isPagingEnabled = true

let subView = AwesomeView(frame: frame)
self.contentView.addSubview(subView)
}

self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4, height: self.scrollView.frame.size.height)
//new line to set contentView's frame
contentView.frame = CGRect(origin: .zero, size: scrollView.contentSize)
}
}

iOS gestureRecognizer not handled subviews

You would have to subclass the UIButton and over ride the tap delegate callback and forward this call to whatever is handling a UISwipeGestureRecognizer. Unless you add the gesture recognizer on the UIButton, it will always call it's touch handler before the view behind it. You can also explicitly tell the button to not handle it's touch events thus passing the touch event down the chain (via userInteractionEnabled), but as you've already stated you do not want this. The best way to go about this would be by creating a subclass of UIButton and handling the touch events there and/or forwarding the events using delegation. Pressing the button is a touch event, so you may just want to add a tap gesture recognizer to the button and call the IBAction from that and then have the swipegesturerecognizer forward a delegate call.

Swift - tap gesture is not recognized in subView of KeyWindow

In your original question, you didn't show how you use the Menu class itself to make anything happen. But that could be where the problem is.

For example, suppose you create the Menu class instance but you don't maintain it somehow. Then you might be able to create the overlay, but after that, the Menu instance is gone, so the tap gesture recognizer subsequently has no target to talk to.

In other words, you've got this tap gesture configuration code:

backView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(dismissHandler)))

...but when the user taps and the tap gesture recognizer tries to send its message, your self has gone out of existence.



Related Topics



Leave a reply



Submit