2010-12-07 107 views
1

我需要做這樣的事情:UIScrollView中有作爲的UIImageView子視圖,盤帶的UIImageView

的UIScrollView用的UIImageViews作爲子視圖。當用戶點擊UIImageViews時會發生一個動作。但是當用戶想要滾動UIScrollView時,即使在UIImageView(在顯示UIImageView的UIImage的位置)開始滾動,也應該滾動。

Basicly我能得到的兩種情況之一:

  1. ,我可以得到,如果用戶點擊的UIImageView(這是UIScrollView的子視圖)的動作出現,但是當您嘗試通過從draging手指滾動UIImageView的行爲也發生(我想要滾動發生)。

  2. 我可以讓用戶點擊視圖的角度滾動,但如果用戶點擊UIImageView的行動將不會發生。

我不能讓你我的任何代碼,因爲我在這裏和那裏測試了很多aprroches的,它是有點亂所以這將是根本沒有使用(沒有評論噸的)。

是否有乾淨和簡單的解決方案呢?

確定這裏是一些代碼:

-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 


if(isDragging == NO) 
{ 


    return [super hitTest:point withEvent:event]; 
} 
NSLog(@"dragging ><><><><><>>><><><><>"); 
     return nil; 

} 

現在,如果我回到零,然後我可以滾動,但我不能去挖掘我的UIImageView的一個動作。如果我返回[超級hitTest:point withEvent:event]我無法滾動我的UIIMageView。

isDragging是測試代碼,以確定我是否嘗試滾動或只是點擊。但是在我可以根據正在發生的事件設置isDragging屬性之前進行命中測試。

這是我的init

-(id) initWithCoder:(NSCoder *)aDecoder 
{ 

if(self = [super initWithCoder:aDecoder]) 
{ 
    [self setUserInteractionEnabled:YES]; 
    UISwipeGestureRecognizer *swipeRecLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)]; 
    swipeRecLeft.direction = UISwipeGestureRecognizerDirectionDown; 

    UISwipeGestureRecognizer *swipeRecRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)]; 
    swipeRecRight.direction = UISwipeGestureRecognizerDirectionUp; 

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSingle)];  

    [self addGestureRecognizer:swipeRecRight]; 
    [self addGestureRecognizer:swipeRecLeft]; 
    [self addGestureRecognizer:singleTap]; 


    [swipeRecLeft release]; 
    [swipeRecRight release]; 
    [singleTap release]; 

    isDragging = NO; 
} 
else { 
    self = nil; 
} 

return self; 

}

下面是動作的其餘

-(void) tapSingle 

{ [self.delegate hitOccur]; }

-(void) swipe 
{ 
    isDragging = YES; 
} 

而且在我的UIScrollView我已經設置好的代表它的頂部和滾動結束時我將在我的UIScrollView的子視圖每個manualy isDragging屬性爲NO。

它的工作......但它並不完美。要實際滾動內容,我必須在UIImageView中刷TWICE(第一個是將isDragging設置爲YES,然後我們可以滾動...)。如何正確和恰當?

最新更新:

好吧我已經設法解決這個問題。然而,我很確定我的方式不乾淨或者不好(但是它的工作原理)。

在我的UIScrollView子類,我overided則hitTest方法與此:

-(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 

    if(!self.dragging) 
    { 
     if([[super hitTest:point withEvent:event] class] == [ResponsiveBookView class]) 
     { 
      container = [super hitTest:point withEvent:event]; 
     } 
    } 
    return self; 

} 

當容器ID容器中,它擁有我的UIView子類。因此,我可以識別觸摸是在我的圖像上還是在滾動視圖本身上。現在我需要DETEC如果是滾動或剛剛接觸,我這這裏做:

-(void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{ 

    if (!self.dragging) { 
     NSLog(@"touch touch touch"); 
     [container tapSingle]; 
     [self.nextResponder touchesEnded: touches withEvent:event]; 
    }  


    [super touchesEnded: touches withEvent: event]; 
} 

正如你看到的,如果self.dragging(滾動)的默認行爲將適用。如果!self.dragging我會手動調用tapSingle在我的容器上(這會使一個動作「發生」)。有用!

回答

0

是的,使用手勢識別器。如果您將UITapGestureRecognizer添加到每個UIImageView(使用UIVIew-addGestureRecognizer:方法),您應該同時識別水龍頭和默認滾動行爲。

+0

好的,非常感謝。你能告訴我如何識別手勢(刷卡我想)與給定的事件(UIEvent)?一個簡單的代碼就足夠了。 – Ertai 2010-12-07 15:02:23

相關問題