2013-04-25 44 views
3

我從UIScrollView創建了一個自定義子類,並實現了touchesBegan,touchesMoved,touchesEndedtouchesCancelled方法。是否可以自定義觸發UIScrollView滾動的滑動手勢識別?

但是我不滿意事情是如何工作的。特別是,何時提及方法被調用,以及UIScrollView何時決定實際滾動(拖動)。

UIScrollView即使第一觸摸點和最後一個觸摸點之間的差異在垂直方向上很小,也會滾動。所以,我幾乎可以水平滑動和UIScrollView是要向上或向下滾動取決於小的區別。(這是完全正常的在正常使用情況下)

Default UIScrollView behavior

這兩個揮動會導致UIScrollView向下滾動。

不過我很感興趣,將有可能以某種方式調整它,這樣它的行爲是這樣的:

Desired behavior

基本上使接近水平的重擊得到由touchesBegan拿起和相關方法和做不啓動滾動。綠色刷卡方向但仍引發滾動...

編輯:

我忘了提,touchesBegan,如果你把你的手指的時間的屏幕,然後移動在短時間內親戚被調用它周圍。因此,不是經典的滑動手勢...

回答

2

克里斯托弗·納瑟正確地指出,我應該使用UIPanGestureRecognizer,所以我嘗試了一下它。

我發現的是,如果您將UIPanGestureRecognizer添加到超級視圖其中包含UIScrollView。然後,內置在平移手勢識別器中的UIScrollView將按照我所希望的確切方式與您自己的UIPanGestureRecognizer配對工作!

水平和接近水平刷卡要由上海華和所有其他垂直那些由UIScrollView(自定義)建於泛手勢識別的UIPanGestureRecognizer被拾起,並使其滾動...

我想這UIScrollView已經這樣設計的,因爲默認的行爲是,只有一個這些平移手勢識別觸發,或兩者同時進行,如果是從這個UIPanGestureRecognizerDelegate方法UIScrollView回報:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer; 

然而似乎UIScrollView有另外的邏輯來選擇性地禁用(對於水平滑動)其自己的泛識別器以防另一個存在。

也許有人在這裏知道更多的細節。

所以總結起來的解決方案,我是在我的UIViewController添加UIPanGestureRecognizerviewDidLoad。(注:UIScrollView添加爲子視圖UIViewController視圖)

UIPanGestureRecognizer *myPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; 
[self.view addGestureRecognizer:myPanGestureRecognizer]; 

,然後添加處理方法:

- (void)handlePan:(UIPanGestureRecognizer *)recognizer 
{ 
    NSLog(@"Swiped horizontally..."); 
} 
2

伊萬,我認爲你正在嘗試做與Facebook頁面相同的效果,並拖動你的滾動視圖,所以讓scrollview跟着你的手指,如果這是正確的,我建議你忘了觸摸事件,並與UIPanGesture,開始其最好在這些情況下,使調用手勢委託裏面,把下面的代碼吧:

//The sender view, in your case the scollview 
    UIScrollView* scr = (UIScrollView*)sender.view; 
    //Disable the scrolling flag for the sake of user experience 
    [scr setScrollEnabled:false]; 

    //Get the current translation point of the scrollview in respect to the main view 
    CGPoint translation = [sender translationInView:self.view]; 

    //Set the view center to the new translation point 
    float translationPoint = scr.center.x + translation.x; 
    scr.center = CGPointMake(translationPoint,scr.center.y); 
    [sender setTranslation:CGPointMake(0, 0) inView:self.view]; 
+0

好點!我剛剛嘗試了Facebook應用程序,這似乎是..如果你向左或向右滑動,新聞提要(可能是一個UIScrollView)滑到一邊...我現在要嘗試你的建議。不過,我必須注意到,實際上我並沒有嘗試打開側面菜單,而是實際上改變了NSInteger變量(增加/減少)的值,涉及水平滑動或拖動... – 2013-04-25 09:23:08

相關問題