ScrollView gesture recognizer eating all touch events

前端 未结 9 714
北恋
北恋 2020-11-27 03:45

I have a UIScrollView to which I added a single tap gesture recognizer to show/hide some UI overlay using:

UITapGestureRecognizer *singleTap = [         


        
相关标签:
9条回答
  • 2020-11-27 03:56

    I think the reason is that User Interaction Enabled is set to false for UIImageView. You should set it to true to enable tapping in it

    0 讨论(0)
  • 2020-11-27 04:02

    TapGestures worked for me. The swipe on the other hand, I had to disable the scrolling and it worked.

    swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeToNewImage(_:)))
    swipeLeftGesture.direction = .left
    scrollView.addGestureRecognizer(swipeLeftGesture)
    
    swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeToNewImage(_:)))
    scrollView.addGestureRecognizer(swipeRightGesture)
    
    scrollView.isScrollEnabled = false
    
    0 讨论(0)
  • 2020-11-27 04:02

    My code.

    I checked all proposed solutions and any work for me. I do not understand why. I do not understand the reason.

    class MyClass: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
    
    scrollView.contentSize = CGSize(width: UIScreen.main.bounds.size.width, height: 800)
    
    scrollView.isUserInteractionEnabled = true
    scrollView.delegate = self
    
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(manageGesture))
    
    tap.cancelsTouchesInView = false
    
    tap.numberOfTapsRequired = 1
    
    scrollView.addGestureRecognizer(tap)
    
    scrollView.canCancelContentTouches = false
    
    self.view.addSubview(scrollView)
    
    }
    
    @objc func manageGesture(){
        // Some action
    }
    
    
    }
    
    0 讨论(0)
  • 2020-11-27 04:04

    Thanks @zambrey

    Swift 2.2+ Version:

    scrollView.delegate = self
    
    let allowMultipleTouches = UITapGestureRecognizer(target: self, action: #selector(genderPressed))
    allowMultipleTouches.numberOfTapsRequired = 1
    allowMultipleTouches.cancelsTouchesInView = false
    
    scrollView.addGestureRecognizer(allowMultipleTouches)
    

    If your scroll view is in the Storyboard, don't forget to pin the Outlet in the view controller. In this example, scrollView is the Outlet of the UIScrollView.

    0 讨论(0)
  • 2020-11-27 04:04

    You can capture any kind of gestures in the UIscrollView. Make sure you also handle some of the default properties as well like set cancelsTouchesInView property to false, it is true by default. Also give some tag nos to your sub views to distinguish in selectors. & also enable their User interaction to true.

    let tap = UITapGestureRecognizer(target: self, action:

    selector(didTapByUser(_:)))

    0 讨论(0)
  • 2020-11-27 04:07

    You can set which objects are to be included/excluded for touches.

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {            
       if (touch.view == [self view]) {
           return YES;
       }
       return NO;
    }
    
    0 讨论(0)
提交回复
热议问题