检测在Gesture Recognizer对象区域之外开始的滑动

时间:2013-05-19 14:38:08

标签: ios xcode uigesturerecognizer uiswipegesturerecognizer

我试图以非常具体的方式检测滑动,并且结果好坏参半。我的第一个问题是仅在屏幕的某个区域识别滑动,现在已经解决了。我目前的问题是这样的:假设我的视图中有一个事物列,并且我将该列的维度与我的手势识别器相对应。使用该应用时,滑动必须在这些尺寸范围内开始,才能被识别为滑动。相反,即使用户开始在列的区域外滑动,我也需要识别滑动。事实上,我甚至不关心它是否是滑动,即使是水龙头或其他东西都很好,我只是想知道用户的手指是否曾经在该列中(并且在这种情况下,他们肯定会开始从在列的区域之外)。

这是我当前的代码,但是滑动必须在列中开始:

- (IBAction)swiperMethod:(UISwipeGestureRecognizer *)sender {
    CGPoint point = [sender locationInView:self.view];
    if(point.y < 316 && point.y > 76 && point.x < 234 && point.x > 164)
    {
        _sampleText.text=@"hi";
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiperMethod:)];
    [leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
    [[self view] addGestureRecognizer:leftRecognizer];
    // Do any additional setup after loading the view, typically from a nib.
}

即使用户只是将他/她的手指拖过这个区域,有人可以告诉我如何进行滑动(或任何手势,我不关心它特别是滑动)吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

此方法有效!

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint point = [touch locationInView:touch.view];
    if(point.y < 316 && point.y > 76 && point.x < 234 && point.x > 164)
    {
        _sample.image = [UIImage imageNamed:@"normal.png"];
    }
    else
    {
        _star.image = [UIImage imageNamed:@"alternate.png"];
    }

}