[英]UIImageView can't detect long press gesture in a customized swippable UITableViewCell which is a scrollview
我正在使用可滑动的UITableViewCell
子类,如下所示:
https://github.com/CEWendel/SWTableViewCell
在我的单元格中,有一个UIImageView
。 我在UIImageView
添加了一个UILongPressGestureRecognizer
。 长按UIImageView
时,我想制作一个“微动摇晃”效果。 但是每次我长按UIImageView
,都会选择整个单元格。 因此,我研究了可滑动的UITableViewCell
代码。 我发现单元格本身还设置了一个UILongPressGestureRecognizer
,如下所示:
self.longPressGestureRecognizer = [[SWLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewPressed:)];
self.longPressGestureRecognizer.cancelsTouchesInView = NO;
self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration;
self.longPressGestureRecognizer.delegate = self;
[self.cellScrollView addGestureRecognizer:self.longPressGestureRecognizer];
该代码来自SWTableViewCell.m
,您可以从上面的链接中找到。
现在我想知道的是有办法的锁定UILongPressGestureRecognizer
的细胞,并且触发了动作UIImageView
时UIImageView
被长按?
非常感谢您的所有帮助。
终于我找到了原因。 我将其发布在这里,以防将来其他人遇到同样的困惑。
在我使用的库中,有几行代码来设置longPressGestureRecognizer
。
self.longPressGestureRecognizer = [[SWLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewPressed:)];
self.longPressGestureRecognizer.cancelsTouchesInView = NO;
self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration;
self.longPressGestureRecognizer.delegate = self;
[self.cellScrollView addGestureRecognizer:self.longPressGestureRecognizer];
只有一行: self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration;
和#define kLongPressMinimumDuration 0.16f
。 所以我的问题的原因是持续时间0.16f太小。 它会比我的长按早触发。 如果我更改持续时间,例如将其更改为0.5s,一切都很好。
您可以尝试下一步(认为不是最佳解决方案,但是可以尝试)
为tableView创建自定义单元格,将UIView添加为内容所有者。 比在cell.m文件中执行下一步
注册您的手势
- (void)initGestrudeRecognizer{ UILongPressGestureRecognizer *longGestrudeRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [self addGestureRecognizer:longGestrudeRecognizer]; }
2.implement longPress
action.In这种方法也都需要动画(你可以很容易地修改它-下面只是为例)
- (IBAction)longPress:(id)sender{
NSLog(@"long press gestrude recognized");
[UIView animateWithDuration:0.05 animations:^{
self.uiview.frame = CGRectMake(-20, 0, self.uiview.frame.size.width, self.uiview.frame.size.height);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.05 animations:^{
self.uiview.frame = CGRectMake(20, 0, self.uiview.frame.size.width, self.uiview.frame.size.height);
} completion:^(BOOL finished){
self.uiview.frame = CGRectMake(0, 0, self.uiview.frame.size.width, self.uiview.frame.size.height);
}];
}];
}
如果您需要识别多个手势,则需要实现<UIGestureRecognizerDelegate>
协议并添加简单返回YES的方法
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
如果您不希望您的单元格变为可选择状态-只需选择单元格的选择类型为NONE
结果,我得到了类似瘦的东西(在longPress上)
关于swipeGestrude用法的好文章也可以在这里找到。 对我来说非常有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.