我打电话给图像视图上的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;
}
}
}
}
谢谢你,
问题是什么?只需跟踪状态并在某些条件下不要覆盖变量 – Azat
发布相关代码 – Michael
请检查上面发布的代码... –