2015-05-13 24 views
1

我打电话给图像视图上的UIGestureRecognizer类,并在调用UIGestureRecognizerStateChanged时为相关标签设置值。如何在重复调用UIGestureRecognizer类时保留变量的值?

如果第二次调用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; 
     } 
    } 
    } 
} 

谢谢你,

+0

问题是什么?只需跟踪状态并在某些条件下不要覆盖变量 – Azat

+1

发布相关代码 – Michael

+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; 
     } 
    } 
    } 
} 
+0

从你的代码改变的唯一的事情是,我将intValue设为'static' – Lefteris

+0

是的,我意识到了这一点,但这意味着可以再次初始化该值,但是如何保留先前已更改UIGestureRecognizerStateChanged被调用时的值? –

+0

你试过我的代码吗?只要整个类没有再次初始化,intValue将保留以前的值。这就是'静态'变量的工作方式 – Lefteris

相关问题