重复调用UIGestureRecognizer类时如何保留变量的值?

时间:2015-05-13 07:06:30

标签: ios objective-c uigesturerecognizer

我在图像视图上调用UIGestureRecognizer类,并且在调用UIGestureRecognizerStateChanged时设置相关标签的值。

如果第二次调用UIGestureRecognizerStateBegan,则会再次使用初始值设置相同标签的值。

即使在状态结束并重新开始后,我如何保留相同的值?

请让我知道......以下是代码,

-(void) longPress: (UIGestureRecognizer *) gestureRecognizer
{     
  NSInteger intValue;

 UIGestureRecognizerState state = [gestureRecognizer state];

switch (state) {
    case UIGestureRecognizerStateBegan: {
        NSLog(@"LongPress UIGestureRecognizerStateBegan");
        beginPositionlocation = [gestureRecognizer locationInView:self.view];
        NSLog(@"%f", beginPositionlocation.y);

        volumeLabelOnLongPress = nil;

        for (UIView* view in gestureRecognizer.view.subviews) {
            if (view.tag == kVolumeLblTag) {
                volumeLabelOnLongPress = (UILabel *)view;
            }
        }
        break;
    }
    case UIGestureRecognizerStateChanged: {

            endPositionlocation = [gestureRecognizer locationInView:self.view];

            NSLog(@"%f", beginPositionlocation.y);
            NSLog(@"%f", endPositionlocation.y);

                intValue = (NSInteger) roundf(beginPositionlocation.y-endPositionlocation.y);

        if (volumeLabelOnLongPress!=nil) {
            [volumeLabelOnLongPress setText:[NSString stringWithFormat:@"%ld",(long)intValue]];

              if ((intValue >= 10 ) && (intValue <=20)){
                [volumeLabelOnLongPress setText:[NSString stringWithFormat:@"%ld",(long)intValue]];

            }

              else if ((intValue >= 21 ) && (intValue <=30)){
                [volumeLabelOnLongPress setText:[NSString stringWithFormat:@"%ld",(long)intValue]];

            }
       }

        break;
    }
    case UIGestureRecognizerStateEnded: {
        NSLog(@"LongPress UIGestureRecognizerStateEnded");
       endPositionlocation = CGPointMake(0.0, 0.0);
        beginPositionlocation = CGPointMake(0.0, 0.0);

        break;
       }
    }
  }
}

谢谢,

1 个答案:

答案 0 :(得分:0)

-(void) longPress: (UIGestureRecognizer *) gestureRecognizer
{     
  static NSInteger intValue;

 UIGestureRecognizerState state = [gestureRecognizer state];

switch (state) {
    case UIGestureRecognizerStateBegan: {
        NSLog(@"LongPress UIGestureRecognizerStateBegan");
        beginPositionlocation = [gestureRecognizer locationInView:self.view];
        NSLog(@"%f", beginPositionlocation.y);

        volumeLabelOnLongPress = nil;

        for (UIView* view in gestureRecognizer.view.subviews) {
            if (view.tag == kVolumeLblTag) {
                volumeLabelOnLongPress = (UILabel *)view;
            }
        }
        break;
    }
    case UIGestureRecognizerStateChanged: {

            endPositionlocation = [gestureRecognizer locationInView:self.view];

            NSLog(@"%f", beginPositionlocation.y);
            NSLog(@"%f", endPositionlocation.y);

                intValue = (NSInteger) roundf(beginPositionlocation.y-endPositionlocation.y);

        if (volumeLabelOnLongPress!=nil) {
            [volumeLabelOnLongPress setText:[NSString stringWithFormat:@"%ld",(long)intValue]];

              if ((intValue >= 10 ) && (intValue <=20)){
                [volumeLabelOnLongPress setText:[NSString stringWithFormat:@"%ld",(long)intValue]];

            }

              else if ((intValue >= 21 ) && (intValue <=30)){
                [volumeLabelOnLongPress setText:[NSString stringWithFormat:@"%ld",(long)intValue]];

            }
       }

        break;
    }
    case UIGestureRecognizerStateEnded: {
        NSLog(@"LongPress UIGestureRecognizerStateEnded");
       endPositionlocation = CGPointMake(0.0, 0.0);
        beginPositionlocation = CGPointMake(0.0, 0.0);

        break;
       }
    }
  }
}
相关问题