Hi, I have a full-screen UIView and a UISwitch as subview. I'm implementing a UIGestureRecognizer on the UIView. The gesture recognizer works fine, but when I enable it, the UISwitch no longer works by tapping it - I have to explicitely slide my finger over it to toggle it. I rather want that the UIGestureRecognizer works only on the parent UIView, but not on it's subviews. When I don't use gesture recognizers but the touchesBegan/touchesEnded methods instead, the UISwitch is working as intended. Is there any way around this?
Code in UIView:
Code:
- (id)initWithFrame:(CGRect)frame andPlayerName:(NSString*)playerName {
if ((self = [super initWithFrame:frame])) {
self.backgroundColor = [UIColor redColor];
self.opaque = NO;
myPlayerName = playerName;
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];
nameLabel.text = myPlayerName;
nameLabel.textColor = [UIColor whiteColor];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.opaque = NO;
nameSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(20, 50, 50, 20)];
[nameSwitch addTarget:self action:@selector(toggleNameLabelVisibility) forControlEvents:UIControlEventValueChanged];
nameSwitch.on = TRUE;
[self addSubview:nameLabel];
[self addSubview:nameSwitch];
[nameLabel release];
[nameSwitch release];
[self createGestureRecognizers];
}
return self;
}
- (void)createGestureRecognizers {
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[self addGestureRecognizer:singleTap];
[singleTap release];
}
- (void)handleSingleTap:(UIGestureRecognizer*)sender {
CGPoint location = [sender locationInView:self];
NSLog(@"%f, %f", location.x/768, location.y/512);
}