首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIGestureRecognizer示例代码

UIGestureRecognizer示例代码
EN

Stack Overflow用户
提问于 2013-02-17 03:43:46
回答 2查看 6.4K关注 0票数 0

我找不出哪里出了问题。下面是我的代码,它调用委托方法一次,然后停止。

我该怎么办?我还没有找到使用这些委托方法的示例代码。我找到的所有东西都是手势识别器,可以使用不同的代表进行推送和点击。

到目前为止的代码:

代码语言:javascript
复制
-(void)initTouchesRecognizer{

    DLog(@"");

    recognizer = [[UIGestureRecognizer alloc] init];

    [self addGestureRecognizer:recognizer];

}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{


    DLog(@"");


    NSSet *allTouches = [event allTouches];
    for (UITouch *touch in allTouches)
    {


    }

}




-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

}




- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
       DLog(@"");

}



- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}

我从initwithrect调用initTouchesRecognizer来查看我的图像。

我到底做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-17 04:17:35

UIGestureRecognizer是一个抽象类,您不应该将其直接添加到视图中。您需要使用从UIGestureRecognizer继承的具体子类,例如UITapGestureRecognizer或UIPanGestureRecognizer。您也可以创建自己的具体子类,但这通常不是必需的。

下面是一个向视图添加UIPanGestureRecognizer的示例(在视图类代码中,手势通常是从控制器添加到视图中的):

代码语言:javascript
复制
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(mySelector:)];
[self addGestureRecognizer:panGesture];

在这种情况下,只要用户在此视图中平移,就会调用选择器。如果添加了UITapGestureRecognizer,则在用户点击时将调用选择器。

你可以查看苹果文档了解更多信息:http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html#//apple_ref/doc/uid/TP40009541-CH2-SW2

此外,我发现Paul Hagerty在斯坦福大学的演讲很棒,这里有一个关于手势识别器的演讲:https://itunes.apple.com/ca/course/6.-views-gestures-january/id593208016?i=132123597&mt=2

您还应该了解,您发布的方法中没有一个是委托方法,并且它们都与您在代码中添加的UIGestureRecognizer没有任何关系。这些是您要覆盖的UIResponder ( UIView继承的一个类)的实例方法。抽象UIGestureRecognizer也有具有相同名称的实例方法,但是在您的类中调用的不是UIGestureRecognizer方法。

票数 0
EN

Stack Overflow用户

发布于 2013-02-17 19:51:44

不需要添加手势识别器。通过覆盖touchesMoved、touchesEnded和touchesBegan方法,我能够在屏幕上跟踪用户的手指。

简单地说,不要调用:

代码语言:javascript
复制
-(void)initTouchesRecognizer

代码,并且我最初发布的代码将会工作。

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

https://stackoverflow.com/questions/14914383

复制
相关文章

相似问题

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