2010-12-13 60 views
6

我有幾個UIImageView,他們每個人都有一個標籤;我有一個圖像陣列,我想要做的是:當用戶點擊其中一個UIImageView時,應用程序將從數組中返回特定的圖像。如何獲取我正在點擊的UIImageView的標籤?

我實現這樣的:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; 
    [self.view addSubview:scroll]; 

    NSInteger i; 
    for (i=0; i<8; i++) 
    { 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)]; 
     imageView.backgroundColor = [UIColor blueColor]; 
     imageView.userInteractionEnabled = YES; 
     imageView.tag = i; 

     NSLog(@"%d", imageView.tag); 

     [scroll addSubview:imageView]; 

     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(findOutTheTag:)]; 
     [imageView addGestureRecognizer:tap]; 

    } 

    scroll.contentSize = CGSizeMake(320, 115*i); 

} 
- (void)findOutTheTag:(id)sender 
{ 

    // HOW TO FIND THE tag OF THE imageView I'M TAPPING? 

} 

我想找出imageView.tag,並通過imageView.tag

UIImageView *tappedImage = [imageArray objectAtIndex:imageView.tag]; 

顯示圖像。

我做了所有標記,問題是我如何才能找出我正在點擊的imageView的tag?感謝您的閱讀^ _^

回答

12

爲了避免看到應用程序的完整畫面而提出建議,爲什麼不使用自定義UIButton而不是UIImageView?使用UIButton,您可以設置一個操作並傳遞發件人ID,從中您可以輕鬆訪問您的標籤並從數組中提取數據。

或者如果你真的想用上面的代碼和你知道一個事實 - (空)findOutTheTag:(ID)發送方法被調用,所有你需要做的是:

- (void)findOutTheTag:(id)sender { 
    switch (((UIGestureRecognizer *)sender).view.tag)  
{ 
    case kTag1: 
    //... 
    case kTag2: 
    //... 
    } 
} 
+1

這裏的發送者是手勢識別器,所以你實際上必須做'switch((((UIGestureRecognizer *)sender).view.tag)''。 – Anna 2010-12-13 02:49:50

+0

@aBit明顯好起來! – Rog 2010-12-13 02:56:25

+3

+1 - (void)childTapped :(UITapGestureRecognizer *)tapGesture {int tag = tapGesture.view.tag;} //爲我工作 – HDdeveloper 2013-01-13 12:31:58

2

爲什麼不使用UIButton而不是使用UIImageView?這樣你可以簡單地爲UITouchDown事件添加一個偵聽器。您可以標記每個按鈕,以便在touchDown方法中可以找到按下哪個按鈕。

UIButton *button = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*100 + i*15, 300, 100)]; 
    button.backgroundColor = [UIColor blueColor]; 
    button.tag = i; 
    [button addTarget:self action:@selector(touchDown:) controlEvent:UIControlEventTouchDown]; 

而且着陸內:方法你只需要發送方轉換爲一個UIButton以訪問標籤。

- (void)touchDown:(id)sender 
{ 
    UIButton* button = (UIButton*)sender; 
    switch(button.tag) 
    { 
     case TAG1: 
      break; 
     //etc 
    } 
} 
+0

謝謝,DHamrick,我試過UIButton,而不是UIImageView,它完全輸出標籤,確實幫助我跳出我自己的想法,但使用UIButton可能會導致實現大量手勢來實現其他手勢,例如longPress和滑動我需要使用的移動/刪除viewItem(也是標記和數組),在我的構建中它可能會變得複雜,因爲UIGestureRecognizer已經擁有了它們,所以我決定使用它們,無論如何,非常感謝您的幫助,我會將該應用程序發送給您。^_ ^ – flutewang 2010-12-13 04:49:44

1

要找出哪個圖像必須是觸摸,使用touchBegan方法:

注:所有你需要確認有關圖像視首先應該是userIntrectionEnabled=YES; 現在用這個方法:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{ 
    // get touch event 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint touchLocation = [touch locationInView:self.view]; 
    if ([touch view].tag == 800) { 

    //if image tag mated then perform this action  
    } 
} 

您可以使用switch語句touchBegan