我在图像视图上调用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;
}
}
}
}
谢谢,
答案 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;
}
}
}
}