2011-09-16 34 views
26

我正在處理我的視圖控制器(UIViewController的自定義子類)中的幾個我的UI組件觸摸。它有方法touchesBegan:withEvent:,touchesMoved:withEvent:touchesEnded:withEvent:。它工作正常。然後我添加一個滾動視圖(UIScrollView)作爲層次結構中的頂視圖。UIScrollView防止touchesBegan,touchesMoved,touchesEnded視圖控制器

現在我的視圖控制器上的觸摸處理程序不起作用。他們不會被叫到。有趣的是,我在滾動視圖中有各種其他UI組件可以工作。一些是按鈕,一些是自定義視圖,它們定義了自己的touchesBegan:withEvent:等。唯一不起作用的是視圖控制器上的觸摸處理程序。

我想也許這是因爲滾動視圖攔截出於其自身目的的接觸,但我的子類的UIScrollView,只是看看,如果我能得到它的工作我從touchesShouldCancelInContentView:返回從touchesShouldBegin:withEvent:inContentView:YES總是和NO始終。仍然不起作用。

如果它有所不同,我的視圖控制器是在標籤欄控制器內,但我認爲它不相關。

有沒有人有這個問題,並有一個現成的解決方案?我的猜測是滾動視圖猴子響應鏈。我可以把它猴子回來嗎?我猜如果我不能找出其他東西,我會將我的滾動視圖下的頂層視圖作爲自定義視圖,並將消息轉發給視圖控制器,但似乎很枯燥。

+0

按照我的答案在這裏http://stackoverflow.com/questions/1685956/uiscrollview-touchesbegan/17759373#:

我通過改變泛手勢識別器的行爲,所以它需要兩個手指克服了這個17759373 – Rajneesh071

回答

0

「它工作的很好,然後在層次結構中添加了一個滾動視圖(UIScrollView)作爲頂視圖。」

是你的contentview頂部包含項目的滾動視圖?

所有組件應在滾動視圖,而不是滾動視圖

+0

頂層分層排列。在所有其他視圖中都是滾動視圖的子視圖。具體而言,有一個視圖是滾動視圖的子視圖,其他所有視圖都是子視圖或子視圖的子視圖等。 – morningstar

2

背後的觀點嗯,這工作,但我不知道我能「逃脫它」,因爲nextResponder不是UIView的一個您被「鼓勵」在子類中重寫的方法。

@interface ResponderRedirectingView : UIView { 

    IBOutlet UIResponder *newNextResponder; 

} 

@property (nonatomic, assign) IBOutlet UIResponder *newNextResponder; 

@end 


@implementation ResponderRedirectingView 

@synthesize newNextResponder; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (UIResponder *)nextResponder { 
    return self.newNextResponder; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

然後在界面生成器我提出的滾動視圖的其中之一的直接子視圖,和掛接其newNextResponder跳過滾動視圖和直接指向視圖控制器。

這工作也與這些覆蓋代替nextResponder的覆蓋:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.newNextResponder touchesBegan:touches withEvent:event]; 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.newNextResponder touchesMoved:touches withEvent:event]; 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.newNextResponder touchesEnded:touches withEvent:event]; 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.newNextResponder touchesCancelled:touches withEvent:event]; 
} 
-1

touchesBegan:等方法將NEVERUIScrollView叫,因爲它是UIView一個子類,並覆蓋這些方法。請查看可用的不同方法here。解決方法將取決於您要實施的內容。

+0

滾動視圖中未觸及的touchesBegan方法未被調用。他們在我的視圖控制器上。另一方面,在正在調用滾動視圖中的「in」視圖上的touchesBegan。這是完全相反的問題。他們不能從滾動視圖中「出」。 – morningstar

+0

我還沒有完全得到你的問題,雖然我想指出的touchesBegan方法將工作的視圖內的一個scrollView! – tipycalFlow

+0

我看了UIScrollView和UIScrollViewDelegate的文檔,並嘗試了可用的東西。我正在嘗試做的事情從簡單的輕擊手勢到突出顯示屏幕區域的觸摸的複雜解釋。我可以將它切換到手勢識別器,這可能會解決問題,但a)我不想重寫代碼b)我希望它運行在3.0沒有手勢識別器。 – morningstar

22

創建的UIScrollView類的子類,並重寫的touchesBegan:和其他觸摸方法如下:

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

// If not dragging, send event to next responder 
    if (!self.dragging){ 
    [self.nextResponder touchesBegan: touches withEvent:event]; 
    } 
    else{ 
    [super touchesBegan: touches withEvent: event]; 
    } 
} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

// If not dragging, send event to next responder 
    if (!self.dragging){ 
    [self.nextResponder touchesMoved: touches withEvent:event]; 
    } 
    else{ 
    [super touchesMoved: touches withEvent: event]; 
    } 
} 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

    // If not dragging, send event to next responder 
    if (!self.dragging){ 
    [self.nextResponder touchesEnded: touches withEvent:event]; 
    } 
    else{ 
    [super touchesEnded: touches withEvent: event]; 
    } 
} 
+2

這是安全的做到這一點的類別,而不是繼承? –

+0

謝謝,到目前爲止,我的類(從你的代碼)在我的應用程序上工作完美無瑕! –

0

user1085093的回答爲我工作。然而,一旦你將觸摸移動的次數不止一次,它就會被解釋爲一個Pan手勢。

-(void)awakeFromNib 
{ 
    NSArray     *gestureRecognizers = self.gestureRecognizers; 
    UIGestureRecognizer  *gestureRecognizer; 

    for (gestureRecognizer in gestureRecognizers) { 
     if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { 
      UIPanGestureRecognizer  *pgRecognizer = (UIPanGestureRecognizer*)gestureRecognizer; 
      pgRecognizer.minimumNumberOfTouches = 2; 
     } 
    } 

} 
相關問題