0

不知道我做错了什么,但这里有一个简化的例子:

@interface Test : NSObject<UIGestureRecognizerDelegate> {
    UIView *_someParentView;
    UIView *_someChildView;
}
- (id)initWithParentView:(UIView *)parentView;
@end

@implementation Test

- (id)initWithParentView:(UIView *)parentView
{
    if (self = [super init])
    {
        _someParentView = parentView;
    }
    return self;
}

- (void)addSubViewsWhenReady
{
    _someChildView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _someChildView.backgroundColor = [UIColor blackColor];
    [_someChildView setUserInteractionEnabled:YES];
    [_someParentView addSubview:_someChildView];

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleFingerTap.delegate = self;
    [_someChildView addGestureRecognizer:singleFingerTap];
}

- (void)handleSingleTap:(id)sender
{
    NSLog(@"handle the single tap");
}

@end

输出:“处理单击”永远不会被记录。关于我做错了什么的任何想法?

谢谢!

4

2 回答 2

3

像你一样设置目标:

UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

是你的问题。如果这里的“self”是一个 UIViewController,它会起作用。

IE

@interface Test : UIViewController<UIGestureRecognizerDelegate> {
    UIView *_someParentView;
    UIView *_someChildView;
}
- (id)initWithParentView:(UIView *)parentView;
@end

@implementation Test

- (id)initWithParentView:(UIView *)parentView
{
    if (self = [super init])
    {
        _someParentView = parentView;
    }
    return self;
}

- (void)addSubViewsWhenReady
{
    _someChildView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _someChildView.backgroundColor = [UIColor blackColor];
    [_someChildView setUserInteractionEnabled:YES];
    [_someParentView addSubview:_someChildView];

    UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleFingerTap.delegate = self;
    [_someChildView addGestureRecognizer:singleFingerTap];
}

- (void)handleSingleTap:(UIGestureRecognizer*)recognizer    {
    NSLog(@"handle the single tap");
}

@end
于 2013-12-30T00:24:36.060 回答
1

尝试将您的定义更改handleSingleTap:

- (void)handleSingleTap:(UIGestureRecognizer*)recognizer {
    NSLog(@"handle the single tap");
}

UIGestureRecognizer文档:

手势识别器有一个或多个与之关联的目标-动作对。如果有多个目标-动作对,它们是离散的,而不是累积的。手势的识别导致将动作消息分派到这些对中的每一对的目标。调用的操作方法必须符合以下签名之一

- (void)handleGesture;

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

于 2013-04-14T00:41:36.000 回答