我对编程和Objective C很陌生。我想知道如何制作一个具有空白屏幕和一分钟计时器的应用程序。你应该尽可能快地点击,只要你可以。我想知道如何在UITapGestureRecognizer
我的代码中实现。
5 回答
这是有关如何在课堂上实现手势识别器的分步指南:
使您的课程符合
UIGestureRecognizerDelegate
协议。实例化手势识别器。例如,要实例化 a
UITapGestureRecognizer
,我们将执行以下操作:UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
在这里,action 是处理手势的选择器。在这里,我们的选择器 handleTapFrom 看起来像:
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer { //Code to handle the gesture }
选择器的参数是手势识别器。我们可以使用这个手势识别器来访问它的属性,例如,我们可以找到手势识别器的状态,比如,
UIGestureRecognizerStateBegan
,UIGestureRecognizerStateEnded
等。在实例化的手势识别器上设置所需的属性。例如,对于 a
UITapGestureRecognizer
,我们可以设置属性numberOfTapsRequired
和numberOfTouchesRequired
。将手势识别器添加到要检测手势的视图中。在我们的示例代码中(我将分享该代码供您参考),我们将使用以下代码行将手势识别器添加到 imageView:
[self.imageView addGestureRecognizer:tapGestureRecognizer];
将手势识别器添加到视图后,设置手势识别器的委托,即处理所有手势识别器内容的类。在我们的示例代码中,它会像:
tapGestureRecognizer.delegate = self;
注意:在将手势识别器添加到视图后分配委托。否则,将不会调用 action 方法。
斯威夫特的例子:
import UIKit
class ViewController: UIViewController {
@IBOutlet var myUIImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "myUIImageViewTapped:")
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
self.myUIImageView.addGestureRecognizer(singleTap)
self.myUIImageView.userInteractionEnabled = true
}
func myUIImageViewTapped(recognizer: UITapGestureRecognizer) {
if(recognizer.state == UIGestureRecognizerState.Ended){
println("myUIImageView has been tapped by the user.")
}
}
}
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
self.tableView.userInteractionEnabled = YES;
gestureRecognizer.cancelsTouchesInView = NO; // this prevents the gesture recognizers to 'block' touches
适用于 Xcode 11.4、Swift 5
class ViewController: UIViewController {
@IBOutlet weak var targetView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchCallback(_:)))
self.targetView.addGestureRecognizer(tap)
}
@objc func touchCallback(_ sender: UITapGestureRecognizer? = nil) {
if(sender?.state == .ended) {
print("tapped")
}
}
}
// If you want to touch start, available touchesBegan
class ViewController: UIViewController {
@IBOutlet weak var targetView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: targetView)
print(location)
}
}
}
如果您正在使用 Swift (iOS9) 和 SpriteKit,请尝试以下操作。
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let singleTap = UITapGestureRecognizer(target:self, action:#selector(self.handleSingleTap:))
singleTap.numberOfTouchesRequired = 1
singleTap.addTarget(self, action:#selector(self.handleSingleTap))
view.userInteractionEnabled = true
view.addGestureRecognizer(singleTap)
}
//event handler
func handleSingleTap(sender:UITapGestureRecognizer){
print("tapped")
}
}