iOS - 计算滑动

时间:2015-07-02 17:38:48

标签: ios objective-c cocoa-touch

我正在尝试计算左侧滑动动作。但是,到目前为止,我的代码结果总是为1。

任何想法为什么?

- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
    int countLeft = 0;

    if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
    {

        countLeft += 1;
        imagesequence = @"2.png";
        [_MainBackground setImage:[ UIImage imageNamed: imagesequence]];

         NSLog(@"LEFT = %d", countLeft);
    }


    if (sender.direction == UISwipeGestureRecognizerDirectionRight)
    {
        NSLog(@"RIGHT");
        imagesequence = @"3.png";
        [_MainBackground setImage:[ UIImage imageNamed: imagesequence]];
    }
}

1 个答案:

答案 0 :(得分:1)

问题是int countLeft = 0;是在方法范围内定义的,因此每次运行时countLeft都已定义并初始化为0,您需要将其提升为更广泛的范围,以保持最新的执行值(移动定义在方法之外)。