1

I'm having a rather basic problem, I've looked around (here, google, etc) and haven't found a solution for this:

In my View Controller's viewDidLoad, I have this:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(myfunc:)];

//I have a UIScrollView named "containerView"
//here's some code that creates an UIView in a variable named "myView"

//this works fine, I can see "myView" when I run it
[containerView addSubview:myView];

[myView addGestureRecognizer:longPress];

and then I have this function in the same class:

- (void)myfunc:(UIRotationGestureRecognizer *)recognizer 
{
    NSLog(@"hola!"); //never runs
}

The call to NSLog never runs. What am I doing wrong?

EDIT

Some extra info: it seems no touch events are ever sent to the subview. However, I tried adding an UIView with a button inside, all in the UIScrollView, and the button receives the touch event just fine, so the problem is only with programmatically added subviews.

cambraca
  • 27,014
  • 16
  • 68
  • 99
  • Have you tried long pressing the screen? –  Apr 23 '12 at 19:38
  • Well that's the whole point of all this, ain't it.. – cambraca Apr 23 '12 at 19:40
  • I think so :) just trolling around ^^ Your code looks perfectly, anyway. –  Apr 23 '12 at 19:41
  • Maybe you could 1. add the recognizer before you add YourView as a subview; 2. inserting it specifically on the top of any other views so no other view can intercept its touch events. –  Apr 23 '12 at 19:42
  • I tried #1, no luck. But, I tried adding a Round Rect Button inside the view and it doesn't seem to be responding to touch... any idea why this would happen? – cambraca Apr 23 '12 at 19:54
  • yes, exactly what I thought. Try #2. –  Apr 23 '12 at 19:55
  • well, they are already on top, it's just that UIScrollView and a bunch of calls to `addSubview` in different positions so they don't even overlap. So, they _are_ on the very top, right? Any change the scroll view thing is messing with me? – cambraca Apr 23 '12 at 19:59
  • Yeah, UIScrollView *badly* intercepts touch events. –  Apr 23 '12 at 20:10
  • There _must_ be a way of receiving touch events in subviews of a UIScrollView :| :| :| – cambraca Apr 23 '12 at 20:11

3 Answers3

2

Strangely enough, adding a "container" UIView inside the UIScrollView, and then the other subviews inside this container, made it work. Now touch events are sent to the subviews.

cambraca
  • 27,014
  • 16
  • 68
  • 99
0

How can a superview interecept a touch sequence before any of its subviews?

TLDR:

[containerView setCanCancelContentTouches:NO]; 

do it just as you add the gesture recogniser to the scroll view.

also look into

[containerView setDelaysContentTouches:NO];

If the above behaviour isn't quite right.

for more info: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html

Community
  • 1
  • 1
jackslash
  • 8,550
  • 45
  • 56
  • I played with those properties, they didn't help. Look at my edit on the question, I added a subview manually on the interface editor, and it receives touch events, but not my programmatically added subviews... – cambraca Apr 23 '12 at 20:55
0

I think on myFunc you must do somethings like that:

switch (reconiger.state)
{
    case UIGestureRecognizerBegin:
      //Do something when start recognizer
      break;
    case UIGestureRecognizerEnd:
      //Do something when end recognizer
      break;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Scofield Tran
  • 828
  • 1
  • 18
  • 30