2

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

+0

**這是我的最終代碼,它的工作原理** –

回答

2

只要做到這樣的..你會得到正確的解決方案..

-(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]; 
} 
+0

亞,我得到了感謝。 –

+0

我爲你取消了:) –

1

即使在「setSegmentValues:」方法中進行控制嗎? 確保您的方法正在從viewDidLoad或viewWillAppear或任何IBAction進行調用。

+0

雅,我的控制進入setSegmentValues方法。我不應該這樣使用嗎?沒有從viewDidLoad或viewWillAppear調用? –

+0

你可以像這樣使用,但是你需要從某個地方調用它,以便方法可以執行。 viewDidLoad&viewWillAppear&viewDidAppear是生命週期方法,所以它們自己執行。 您可以從他們或任何IBAction調用您的方法,如點擊按鈕。 – Sana

+0

只需將此視圖添加進viewDidLoad並嘗試: [self setSegmentValues:abcArray]; – Sana

0

您在沒有alloc init的情況下向UiView添加了一個手勢。加手勢之前 初始化視圖

請使用此代碼

-(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] 
} 
+0

不用它幫助我,無論如何謝謝。 –

+0

嘗試禁用userInteration標籤 –

+0

嘗試,但沒有它的幫助謝謝。 –

1

這是錯誤:

[val1 addGestureRecognizer:tap1]; 
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)]; 

寫:

val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)]; 
[val1 addGestureRecognizer:tap1]; 
+0

也許這也是我犯的一個錯誤。但問題仍然在那裏。它dosnt工作。感謝您的回覆 –

+0

是的,這是一個錯誤,謝謝。但是這並不能解決問題手勢不起作用。 –

相關問題