本文介绍了如何实现触摸内部在touchesBegan,touchesEnded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果有人知道如何实现触摸内部响应,当用户按下然后举起他们的手指在 touchesBegan touchesEnded 方法。我知道这可以通过 UITapGestureRecognizer 来实现,但实际上我想让它只工作在一个快速点击( UITapGestureRecognizer ,如果你把你的手指在那里很长一段时间,然后提升,它仍然执行)。

实际上,使用 UILongPressGesturizer 一个更好的解决方案来模仿 UIButton touchUpInside touchUpOutside touchDown 等):

  - (void)longPress:(UILongPressGestureRecognizer *)longPressGestureRecognizer 
{
if(longPressGestureRecognizer.state == UIGestureRecognizerStateBegan || longPressGestureRecognizer.state == UIGestureRecognizerStateChanged)
{

CGPoint triggeredPoint = [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 triggeredPoint = [longPressGestureRecognizer locationInView:self];

if(CGRectContainsPoint(self.bounds,touchedPoint))
{
if([self.delegate respondingToSelector:@selector(buttonViewDidTouchUpInside :)])
{
[self.delegate buttonViewDidTouchUpInside:self];
}
}
}
}


I'm wondering if someone knows how to implement the "touch up inside" response when a user pushes down then lifts their finger in the touchesBegan, touchesEnded methods. I know this can be done with UITapGestureRecognizer, but actually I'm trying to make it so that it only works on a quick tap (with UITapGestureRecognizer, if you hold your finger there for a long time, then lift, it still executes). Anyone know how to implement this?

解决方案

Using the UILongPressGesturizer is actually a much better solution to mimic all of the functionality of a UIButton (touchUpInside, touchUpOutside, touchDown, etc.):

- (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];
            }
        }
    }
}

这篇关于如何实现触摸内部在touchesBegan,touchesEnded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:25