我的应用程序包含一副纸牌。卡座中的大多数卡都是用draggableView制作的。我试图制作它,以便在屏幕上双击可以使卡上的按钮消失/重新出现。看完post之后,我试着试一试。我在我的draggableView类中将UITapGesture放入我的initWithFrame方法中,如下所示:
- (id)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {
[self addSubviewFromNib];
[self setupView];
panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(beingDragged:)];
cardWidth = frame.size.width;
cardHeight = frame.size.height;
type = 0;
panGestureRecognizer.delegate = self;
panGestureRecognizer.cancelsTouchesInView = NO;
_backgroundScrollView.panGestureRecognizer.cancelsTouchesInView = NO;
//...more code above....
_backgroundScrollView.userInteractionEnabled = YES;
tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:_backgroundScrollView action:@selector(doubleTap)];
tapGestureRecognizer.delegate = _backgroundScrollView;
tapGestureRecognizer.numberOfTapsRequired = 2;
[_backgroundScrollView addGestureRecognizer:tapGestureRecognizer];
[self addGestureRecognizer:panGestureRecognizer];
}
- (void) doubleTap: (UITapGestureRecognizer *)tapGesturerecognizer{
_mapButton.hidden = !_mapButton.hidden;
_menuButton.hidden = !_menuButton.hidden;
_phoneButton.hidden = !_phoneButton.hidden;
reviewButton.hidden = !reviewButton.hidden;
_shareButton.hidden = !_shareButton.hidden;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// If the gesture recognizer is a UITapGestureRecongizer, but the other
// gesture detected is a UIPanGestureRecognizer, require the
// UITapGestureRecognizer to fail.
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] &&
[otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
} else {
return NO;
}
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
return YES;
}
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint velocity = [panGesture velocityInView:self];
return fabs(velocity.y) <= fabs(velocity.x);
}
return YES;
}
然而,如果我双击,我会得到这个sib abrt错误:
-[UITapGestureRecognizer velocityInView:]: unrecognized selector sent to instance 0x7f9f9a244230
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer velocityInView:]: unrecognized selector sent to instance 0x7f9f9a244230'
我假设这意味着PanGestureRecognizer和TapGesture Recognizer之间存在错误。我该如何解决?
编辑:仍未解决,但如果我将tapGesture更改为可拖动视图(因此,自我),而不是_backgroundScrollView,我会收到此错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DraggableView doubleTap]: unrecognized selector sent to instance 0x7fefdd9e4f70'
其他编辑:我将其更改回backgroundScrollView,现在我将其视为错误:
[UIScrollView doubleTap]: unrecognized selector sent to instance 0x7fe5130af000
答案 0 :(得分:0)
gestureRecognizerShouldBegin
将被调用,当手势识别器即将触发触摸时,它可以是轻击手势,也可以是平移手势。你需要检查一下gestureRecogniser类型,然后再继续。
实施以下方法(取自here)以允许同时识别手势:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// If the gesture recognizer is a UITapGestureRecongizer, but the other
// gesture detected is a UIPanGestureRecognizer, require the
// UIPanGestureRecognizer to fail.
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] &&
[otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
} else {
return NO;
}
}
并且在另一个委托方法中,崩溃原因告诉您:
-[UITapGestureRecognizer velocityInView:]: unrecognized selector sent to instance 0x7f9f9a2
velocityInView
被调用错误的手势类型,为了避免这些错误,修改你的委托方法如下:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
return YES;
}
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
UIPanGestureRecogniser *panGesture = (UIPanGestureRecongiser *)gestureRecogniser;
CGPoint velocity = [panGesture velocityInView:self];
return fabs(velocity.y) <= fabs(velocity.x);
}
return YES;
}
答案 1 :(得分:0)
我的错误: 在我的h文件中,我需要使用draggableView作为tapGesture的委托,然后将gestureRecognizer插入到UIGestureRecognizerDelegate协议中,如下所示:
@protocol UIGestureRecognizerDelegate <NSObject>
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
@end