2013-04-25 118 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

相关问题