【问题标题】:How to Implement Touch Up Inside in touchesBegan, touchesEnded如何在 touchesBegan、touchesEnded 中实现 Touch Up Inside
【发布时间】:2012-10-11 00:40:27
【问题描述】:

我想知道当用户在touchesBegantouchesEnded 方法中按下然后抬起手指时,是否有人知道如何实现“内部修饰”响应。我知道这可以用UITapGestureRecognizer 来完成,但实际上我正在努力让它只在快速点击时才能工作(UITapGestureRecognizer,如果你把手指放在那里很长时间,然后抬起,它仍然执行)。有人知道如何实现吗?

【问题讨论】:

    标签: iphone ios cocoa-touch uigesturerecognizer


    【解决方案1】:

    使用UILongPressGesturizer 实际上是模拟UIButtontouchUpInsidetouchUpOutsidetouchDown 等)所有功能的更好解决方案:

    - (void) longPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer
    {
        if (longPressGestureRecognizer.state == UIGestureRecognizerStateBegan || longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)
        {
    
            CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];
    
            if (CGRectContainsPoint(self.bounds, touchedPoint))
            {
                [self addHighlights];
            }
            else
            {
                [self removeHighlights];
            }
        }
        else if (longPressGestureRecognizer.state == UIGestureRecognizerStateEnded)
        {
            if (self.highlightView.superview)
            {
                [self removeHighlights];
            }
    
            CGPoint touchedPoint = [longPressGestureRecognizer locationInView: self];
    
            if (CGRectContainsPoint(self.bounds, touchedPoint))
            {
                if ([self.delegate respondsToSelector:@selector(buttonViewDidTouchUpInside:)])
                {
                    [self.delegate buttonViewDidTouchUpInside:self];
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过创建 UIView 子类并在那里实现它来实现 touchesBegan 和 touchesEnded。

      但是,您也可以使用 UILongPressGestureRecognizer 并获得相同的结果。

      【讨论】:

        【解决方案3】:

        我通过在 touchesBegan 中放置一个触发的计时器来做到这一点。如果在调用 touchesEnded 时此计时器仍在运行,则执行您想要的任何代码。这给出了 touchUpInside 的效果。

          -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
            {
                NSTimer *tapTimer = [[NSTimer scheduledTimerWithTimeInterval:.15 invocation:nil repeats:NO] retain];
                self.tapTimer = tapTimer;
                [tapTimer release];
            }
        
            -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
            {
                if ([self.tapTimer isValid])
                {
        
                }
            }
        

        【讨论】:

        • 这真是聪明又容易理解。
        【解决方案4】:

        我不确定它是何时添加的,但属性isTouchInside 是任何UIControl 派生对象(例如UIButton)的救命稻草。

        override func endTracking(_ touch: UITouch?, with event: UIEvent?) {
            super.endTracking(touch, with: event)
            if isTouchInside {
                // Do the thing you want to do
            }
        }
        

        这是Apple official docs

        【讨论】:

          【解决方案5】:

          您可以创建一些BOOL 变量,然后在-touchesBegan 中检查触摸了什么视图或您需要的任何内容,并将此BOOL 变量设置为YES。之后在-touchesEnded 中检查此变量是否为YES,并且您的视图或您需要的任何内容都被触及,这将是您的-touchUpInside。当然,在之后将BOOL 变量设置为NO

          【讨论】:

            【解决方案6】:

            您可以添加UTapGestureRecognizerUILongPressGestureRecognizer 并使用[tap requiresGestureRecognizerToFail:longPress]; 添加依赖项(点击和长按是添加识别器的对象)。

            这样,如果触发长按,将不会检测到点击。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-18
              • 2012-08-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多