如何只在touchesMoved上寻找向上滑动

时间:2013-02-02 12:28:18

标签: ios objective-c touch uigesturerecognizer touchesmoved

我目前正在使用touchesMoved允许用户向上滑动,向上滑动的次数越多,数量增加的幅度就越大。我不认为我可以在这里使用UIGestureRecognizer,因为这不会记录每一个动作 - 它只记录单次滑动。

这是我目前的代码:

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

    UITouch *touch = [touches anyObject];
    CGPoint gestureEndPoint = [touch locationInView:self.view];

    NSLog(@"START: %@  END: %@", NSStringFromCGPoint(gestureStartPoint), NSStringFromCGPoint(gestureEndPoint));

    if((gestureStartPoint.x - gestureEndPoint.x) < 20 && (gestureStartPoint.x - gestureEndPoint.x) > -20)
    {

        if((gestureStartPoint.y - gestureEndPoint.y) > (gestureStartPoint.x - gestureEndPoint.x))
        {

            if(gestureStartPoint.y > gestureEndPoint.y)
            {

                NSInteger integer =  (NSInteger)(gestureEndPoint.y - gestureStartPoint.y);

                NSString *string = [NSString stringWithFormat:@"%i", integer / 40];

                if([string intValue] && self.lastValue != [string intValue])
                {

                    [self.timer invalidate];

                    [self.updateCounter invalidate];

                    [self.secondsTimer invalidate];

                    self.lastValue = [string integerValue];

                    self.number = self.number + 1;

                    NSString *finalTime = [NSString stringWithFormat:@"%i", self.number];

                    if(self.number == 1)
                        finalTime = [finalTime stringByAppendingString:@" MINUTE"];

                    else
                        finalTime = [finalTime stringByAppendingString:@" MINUTES"];

                    [self.amountOfTime setText:finalTime];

                    lastPoint = gestureEndPoint;

                    NSLog(@"Setting timer for %i", self.number * 60);

                    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.number * 60 target:self  selector:@selector(countdownEnded) userInfo:nil repeats:NO];
                    self.updateCounter = [NSTimer scheduledTimerWithTimeInterval:60 target:self  selector:@selector(updateCounterText) userInfo:nil repeats:YES];

                    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
                    crossFade.duration = 2.0;
                    crossFade.fromValue = (__bridge id)([self.background.image CGImage]);
                    crossFade.toValue = (__bridge id)([[UIImage imageNamed:@"MainBackground"] CGImage]);
                    crossFade.removedOnCompletion = NO;
                    crossFade.fillMode = kCAFillModeForwards;
                    [self.background.layer addAnimation:crossFade forKey:@"animateContents"];

                }

            }

            else
            {

                if(self.number - 1 >= 0)
                {

                    [self.timer invalidate];

                    [self.updateCounter invalidate];

                    [self.secondsTimer invalidate];

                    self.number = self.number - 1;

                    NSString *finalTime = [NSString stringWithFormat:@"%i", self.number];

                    if(self.number == 1)
                        finalTime = [finalTime stringByAppendingString:@" MINUTE"];

                    else
                        finalTime = [finalTime stringByAppendingString:@" MINUTES"];

                    [self.amountOfTime setText:finalTime];

                    lastPoint = gestureEndPoint;

                    NSLog(@"Setting timer for %i", self.number * 60);

                    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.number * 60 target:self  selector:@selector(countdownEnded) userInfo:nil repeats:NO];
                    self.updateCounter = [NSTimer scheduledTimerWithTimeInterval:60 target:self  selector:@selector(updateCounterText) userInfo:nil repeats:YES];

                }

        else
            self.number = 0;

        }

    }

    }

}

问题是 - 如果您从左向右或从右向左或从上到下滑动,则数字会增加。我只想要if语句中的位来接受从下到上的滑动。我怎么能这样做并消除所有其他滑动?

1 个答案:

答案 0 :(得分:2)

喜欢这个吗?

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

    UITouch *touch = [touches anyObject];
    CGPoint gestureEndPoint = [touch locationInView:self.view];

    int dx = abs(gestureStartPoint.x - gestureEndPoint.x);
    int dy = -1 * (gestureEndPoint.y - gestureStartPoint.y);

    if(dx > 20) {
        // too much left/right, so don't do anything
        return;
    }

    if(dy < 0) {
        // they moved down
        return;
    }

    NSLog(@"Change in Y: %d", dy); // now do something with dy
    // ....