2010-03-10 62 views
1

所以基本上我試圖讓UIScrollView只能在更高角度上滾動。就像現在,如果將手指從水平移動10度,滾動視圖將滾動。我想把它推高到30度。通過編程將觸摸傳遞給UIScrollView滾動

做了一些閱讀之後,我建立了最好的方法來做到這一點,將一個子類UIView放在scrollview的頂部。如果頂部觸摸上的UIView高於30度,則將其傳遞給滾動視圖,否則不要。

但是,我無法弄清楚如何傳遞觸摸。這是我現在的代碼:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"glass touch began"); 
    UITouch *touch = [touches anyObject]; 
    beginning_touch_point = [touch locationInView:nil]; 
    [scroll_view touchesBegan:touches withEvent:event]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"glass touch ended"); 
    UITouch *touch = [touches anyObject]; 
    CGPoint previous_point = beginning_touch_point; 
    CGPoint current_point = [touch locationInView:nil]; 

    float x_change = fabs(previous_point.x - current_point.x); 
    float y_change = fabs(previous_point.y - current_point.y); 

    if(x_change > y_change) 
    { 
     if(previous_point.x - current_point.x < 0) 
     { 
      [(MyScheduleViewController *)schedule_controller didFlickLeft]; 
     } 
     else 
     { 
      [(MyScheduleViewController *)schedule_controller didFlickRight]; 
     } 

     [scroll_view touchesCancelled:touches withEvent:event]; 
    } 
    else 
    { 
     [scroll_view touchesEnded:touches withEvent:event]; 
    } 
} 

我現在知道它正在檢查45度,但那不是重要的。重要的是觸摸確實正確地傳遞給我的scroll_view。我已經在touchesbegan和touchesended上做了一個NSLog(),並且它正確地執行了這兩個操作。它只是不滾動。我擔心touchesBegan和touchesEnded不能導致滾動。有誰知道可以做什麼,或者我做錯了什麼?

謝謝

+1

你有沒有設法得到這個工作?我目前有類似的問題。 – 2010-10-16 10:35:28

回答

0

我也試圖做同樣的事情,但沒有成功。它似乎scrollView不處理touchesMoved/touchesBegan,但它處理一些其他事件,以瞭解用戶想要滾動視圖。 對我來說,解決方案是確定shift值並將其明確設置爲scrollView內容偏移量。它看起來像這樣(我沒有確切的源代碼否,此代碼可能不正確):

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 

    CGPoint curr = [touch locationInView: self]; 
    CGPoint offset = [scrollView contentOffset]; 

    [scrollView setContentOffset: CGPointMake(offset.x + (curr.x - prev.x), offset.y) animated:YES]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    prev = [[touches anyObject] previousLocationInView: self]; 
} 
相關問題