2

I am currently trying to create a View that would handle all the Gesture that could happened on my application.

I would like this view transparent in order to put other view below and they would still display (I don't want to make them subbiews of the handling view)

The Gesture handling works fine until I set the view color on "clearColor" from then, it is has the view disappear. Unless I stick subviews, in this case the gesture are only happening when hitting on subviews.

My question hence is: How could I manage to have the Gesture event happening on a transparent view?

Popeye
  • 11,839
  • 9
  • 58
  • 91
fBourgeois
  • 316
  • 2
  • 10
  • If memory serves, a view with opacity set to 0 will NOT respond to gestures. However, I would like to be able to do this as well.. – Patrick Borkowicz Jun 21 '12 at 16:11
  • Is you transparent view the delegate for the Gesture? I am not sure if you are doing this in storyboard or in code, but in storyboard for example have you control dragged from the gesture recognizer to the transparent view and set it as the delegate? I think this should work. – Kevin Horgan Jun 21 '12 at 16:15
  • see:http://stackoverflow.com/questions/3344341/uibutton-inside-a-view-that-has-a-uitapgesturerecognizer – Patrick Borkowicz Jun 21 '12 at 16:16
  • @KevinHorgan Horgan : I am doing this in code. And the view responds well as long as it is not transparent. The problem is not the linking of objects, but the view considered as not existing when transparent – fBourgeois Jun 22 '12 at 07:43
  • @Patrick : The solution given in your link tells how to avoid the gesture to be handled if not on the view but on a subview. My issue is to make the gesture happen when the view is transparent and without subview. – fBourgeois Jun 22 '12 at 07:44

1 Answers1

1

Try something like this. This code adds two subviews to the main view "bottomView which is has a red background and then "testView" which is transparent an overlay on top of "bottomView" with a tap gesture recognizer. If you tap anyway in the "testView" it will print out the NSLog message. I hope that helps.

-(void)viewDidLoad
{
    [super viewDidLoad];

    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [bottomView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:bottomView];    
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 150)];
    [testView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:testView];

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] 
                                          initWithTarget:self
                                          action:@selector(handleTap:)];
    [tapRecognizer setNumberOfTapsRequired:1];
    [tapRecognizer setDelegate:testView];
    [testView addGestureRecognizer:tapRecognizer];
}

-(void)handleTap:(UITapGestureRecognizer *)sender 
{
     NSLog(@"Tapped Subview");
}
tonymontana
  • 5,728
  • 4
  • 34
  • 53
Kevin Horgan
  • 1,590
  • 16
  • 20
  • How can you make testView a delegate of tapRecognizer? Shouldn't be the viewController that handle the viewDidLoad? – tiguero Jan 24 '13 at 18:13
  • 1
    I also wanted to mention that this solution doesn't seem to work on transparent view. Can you confirm? – tiguero Jan 24 '13 at 18:14