2

私は現在touchesMoved、ユーザーが上にスワイプできるようにするために使用しており、上にスワイプすればするほど、数が増えます。UIGestureRecognizerここでは、すべての動きが記録されるわけではなく、1回のスワイプのみが記録されるため、ここでは使用できないと思います。

これが私の現在のコードです:

-(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 に答える