2013-09-27 79 views
1

我嘗試將UIImageView添加到下圖中,但它不起作用! _img_compass_text_background的UIImageView向UIImageView添加觸摸不起作用

我發現了另一個觀點是覆蓋我的看法,但多數頂視圖的充滿透明的,我怎麼能強迫水龍頭穿過它?

換句話說,如何禁用消費水龍頭視圖?

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationNameTapped)]; 
tapGestureRecognizer.numberOfTapsRequired = 1; 
_img_compass_text_background.userInteractionEnabled=YES; 
[_img_compass_text_background addGestureRecognizer:tapGestureRecognizer]; 
+0

你如何檢查,這是不叫。 – Jitendra

+0

在locationNameTapped方法中添加斷點並檢查它是否正在調用。 –

+0

確保在到達UIImageView之前攔截輕擊事件的圖像視圖沒有視圖前端。 –

回答

1

你說你imageView有另一個view在上面?

在這種情況下,有很多事情可以做:

  • 上重疊view添加tapGesture,並在目標方法,檢查抽頭點被包含在imageView幀的rect 。使用locationInViewCGRectContainsPoint

  • 使用touchesBegan/Moved/Ended並檢查imageView幀是否包含使用containsPoint的觸摸點。

編輯:

加點觸手勢您重疊的視圖,目標方法是這樣的:

- (void) targetMethod:(UITapGestureRecogniser*)gesture{ 

    CGPoint tapPoint = [gesture locationInView:(the view that contains both the views)]; 

    if (CGRectContainsPoint(imageView.frame, tapPoint)){ 

     //your code 

    } 

} 
+0

這樣做需要一些工作。如果可能,儘量保持框架與其他邊界分開。如果你現在絕對需要他們,你將不得不這樣做。 – n00bProgrammer

0

使用IBOutlet鏈接你的形象:

@property (nonatomic, weak) IBOutlet UIImageView *background; 

,並命名爲你的財產,沒有下劃線_,並使用駝峯你的財產。

此代碼的工作對我來說:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationNameTapped:)]; 
    tapGestureRecognizer.numberOfTapsRequired = 1; 
    _background.userInteractionEnabled=YES; 
    [_background addGestureRecognizer:tapGestureRecognizer]; 
} 

- (void)locationNameTapped:(UITapGestureRecognizer *)gesture 
{ 
    NSLog(@"image tapped"); 
} 
+1

是不是正確的方法簽名:' - (void)locationNameTapped:(UITapGestureRecognizer *)gesture'?用'@selector(locationNameTapped:)'? – Amar

0

如果你有太多的圖像爲他們設置的標籤和addding

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 


     UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationNameTapped:)]; 
     tapGestureRecognizer.numberOfTapsRequired = 1; 
     _background.userInteractionEnabled=YES; 
     _background.tag = 0; 
     [_background addGestureRecognizer:tapGestureRecognizer]; 
    } 

    - (void)locationNameTapped:(UITapGestureRecognizer*)gesture 
    { 
      if(gesture.view.tag == 0) 
     { 
         NSLog(@"image tapped"); 

     } 
    } 
0

可能是一些其他的看法是在你UIImageView ..in的情況下,只需禁用上層視圖的用戶交互,這樣潛在的視圖將獲得觸摸事件

0

你的代碼是正確的,但是如何添加UIImageView它的父視圖或其他視圖覆蓋它試試這個代碼,如果另一種觀點是在它

[parentView bringSubviewToFront:childView]; 

或檢查這增加了其父母查看得當與否