3

I have a UIViewController with a subclass of UIView on it called customSubView1. Then on customSubView1 I have another subclass of UIView called customSubView2.

I can capture all the events for touches on all the subviews when I put touchesBegan/touchesMoved/etc in the UIViewController class. But I want to be able to process them in my custom classes.

I keep reading that the UIViewController class needs to 'forward' the touch events to the subviews, but I haven't been able to find any example code to do this. Does anyone have any idea?

Many thanks, Brett

Brett
  • 11,637
  • 34
  • 127
  • 213

2 Answers2

0

Implement the touches... methods in your views.

fzwo
  • 9,842
  • 3
  • 37
  • 57
  • They are all present, but none of them are called. – Brett Jul 06 '11 at 15:42
  • And your views are present, visible, have `userInteractionEnabled = YES` (should be default for straight UIView subclasses)? Are they inside a scrollView, perhaps? Have you tried doing it the most basic way first, to eliminate other causes of error? `CustomViewClass *myCustomTestView = [[CustomViewClass alloc] initWithFrame:CGRectMake(100,100,100,100)]; myCustomView.userInteractionEnabled = YES; myCustomView.backgroundColor = [UIColor redColor]; [self.view addSubview:myCustomView];` should work. – fzwo Jul 06 '11 at 15:48
  • Yeah, that was just to cover the bases; sometimes it's the most obvious problems that are overlooked. Are they in a scrollView? The scrollView can "steal" touches. Have you tried my very basic example? – fzwo Jul 06 '11 at 19:05
  • I'm having this exact same problem and I tried the above code CustomViewClass *myCustomTestView = [[CustomViewClass alloc] initWithFrame:CGRectMake(100,100,100,100)]; myCustomView.userInteractionEnabled = YES; myCustomView.backgroundColor = [UIColor redColor]; [self.view addSubview:myCustomView]; and calls on touchesBegan on the UIView subclass. Was there ever any solution to this problem? – CodeOwl Oct 21 '15 at 05:19
0

Try to set for you UIView object userInteractionEnabled property:

myUIView.userInteractionEnabled = YES;
jamapag
  • 9,290
  • 4
  • 34
  • 28