Pass specific touches through UIScrollView to the main views

I have a view that has two subtitles:

  • Subview A, UIView (contains other views containing UIButtons, views with gesturerecognizer ...)
  • Subview B, UIScrollView (contains some views, but has transparent areas).

Scrollview sits on top of Subview A and has the full width / height of the device. I want the user to be able to interact - through transparent areas - with all of these buttons and gesture removers under the scroll list, while at the same time being able to scroll (so there is no hittest transmission).

This seems to be a pretty simple task, but I can't get it to work. Scrollview always blocks all touches.

Any idea how I accomplished this? Thanks!

+8
ios objective-c uiscrollview
source share
3 answers

I solved this somewhat now by adding a view, which should be below scrollview, to scrollview as the first subset and offset its position in scrollViewDidScroll:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView { [self updateBottomViewPosition]; } - (void)updateBottomViewPosition { CGPoint contentOffset = _mainScrollView.contentOffset; CGFloat y = MAX(contentOffset.y, 0); CGRect frame = _bottomPage.frame; if (y != frame.origin.y) { frame.origin.y = y; _leadPage.frame = frame; } } 

It works, but maybe not as elegantly as possible.

+2
source share

You must subclass UIScrollView and overwrite the following method:

 -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 

If this method returns NO, scrollview will be transparent for touch events.

Since you want the scrollview to be “transparent” to touch events only if the touch is in the transparent area of ​​your scrollview, your implementation should look like this:

 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { return ![self isPointInsideATransparentRegion:point]; //you need to implement isPointInsideATransparentRegion to check whether the point touched is in a transparent region or not } 
+10
source share

Based on Renee's answer, this works if the background view is fixed (for example, at the top of the view) and the bottom scroll has a rebound.

 #pragma mark - Scroll delegate -(void)scrollViewDidScroll:(UIScrollView *)scrollView { [self updateBottomViewPosition]; } - (void)updateBottomViewPosition { CGPoint contentOffset = self.scrollView.contentOffset; CGFloat y = MAX(contentOffset.y, 0); CGRect frame = self.fixedView.frame; if (y > 0) { frame.origin.y = y; } else { frame.origin.y = 0; } self.fixedView.frame = frame; } 

You can use this to create a good parallax effect, if you do not want it to be fixed, you must change the y value to the desired transient motion.

0
source share

All Articles