首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIGestureRecognizer不工作

UIGestureRecognizer不工作
EN

Stack Overflow用户
提问于 2013-04-14 08:23:00
回答 2查看 6K关注 0票数 0

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

代码语言:javascript
复制
@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

输出:“处理单击”永远不会被记录下来。你知道我做错了什么吗?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-30 08:24:36

像你这样设定目标:

代码语言:javascript
复制
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

是你的问题。如果这里的"self“是一个UIViewController,它将会工作。

代码语言:javascript
复制
@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
票数 3
EN

Stack Overflow用户

发布于 2013-04-14 08:41:36

尝试将handleSingleTap:的定义更改为

代码语言:javascript
复制
- (void)handleSingleTap:(UIGestureRecognizer*)recognizer {
    NSLog(@"handle the single tap");
}

UIGestureRecognizer文档中:

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

- (void)handleGesture;

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15994386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档