如何在我的应用程序中实现UITapGestureRecognizer

编程入门 行业动态 更新时间:2024-10-27 02:25:56
本文介绍了如何在我的应用程序中实现UITapGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对编程和目标C都很陌生。我想知道如何制作一个有空白屏幕和一分钟计时器的应用程序。你的意思是尽可能快地点击,只要你可以。我想知道如何在我的代码中实现 UITapGestureRecognizer 。

I am quite new to programming and Objective C. I was wondering how to make an app which has a blank screen and a timer that goes for one minute. You are meant to tap as fast as you can and as long as you can for. I was wondering how to implement the UITapGestureRecognizer into my code.

推荐答案

这是关于如何在班级中实现手势识别器的分步指南:

This is a step by step guide on how to implement gesture recognizers in your class:

  • 使您的班级符合 UIGestureRecognizerDelegate protocol。

  • Conform your class to UIGestureRecognizerDelegate protocol.

    实例化手势识别器。例如,要实例化 UITapGestureRecognizer ,我们将执行以下操作:

    Instantiate the gesture recognizer. For example, to instantiate a UITapGestureRecognizer, we will do:

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];

    这里,action是处理手势的选择器。在这里,我们的选择器handleTapFrom将如下所示:

    Here, action is the selector which will handle the gesture. Here, our selector handleTapFrom will look something like:

    - (void) handleTapFrom: (UITapGestureRecognizer *)recognizer { //Code to handle the gesture }

    选择器的参数是手势识别器。我们可以使用此手势识别器来访问其属性,例如,我们可以找到手势识别器的状态,例如 UIGestureRecognizerStateBegan , UIGestureRecognizerStateEnded 等。

    The argument to the selector is the gesture recognizer. We can use this gesture recognizer to access its properties, for example, we can find the state of the gesture recognizer, like, UIGestureRecognizerStateBegan, UIGestureRecognizerStateEnded, etc.

    在实例化的手势识别器上设置所需的属性。例如,对于 UITapGestureRecognizer ,我们可以设置属性 numberOfTapsRequired 和 numberOfTouchesRequired 。

    Set the desired properties on the instantiated gesture recognizer. For example, for a UITapGestureRecognizer, we can set the properties numberOfTapsRequired, and numberOfTouchesRequired.

    将手势识别器添加到要检测手势的视图中。在我们的示例代码中(我将分享该代码供您参考),我们将使用以下代码行向imageView添加手势识别器:

    Add the gesture recognizer to the view you want to detect gestures for. In our sample code (I will be sharing that code for your reference), we will add gesture recognizers to an imageView with the following line of code:

    [self.imageView addGestureRecognizer:tapGestureRecognizer];

  • 将手势识别器添加到视图后,设置手势识别器的代理,即将处理所有手势识别器内容的类。在我们的示例代码中,它类似于:

  • After adding the gesture recognizer to the view, set the delegate for the gesture recognizer, i.e. the class which will handle all the gesture recognizer stuff. In our sample code, it would be like:

    tapGestureRecognizer.delegate = self;

    注意:在向视图添加手势识别器后分配代理。否则,将不会调用action方法。

    Note: Assign the delegate after adding the gesture recognizer to the view. Otherwise, the action method won’t be called.

  • 更多推荐

    如何在我的应用程序中实现UITapGestureRecognizer

    本文发布于:2023-11-12 03:29:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1580381.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应用程序   如何在   UITapGestureRecognizer

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!