2016-03-25 44 views
0

我有一個垂直的UIScrollView,它有幾個大按鈕佔據了滾動視圖中的大部分空間。出於這個原因,我不得不繼承下面這篇文章ios 8 - buttons in horizontal scroll view intercepting pan event - scroll does not work滾動視圖),並重寫下列類方法:子類UIScrollView檢測按鈕內的觸摸,但對用戶交互無響應

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view 
{ 
    static int count = 0; 
    count++; 
    NSLog(@"calling this function 2"); 
    NSLog(@"count = %d", count); 
    UITouch *touch = [touches anyObject]; 

    if(touch.phase == UITouchPhaseMoved) 
    { 
     NSLog(@"UITouchPhaseMoved so returning no"); 
     return NO; 
    } 


    else 
    { 
     NSLog(@"returning super"); 
     NSLog(@"returning whatever this value is %d 0/1", [super touchesShouldBegin:touches withEvent:event inContentView:view]); 
     [super touchesShouldBegin:touches withEvent:event inContentView:view]; 
     return [super touchesShouldBegin:touches withEvent:event inContentView:view]; 
    } 
} 

當我運行我的應用程序,當按鈕被竊聽這上面的函數被調用,但功能當我在滾動視圖中的任何地方,在按鈕內部或外部拖動時,不會調用它。無論我在哪裏拖動,滾動視圖都不會滾動。

這裏是我的所有相關滾動視圖代碼(它的成立與IB):

  smallView.scrollEnabled = YES; 
      smallView.contentSize = CGSizeMake(smallView.frame.size.width, smallView.frame.size.height*1.4); 
      smallView.delegate = self; 

而這裏的情況下的按鈕相關的代碼的事項:

NSArray *buttonsArray = @[button1, button2, button3, button4, button5]; 
for (int i = 0; i < [buttonsArray count]; i++) { 
    UIButton *button = buttonsArray[i]; 
    button.tag = i+1; 
    [button addTarget:self action:@selector(p_motivationButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; 
    button.titleLabel.textAlignment = NSTextAlignmentCenter; 
} 

爲什麼我的滾動視圖不滾動,甚至不打電話touchesShouldBegin

+0

Ÿü沒有返回時touchphase移動? –

+0

@TejaNandamuri我不知道,這可以改變,但函數永遠不會進入邏輯的那個分支。 – helloB

+0

你在哪裏實施觸摸移動的方法?觸摸開始方法不能攔截觸動的事件。它只會檢測觸摸事件的起點。 –

回答

0

子類UIScrollView並重寫touchesShouldCancelInContentView()如下:

override func touchesShouldCancelInContentView(view: UIView) -> Bool { 
    if let _ = view as? UIControl { 
     return true 
    } 
    else { 
     return super.touchesShouldCancelInContentView(view) 
    } 
}