菜鸟教程小白 发表于 2022-12-13 06:53:57

ios - 滑动在 uiscrollview 中无法正常工作


                                            <p><p>我有一个名为 <code>templateView</code> 的 <code>UISrollView</code>。我必须向它添加一个滑动手势,以允许用户向左/向右滑动以查看另一个模板。问题是大多数时候用户不能轻松滑动,因为 View 向下/向上滚动而不是滑动到另一个 View 。他的手指需要严格水平对齐才能滑动到另一个页面,从用户体验的角度来看这是 Not Acceptable 。</p>

<p>知道如何处理这种情况吗?有没有办法实现检测滑动手势的角度?或者,有没有办法将其作为自定义手势来检测具有特定角度的斜线?</p>

<p>提前致谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>尝试实现 UIGestureRecognizer Delegate 方法。当gestureRecognizer 或otherGestureRecognizer 对手势的识别会阻止其他手势识别器识别其手势时,将调用此方法。请注意,返回 YES 保证允许同时识别。 </p>

<pre><code>- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
    shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
      return YES;
    }
</code></pre>

<p><strong>引用:</strong> <a href="https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizerDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIGestureRecognizerDelegate/gestureRecognizer%3ashouldRecognizeSimultaneouslyWithGestureRecognizer%3a" rel="noreferrer noopener nofollow">UIGestureRecognizer Protocol</a> </p>

<p>在初始化滑动手势时不要忘记分配委托(delegate)。</p>

<p><strong>更新 1</strong> 创建自己的手势</p>

<p>您始终可以继承 UIGestureRecognizer 类并实现 <code>touchesBegan</code>、<code>touchesMoved</code>、<code>touchesEnded</code> 方法 - 根据您自己的手动管理手势状态需要。</p>

<p>我发布了一些实现自定义 EdgeGestureRecognizer 的示例代码,以便您更好地理解。 </p>

<pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    ;
    UITouch *touch = touches.anyObject;
    CGPoint location = ;

    // if not single finger, then fail

    if ( != 1)
    {
      self.state = UIGestureRecognizerStateFailed;
      return;
    }
   //put here some logics for your case. For instance, you can register
   //here your first touch location, it will help
   //you to calculate the angle after.
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    ;

    if (self.state == UIGestureRecognizerStateFailed) return;

    UITouch *touch = touches.anyObject;
    self.previousPoint = self.currentPoint;
    self.previousPointTime = self.currentPointTime;
    self.currentPoint = ;
    self.currentPointTime = touch.timestamp;

    if (self.state == UIGestureRecognizerStatePossible)
    {
      CGPoint translate = CGPointMake(self.currentPoint.x - self.startPoint.x, self.currentPoint.y - self.startPoint.y);

      // see if we&#39;ve moved the necessary minimum distance

      if (sqrt(translate.x * translate.x + translate.y * translate.y) &gt;= self.minimumRecognitionDistance)
      {
            // recognize if the angle is roughly horizontal, otherwise fail

            double angle = atan2(translate.y, translate.x);

            if ()
                self.state = UIGestureRecognizerStateBegan;
            else
                self.state = UIGestureRecognizerStateFailed;
      }
    }
    else if (self.state == UIGestureRecognizerStateBegan)
    {
      self.state = UIGestureRecognizerStateChanged;
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 滑动在 uiscrollview 中无法正常工作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22426447/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22426447/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 滑动在 uiscrollview 中无法正常工作