2012-05-20 37 views
0

我在UIScrollview裏面有TTImagViews的問題。我搜索高低,但coul'dnt真的找到一個解決方案。有趣的是,點擊手勢在滾動視圖中的最後一個TTImageview上工作。例如,我有10個圖像,用戶可以滾動,並且觸摸手勢僅適用於第2頁上的第10個圖像,或者更確切地說,最後一個圖像。這是我的代碼;有什麼建議麼?UIScrollview裏面的TTImageView沒有收到觸摸

UIScrollView *imageScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(70, postMessageLabel.frame.size.height+10, 250, 78)]; 
    [imageScroll setContentSize:CGSizeMake(70 * ([[images objectAtIndex:0]count])+10,64)];   
    int startAtX=5; 
    for(int i=0;i<[[images objectAtIndex:0]count];i++){ 
     if([[GlobalFunctions sharedGlobalFunctions] isValidURL:[[images objectAtIndex:0] objectAtIndex:i]]){ 
      TTImageView *imageView=[[TTImageView alloc] initWithFrame:CGRectMake(startAtX, 5, 64, 64)] ; 
      imageView.userInteractionEnabled=YES; 
      [imageView addGestureRecognizer:thumbnailTap];     
      imageView.urlPath=[[images objectAtIndex:0] objectAtIndex:i]; 
      imageView.autoresizesToImage=NO; 
      imageView.defaultImage=nil; 
      imageView.delegate=self;  
      [imageView setBackgroundColor:[UIColor blackColor]]; 
      [imageScroll addSubview:imageView]; 
      [imageView release]; 

     } 
      startAtX+=70;    
    } 

    [imageScroll setBounces:YES]; 
    [imageScroll setDelaysContentTouches:YES]; 
    [imageScroll setCanCancelContentTouches:NO]; 
    [self.view addSubview:imageScroll]; 
    [imageScroll release]; 

,是的,敲擊手勢完美的作品如果只有一個UIScrollView的內部ttimageview。我不知道爲什麼!

回答

0

嗯,看起來就像有什麼錯我的代碼;唯一的區別是我如何附加手勢識別器。即使我修好了,我不確定它爲什麼出錯。這是發生了什麼

  1. 我已經在我的viewdidload中用典型的alloc init聲明瞭手勢識別器。
  2. 因此,我認爲,通過視圖可以使用手勢識別器。我只是不斷將它添加到一個圖像循環。

步驟2;似乎是問題所在。當我爲每個圖像聲明一個手勢識別器並在分配後釋放它時,問題就消失了。

因此從技術上爲每個圖像

---Create a gesture recognizer 
    |-Attach it to the TTImageView 
---Release the gesture recognizer 

這解決了這個問題。但我仍然好奇,爲什麼手勢識別器在分配給單個圖像和圖像時工作,而當我將其分配給多個圖像瀏覽時失敗。

感謝阿卜杜拉嘗試,但你的解決方案讓我無處可去。

0

在UIView中添加圖像視圖,然後將輕擊手勢添加到該UIView。這對我有用,也將爲你工作。請嘗試以下:

UIScrollView *imageScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(70, postMessageLabel.frame.size.height+10, 250, 78)]; 
    [imageScroll setContentSize:CGSizeMake(70 * ([[images objectAtIndex:0]count])+10,64)];   
    int startAtX=5; 
    for(int i=0;i<[[images objectAtIndex:0]count];i++){ 
     if([[GlobalFunctions sharedGlobalFunctions] isValidURL:[[images objectAtIndex:0] objectAtIndex:i]]){ 
      TTImageView *imageView=[[TTImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)] ; 
      imageView.userInteractionEnabled=YES; 
      [imageView addGestureRecognizer:thumbnailTap];     
      imageView.urlPath=[[images objectAtIndex:0] objectAtIndex:i]; 
      imageView.autoresizesToImage=NO; 
      imageView.defaultImage=nil; 
      imageView.delegate=self;  
      [imageView setBackgroundColor:[UIColor blackColor]]; 

UIView *viewWithImg=[[UIView alloc] initWithFrame:CGRectMake(startAtX, 5, 64, 64)]; 
     [viewWithImg addSubview:imgView]; 
     viewWithImg.tag=i; 
     UITapGestureRecognizer *tapGesture2 = 
     [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured2:)];   
     tapGesture2.numberOfTapsRequired = 1; 
     tapGesture2.numberOfTouchesRequired=1; 
     [viewWithImg addGestureRecognizer:tapGesture2]; 

      [imageScroll addSubview: viewWithImg]; 
      [imageView release]; 

     } 
      startAtX+=70;    
    } 

    [imageScroll setBounces:YES]; 
    [imageScroll setDelaysContentTouches:YES]; 
    [imageScroll setCanCancelContentTouches:NO]; 
    [self.view addSubview:imageScroll]; 
    [imageScroll release]; 

和singleTapGestureCaptured2方法

-(void)singleTapGestureCaptured2:(UITapGestureRecognizer *)gesture 
{ 
    NSLog(@"image tag or arr index from images array: %d",[[gesture view] tag]);  
} 
+0

HI abdullah,謝謝你的迴應。但我認爲你沒有正確閱讀我的問題。我需要滾動的UIScrollview和異步加載的TTImageview。 – Veeru

+0

我想說的是:你應該創建UIView每次你創建imageView與圖像視圖的框架,然後你應該添加該UIView滾動而不是imageView,因爲imageView是在UIView和你的異步加載將工作沒有任何麻煩。 –

+0

嗨阿卜杜拉,我沒有看到解決方案的任何邏輯;但我確實嘗試過,有一個uiview,在uiview中添加ttimageview,然後將uiview添加到scrollview中;仍然一樣,你的解決方案不起作用。 – Veeru

相關問題