- 基本步骤:
- 创建手势识别器对象,例如
UITapGestureRecognizer
。
- 设置手势识别器的目标和动作,即指定当手势发生时调用哪个对象的哪个方法。
- 将手势识别器添加到需要识别手势的视图上。
- 关键代码示例:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建UITapGestureRecognizer对象,设置点击次数为1
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
// 将手势识别器添加到视图上
[self.view addGestureRecognizer:tapGesture];
}
// 处理点击手势的方法
- (void)handleTap:(UITapGestureRecognizer *)sender {
NSLog(@"View was tapped!");
}
@end