UITapGestureRecognizer
添加爲UIView
時無法正常工作。無法檢測到問題。當爲UIView添加時UITapGestureRecognizer無法工作無法檢測到問題
注意:我沒有在ViewDidLoad
中使用它。請幫我解決問題。提前致謝。
-(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];
}
其他的東西:
- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
NSLog(@"success1");
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
注:我還添加了UIGestureRecognizerDelegate
。
**這是我的最終代碼,它的工作原理** –