UILongPressGestureRecognizer停止触摸不停止触摸
我正在使用UILongPressGestureRecognizer类来处理是否正在选择一个项目。UILongPressGestureRecognizer停止触摸不停止触摸
逻辑如下:用户在1秒内按下一个项目(UIView子类)。一旦检测到手势,该项目就会突出显示并可移动。
用户必须在屏幕上移动此项目而不停止触摸它。
我面临的问题是手势识别阴影touchesBegan /移动/结束需要项目类来安排运动。
我试图删除检测到的手势,并检测到选中的项目。但仍然将消息发送到手势的句柄而不是调用触摸方法。
任何人都知道任何方式来停止“聆听”手势识别器而不留下屏幕的手指?
谢谢。
下面的代码:
-(void)addGestures
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
longPress.minimumPressDuration = iItemLongPressTime;
[self addGestureRecognizer:longPress];
[longPress release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Long press Ended");
}
else {
if (self.isSelected) return;
if ([delegate respondsToSelector:@selector(singleTouch:)])
[delegate singleTouch:self];
[self removeGestureRecognizer:[self.gestureRecognizers objectAtIndex:0]];
NSLog(@"Long press detected.");
}
}
正如你可以在其他分公司的委托调用使各项程序选择,以纪念这个项目看,并随即删除的识别。
我失踪了什么?
- 编辑 -
完成!这工作:
#pragma mark Gesture Functions
-(void)addGestures
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPress:)];
longPress.minimumPressDuration = iItemLongPressTime;
[self addGestureRecognizer:longPress];
[longPress release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Long press Ended");
}
else {
NSLog(@"Long press detected.");
if (self.isSelected) return;
if ([delegate respondsToSelector:@selector(singleTouch:)])
[delegate singleTouch:self];
[sender removeTarget:self action:@selector(handleLongPress:)];
sender.enabled = NO;
[self removeGestureRecognizer:sender];
}
}
关心!
我脑海中有两种解决方案。
- 对于动画的UIView,请写这是从UIView类继承和实现,而不是写Gustures处理动画(如果触摸代表没有在当前类触发)触摸代表一个新的类。
2.在触发一次后,我成功地删除了UILongPressGestureRecognizer。
请参考下面的代码。问我,如果你有
步骤我都做
我加入了一个UIView为“MyView的”我的主视任何疑问
当主视点负载。我已经给myView标签(你可以给1,2,3 ...等)来区分龙头视图和主视图子视图。
将UILongPressGestureRecognizer手势分配给myView并将目标指定为“moveMe”方法。
当用户长按myView时,会触发“moveMe”方法。
然后我迭代与条件标记== 1
我从subview.As取出UILongPressGestureRecognizer的MAINVIEW子视图,我们可以知道,标记1主视图子视图是MyView的。因此,NSLog(@“gesture removed”);和NSLog(@“moveMe”);将仅在一次登录控制台。这个NSLog(@“touchesBegan”);将首先触发而不是触发“moveMe”方法。
然后NSLog(@“touchesBegan”);将在删除手势后始终触发。 “moveMe”方法不会触发。
代码
- (void)viewDidLoad {
//Adding to UIView to main view when application is loading.
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
myView.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myView.tag = 1; //adding a tag to identify it.
//Adding Long Press Gesture to the UIView.
UILongPressGestureRecognizer *myGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveMe:)];
[myView addGestureRecognizer:myGesture];
[myGesture release];
myGesture = nil;
[self.view addSubview:myView];
[myView release];
myView = nil;
[super viewDidLoad];
}
//Method to trigger when user pressed long on the added UIView.
-(void)moveMe:(id)sender
{
for (UIView *subViews in [self.view subviews])
{
if (subViews.tag == 1) {
[subViews removeGestureRecognizer:sender];
NSLog(@"gesture removed");
}
}
NSLog(@"moveMe");
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesBegan");
}
是否自定义UIView类有它自己的触摸处理代码?如果不是,一个简单的解决方案是将UILongPressGestureRecognizer
的allowableMovement
属性设置为CGFLOAT_MAX
或某个大数字,并使用手势更新回调拖动您的自定义视图。您可以在超级视图上使用- (CGPoint)locationInView:(UIView *)view
方法获得位移,并将其位置与识别器开始时的位置进行比较。
+1这是最简单的解决方案。基本上,'如果你使用手势识别器,然后使用手势识别器'。 – NJones 2012-03-13 18:15:58
没有必要更改'allowableMovement'属性;它仅用于测试失败。来自'UILongPressGestureRecognizer.h'中的注释'''在手势失败之前允许的最大移动像素数(一旦识别到(在minimumPressDuration之后),对于剩余的触摸跟踪,手指移动没有限制“ – NJones 2012-03-13 18:17:41
嗨,谢谢你的答案。 UIView子类实现了touchesBegan/Moved/Ended以便在他们的超级视图中操作大小和位置。手势的使用来自对每个项目的触摸不敏感的需要。目前我正在使用禁用手势识别器对象的解决方案,并且像魅力一样工作。 – NemeSys 2012-03-14 20:14:46
请尝试描述你的问题详细 – 2012-03-13 16:52:18
我试着发送删除方法发件人作为参数,但仍然无法正常工作。 – NemeSys 2012-03-13 17:05:14
我编辑了我的答案... – 2012-03-14 16:10:47