I don't want closeShareView
being called when the user taps on contentView
, only on modalView
, how can I do that?
UIWindow* mainWindow = (((AppDelegate *)[UIApplication sharedApplication].delegate).window);
// Modal view
UIView *modalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
modalView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
// Content view (goes inside modalView)
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(modalView.center.x, modalView.center.y, 220, 220)];
[modalView addSubview:contentView];
[mainWindow addSubview:modalView];
// Tap gesture (added to modalView)
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeShareView:)];
gr.numberOfTapsRequired = 1;
[modalView addGestureRecognizer:gr];
So you want something like a "click outside the content view to dismiss the modal" kind of deal?
Instead of adding the tap gesture to the entire modal view, have a background view that handles that, and the content view will naturally intercept all taps (if it has user interaction enabled), so you basically don't have to do any coding.
UIWindow* mainWindow = (((AppDelegate *)[UIApplication sharedApplication].delegate).window);
// Modal view
UIView *modalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Background tap view
UIView *backgroundView = [[UIView alloc] initWithFrame:modalView.bounds];
backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];
[modalView addSubview:backgroundView];
// Content view (goes inside modalView)
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(modalView.center.x, modalView.center.y, 220, 220)];
[modalView addSubview:contentView];
[mainWindow addSubview:modalView];
// Tap gesture (added to backgroundView)
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeShareView:)];
gr.numberOfTapsRequired = 1;
[backgroundView addGestureRecognizer:gr];