UI- 五种手势识别总结
#pragma mark - 手势 总共有五种手势 分别为 Tap点击 Pan拖拽 LongPress长时间按压 Pinch捏合手势 rotation旋转
1. 定义成员变量
UIImageView *_imgView; 定义UIImageView, 响应手势方法时调用
CGPoint originalCenter; 记录一下拖拽手势起始位置
CGAffineTransform originalTrans; 记录一下捏合、旋转手势起始位置
2. 定义ImageView
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];
imgView.image = [UIImage imageNamed:@"btn_02"];
[self.view addSubview:imgView];
_imgView = imgView; //赋值
3. 打开与用户的交互能力 (至关重要,不能忘记)
imgView.userInteractionEnabled = YES;
4.Tap手势 --> 点击1次/两次
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onceTapped )];
把这个手势添加到视图中
[imgView addGestureRecognizer:tap];
UITapGestureRecognizer *dblTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twiceTapped)];
dblTap.numberOfTapsRequired = 2;
[imgView addGestureRecognizer:dblTap];
4.1 点击手势响应的方法
-(void)onceTapped{
NSLog(@"onceTapped");
}
-(void)twiceTapped{
NSLog(@"twiceTapped");
}
5.Pan手势 --> 拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(viewPanned:)];
[imgView addGestureRecognizer:pan];
5.1 拖拽时响应的方法
-(void)viewPanned:(UIPanGestureRecognizer *)pan{
先记录一下起始位置
if (pan.state == UIGestureRecognizerStateBegan) {
originalCenter = imgView.center;
}
CGPoint transPoint = [pan translationInView:_imgView];
CGPoint center = originalCenter;
center.x += transPoint.x;
center.y += transPoint.y;
imgView.center = center;
CGPoint velocityPoint = [pan velocityInView:_imgView];
}
6. LongPress手势 --> 长时间按压
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressed )];
[imgView addGestureRecognizer:longPress];
longPress.minimumPressDuration = 1;
6.1 长时间按压响应的方法
-(void)longPressed{
NSLog(@"long Press");
}
7. Pinch手势 --> 捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchImg:)];
[imgView addGestureRecognizer:pinch];
7.1 捏合时响应的方法
-(void)pinchImg:(UIPinchGestureRecognizer *)pinch {
if (pinch.state == UIGestureRecognizerStateBegan) {
originalTrans = _imgView.transform;
}
_imgView.transform = CGAffineTransformScale(originalTrans, pinch.scale, pinch.scale);
}
8. rotation手势 --> 旋转
UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotateImg:)];
[imgView addGestureRecognizer:rotation];
8.1 旋转手势响应的方法
-(void)rotateImg:(UIRotationGestureRecognizer *)rotation{
if (rotation.state == UIGestureRecognizerStateBegan) {
originalTrans = _imgView.transform;
}
_imgView.transform = CGAffineTransformRotate(originalTrans, rotation.rotation);
if (rotation.state == UIGestureRecognizerStateEnded) {
_imgView.transform = originalTrans;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~