简体   繁体   English

当我向 UIScrollView 添加一个 UIPangesturerecognizer 时,滚动被禁用

[英]Scrolling is disabled when i add A UIPangesturerecognizer to UIScrollView

I have a scroll view and i want to make it disappear whenever a pan is done.我有一个滚动视图,我想让它在平底锅完成时消失。 It works fine and on panning , scrollview disappear but the problem is now i cant scroll the contents.它工作正常,在平移时,滚动视图消失,但问题是现在我无法滚动内容。

[UIView animateWithDuration:0.2
                      delay:0.2
                    options: UIViewAnimationCurveLinear
                 animations:^{
                     slideView.frame=CGRectMake(268, 0, 500, 950);
                     curtain.frame=CGRectMake(0, 0, 268, 950);
                     curtain.backgroundColor=[[UIColor alloc]initWithRed:0 green:0 blue:0 alpha:0.6];
                     [self.view addSubview:slideView];
                    [self.view addSubview:curtain];

                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Done!");
                 }];

this is how i how i make my uiscrollview to appear (slideview is scrollview).And then I add a pangesturerecognizer.All works fine but scrolling is disabled.这就是我如何让我的 uiscrollview 出现(slideview 是 scrollview)。然后我添加一个 pangesturerecognizer。一切正常,但滚动被禁用。 (panImage again hides my slideview.)how to make scrolling works .?? (panImage 再次隐藏了我的幻灯片视图。)如何使滚动工作.??

[slideView addGestureRecognizer:panImage];

UIScrollView has its own pan gesture recognizer, which controls scrolling. UIScrollView有自己的平移手势识别器,它控制滚动。 By adding another pan gesture recognizer, you are preventing the scroll view's own pan gesture recognizer from working.通过添加另一个平移手势识别器,您将阻止滚动视图自己的平移手势识别器工作。

There are a number of ways to deal with this, but it would be helpful if you could explain how the system is supposed to know when the user is trying to dismiss the scroll view, and when he is trying to scroll, since you want a pan gesture to do both.有很多方法可以解决这个问题,但是如果您能解释系统应该如何知道用户何时尝试关闭滚动视图以及他何时尝试滚动,那将会很有帮助,因为您想要一个平移手势同时执行。

For example, you could set the delegates of both gesture recognizers to allow the recognizers to operate simultaneously (by overriding the gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: method).例如,您可以设置两个手势识别器的委托以允许识别器同时操作(通过覆盖gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:方法)。 You could make each gesture recognizer require a different number of touches (by setting the minimumNumberOfTouches and maximumNumberOfTouches properties).您可以使每个手势识别器需要不同的触摸次数(通过设置minimumNumberOfTouchesmaximumNumberOfTouches属性)。 You could use a UISwipeGestureRecognizer to recognize the dismiss gesture.您可以使用UISwipeGestureRecognizer来识别关闭手势。 You could detect when the user tries to scroll past the left edge of the scroll view by overriding the scrollViewDidScroll: method of the scroll view's delegate.您可以通过覆盖滚动视图委托的scrollViewDidScroll:方法来检测用户何时尝试滚动超过滚动视图的左边缘。

You can set priorities of your panGestureRecognizers manually.您可以手动设置 panGestureRecognizers 的优先级。 This is how I solved the same problem: in delegate (BOOL)gestureRecognizerShouldBegin: I made scrolling disable in my scrollView if it should return YES.这就是我解决相同问题的方法:在委托 (BOOL)gestureRecognizerShouldBegin 中:如果应该返回 YES,我在我的 scrollView 中禁用了滚动。 And in the beginning of a selector of my panGestureRecognizer I made scrolling enabled.在 panGestureRecognizer 的选择器的开头,我启用了滚动。

In this delegate you can create a condition of your panGestureRecognizer to enable such a way, that it will be work if and only if you want it to have a major priority.在此委托中,您可以创建 panGestureRecognizer 的条件以启用这种方式,当且仅当您希望它具有主要优先级时,它才会起作用。

Swift method,迅捷的方法,
Need to add UIGestureRecognizerDelegate to your class:需要将 UIGestureRecognizerDelegate 添加到您的类中:

class ViewController: UIViewController, UIGestureRecognizerDelegate {

Need to set its delegate to self:需要将其委托设置为 self:

override func viewDidLoad() {
    super.viewDidLoad()
    var scrollView = UIScrollView(frame: self.view.frame)
    scrollView.delegate = self
}

Add this part in your class methods to active simultaneous gestures:将此部分添加到您的类方法中以激活同步手势:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

当我在scrollView顶部有一个视图并且我需要添加一个手势识别器以便在视图中滚动它时,我做了这样的事情(放置在viewDidAppear方法中):

  hostedView.addGestureRecognizer(scrollView.panGestureRecognizer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM