Reputation: 180
UITapGestureRecognizer
not working when added for UIView
. Cannot detect the issue.
Note: I am not using it in ViewDidLoad
. Please help me is solving the problem. Thanks in Advance.
-(void)setSegmentValues:(NSArray *) values
{
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
tap1.numberOfTapsRequired = 1;
tap1.delegate=self;
[val1 addGestureRecognizer:tap1];
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
val1.backgroundColor = [UIColor magentaColor];
label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,70, 40)];
label1.backgroundColor = [UIColor yellowColor];
label1.text = [values objectAtIndex:0];
label1.textAlignment = UITextAlignmentCenter;
val1.userInteractionEnabled=YES;
[val1 addGestureRecognizer:tap1];
[val1 addSubview:label1];
[self addSubview:val1];
}
Other stuffs:
- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
NSLog(@"success1");
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Note: I've also added UIGestureRecognizerDelegate
.
Upvotes: 2
Views: 6862
Reputation: 1643
Just do like this.. You will get correct solution..
-(void)setSegmentValues:(NSArray *)values bgColor:(UIColor *)color
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedIndexValue:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
viewOne = [[UIView alloc]initWithFrame:CGRectMake(40, 50, 80, 60)];
viewOne.backgroundColor = color;
viewOne.tag = 0;
UILabel *lblOne = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 60)];
lblOne.text = [values objectAtIndex:0];
lblOne.textAlignment = UITextAlignmentCenter;
lblOne.backgroundColor = [UIColor clearColor];
[viewOne addSubview:lblOne];
viewOne.userInteractionEnabled = YES;
[viewOne addGestureRecognizer:tap];
[self addSubview:viewOne];
}
-(void)selectedIndexValue:(UIGestureRecognizer *)gesture
{
int myViewTag = gesture.view.tag;
if (myViewTag == 0)
{
selectedIndex1 = 0;
self.lblValue.text = [valueArray objectAtIndex:myViewTag];
viewOne.backgroundColor = [UIColor colorWithRed:57.0f green:122.0f blue:255.0f alpha:1.0f];
viewTwo.backgroundColor = viewColor;
viewThree.backgroundColor = viewColor;
}
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
Upvotes: 2
Reputation: 2020
this is error:
[val1 addGestureRecognizer:tap1];
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
write:
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
[val1 addGestureRecognizer:tap1];
Upvotes: 1
Reputation: 721
You added a gesture to an UiView without alloc init. Initialize the View before adding Gesture to it
Please use this code
-(void)setSegmentValues:(NSArray *) values
{
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
tap1.numberOfTapsRequired = 1;
tap1.delegate=self;
[val1 addGestureRecognizer:tap1];
val1.backgroundColor = [UIColor magentaColor];
label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,70, 40)];
label1.backgroundColor = [UIColor yellowColor];
label1.text = [values objectAtIndex:0];
label1.textAlignment = UITextAlignmentCenter;
val1.userInteractionEnabled=YES;
[val1 addGestureRecognizer:tap1];
[val1 addSubview:label1];
[self addSubview:val1]
}
Upvotes: 0
Reputation: 557
Is you control even going in "setSegmentValues:" method? Make sure your method is being called from viewDidLoad or viewWillAppear or any IBAction.
Upvotes: 1