2011-01-09 77 views
1

我有一個滾動視圖,其中添加了圖像作爲子視圖。我需要能夠選擇或點擊圖像。換句話說,捕獲點擊圖像的名稱。請參閱下面的代碼。在UIScrollview中選擇特定圖像

for (j = 1; j <= 12; j++) 
{ 
    NSString *imageName = [NSString stringWithFormat:@"%@",[months objectAtIndex:j-1]]; 
    NSLog(@"imagename is %@",imageName); 
    UIImage *image = [UIImage imageNamed:imageName]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 


    CGRect rect = imageView.frame; 

    rect.size.height = smallScrollObjHeight; 
    rect.size.width = smallScrollObjWidth; 
    imageView.frame = rect; 
    imageView.tag = j; 
    [smalltapRecognizer setNumberOfTapsRequired:1]; 
    [smalltapRecognizer setDelegate:self]; 
    [imageView addGestureRecognizer:smalltapRecognizer]; 
    [smalltapRecognizer release];*/ 
    NSLog(@"tag value is %d ",imageView.tag); 
    [monthscroll addSubview:imageView]; 
    [imageView release]; 
} 


[self layoutsmallScrollImages]; 
UITapGestureRecognizer *smalltapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smalltapped:)]; 
[smalltapRecognizer setNumberOfTapsRequired:1]; 
[smalltapRecognizer setDelegate:self]; 
[monthscroll addGestureRecognizer:smalltapRecognizer]; 
[smalltapRecognizer release]; 

// ----------------------------

- (無效)smalltapped:(ID)發件人 { NSLog(@「tapped」); //需要知道如何從這裏開始。 }

我的程序檢測到水龍頭,並打印taptap日誌在smalltapped函數。我需要知道如何捕獲選定或點擊的圖像的名稱。

回答

1

爲什麼不直接在scrollView中使用UIButtons呢?這樣你就可以使用標準的UIButton方法,不需要用手勢識別器來打擾,並且你可以看到被按下的按鈕上的圖像。

+0

聽起來像個好主意,明天會試試,讓你知道 – Rajashekar 2011-01-09 18:22:24

0

副每次的UIImageView實例的唯一標記值:

在viewDidLoad中或其他地方你的圖像被初始化:

myImageView1.tag = 1; 
myImageView2.tag = 2; 
myImageView3.tag = 3; 

然後嘗試手勢或類似的東西:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event  
{ 
UITouch *touch = [[event allTouches] anyObject]; 
int t = [touch view].tag; 
UIImageView *imageView = (UIImageView *) [self.view viewWithTag:t]; 
//your logic here since you have recognized the imageview on which user touched 
} 

避風港之前沒有嘗試過這樣的事情,但它應該可以工作。

編輯:我複製粘貼此代碼從我的答案在另一個類似的問題here 因此,請根據您的要求調整條件。