2

I have a UIScrollView that fills half of the screen. In the other half, I have another UIView.

When the user taps, pans, pinches in the UIView; I want these gestures to take effect in the UIScrollView, as if they happended there.

Is this possible?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sergio
  • 1,610
  • 14
  • 28

2 Answers2

1

Disclaimer: this is a fairly lightweight solution, but depending on what you want to achieve it might already be enough.

You can try subclassing UIScrollView to provide some custom recognition of whether any given touch event is "in" or "out" of the scrollView's frame, and simply declare it "in" whenever the touch happens within the lower half of your screen, or whatever other "control area" you desire.

 class CustomScrollView: UIScrollView {

     var targetFrame: CGRect? // set this to the frame of your "Other View"

     override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
         return targetFrame?.contains(point) == true
     }
 }

Note: if you do not provide a CGRect and simply return true, a dragging or pinching gesture anywhere on the screen will register on the scrollView.

Gamma
  • 1,347
  • 11
  • 18
0

I created an UIView called PassGesturesView that overrides the hitTest method. The UIView that receives the gestures, must extend it:

//
//  PassGesturesView.swift
//  pass-gestures
//
//  Created by Sergio Rodríguez Rama on 23/11/2019.
//  Copyright © 2019 Sergio Rodríguez Rama. All rights reserved.
//

import UIKit

class PassGesturesView: UIView {

    var sisterView: UIView?

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        return sisterView
    }

    // MARK: - Init

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

}

Then, just set the scroll view (or any other UIView where you want to send the gestures) as the sisterView of your PassGesturesView.

passGesturesView.sisterView = scrollView
Sergio
  • 1,610
  • 14
  • 28