2012-08-09 30 views
0

根據此questionUIGestureRecognizer具有view屬性,該屬性指的是手勢所附的視圖。我用這個在我的代碼是這樣的:UIGestureRecognizer的發件人視圖無法正常工作

//Code for the 1st UIScrollView 
UIImageView *bookCover = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 145, 420)]; 
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self 
action:@selector(downloadBookTapped:)]; 
[bookCover addGestureRecognizer:singleTap]; 

[bookCover release]; 
[singleTap release]; 

//Code for the second UIScrollView 
UIImageView *fileCover = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 145, 420)]; 
UITapGestureRecognizer *singleFileTap = [[UITapGestureRecognizer alloc]initWithTarget:self 
action:@selector(downloadFileTapped:)]; 
[fileCover addGestureRecognizer:singleFileTap]; 

[fileCover release]; 
[singleFileTap release]; 

這裏是我用戶view屬性:

- (void)downloadBookTapped:(UITapGestureRecognizer *)sender 
{ 
    UIImageView *imgView = (UIImageView *)sender.view; 

    CGRect rect = [imgView frame]; 

    UIImageView *images = [[UIImageView alloc]initWithFrame:rect]; 

    //rest of code here... 
} 

- (void)downloadFileTapped:(UITapGestureRecognizer *)sender 
{ 
    UIImageView *imgView = (UIImageView *)sender.view; 

    CGRect rect = [imgView frame]; 

    UIImageView *images = [[UIImageView alloc]initWithFrame:rect]; 

    //rest of code here... 
} 

這裏的問題是,我有兩個scrollView和每個滾動視圖持有多本書籍。當我在第一張scrollView上選擇一本書時,images顯示正確。但是當我在第二張scrollView內選擇一本書時,images顯示不正確。誰能解釋爲什麼會發生這種情況?謝謝。

---附加信息---

兩個scrollViews具有相同的寬度和高度。當然,差異在於放置。第一個scrollView放置在(0,0),而第二個在(0,350)。你可以將這兩個想象爲「貨架」,第一個是最高的貨架,第二個是最低的貨架。

要指定問題,請說我在第二個scrollView中選擇了一本書。 images然後將顯示好像我在第1個scrollView中選擇了一本書。意思是,images顯示在第一個scrollView而不是第二個scrollView中。

回答

0

我現在知道我做錯了什麼!相反,加入images作爲scrollViews的子視圖的,我這樣做:

[self.view addSubView:images]; 

這就是爲什麼它不斷出現在頂側。它應該是這樣的:

[scrollBook addSubview:images]; 
[scrollFile addSubView:files]; 
1

因爲gestureRecognizer綁定到第一個UIImageView而不是第二個。

[bookCover addGestureRecognizer:singleTap]; 

爲您的其他UIImageView做到這一點,你會得到你想要的結果。

+0

正確的發現 – 2012-08-09 06:05:13

+0

糟糕!我已經有了!我只是忘了在上面的代碼中添加它。我會編輯它來添加這個。 – 2012-08-09 06:05:41