2

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

这是我当前的代码:

-(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 语句中的位能够拾取从下到上的滑动。我怎样才能做到这一点并消除所有其他滑动?

4

1 回答 1

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
    // ....
于 2013-02-02T13:06:51.650 回答