How to prevent touch event passed to UIView supervisor?

I created a program that contains images that you can drag to move around the screen. I also used UIScrollView as the space is bigger than the window. I have subclassed UIView as an image in order to display the image and handle the strokes in this class. as routines, images are added to scrollView. but now I have a problem: when I drag the image, scrollView moves with the image. I am wondering if there is a way to use strokes in the picture class so that they do not go into the supervisor.

+4
source share
5 answers

You must put your code in the "touchhesMoved: withEvent:" method of your UIView. If your view will handle this event on its own, then it should not bubble for observation

+7
source

Comment on Icewind above solved the problem:

You can try setting canCancelContentTouches to NO in your UIScrollView. And set exclusiveTouch to YES in your UIView with image - icewind March 7th at 2:22 pm

+3
source

Why are you using UIScrollView ? You have your own kind of scroll, so there is no need to use UIScrollView to handle touch scroll input.

differently why are you using your own scroll view? UIScrollView will handle the scroll register, so you can just put a UIImageView under a UIScrollView , which will work the same way if your own view has no other behavior, but you can subclass UIImageView or UIView and don't need to scroll. You yourself know that UIScrollView will do it for you .

0
source

I know this is an old question, but I had a similar problem, and the approved answer (setting canCancelContentTouches to false in the scroll and isExclusiveTouch to true in the review) did not work for me.

After some experimentation, I found that just overriding gestureRecognizerShouldBegin(_:) in my subview to return false solved the problem:

 override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { return false } 
0
source

You need to subclass UIScrollView and override touchesShouldCancel :

 override func touchesShouldCancel(in view: UIView) -> Bool { return !(view is MyView) // or whatever your check is } 

As a bonus, you might want scrollView.delaysContentTouches = false for an instant response to your user controls.

0
source

Source: https://habr.com/ru/post/1342604/


All Articles