1、tapGesture 点击手势
复制
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
[imageView addGestureRecognizer:tapGesture];
singleTapGesture.numberOfTapsRequired = 1;
singleTapGesture.numberOfTouchesRequired = 1;
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
UIView *tapView = tapGesture.view;
UIImage *tapImage = ((UIImageView *)tapGesture.view).image;
- (void)tapClick:(UITapGestureRecognizer *)tapGesture {
}
2、longPressGesture 长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];
[imageView addGestureRecognizer:longPressGesture];
UIGestureRecognizerState state = longPressGesture.state;
if (longPressGesture.state == UIGestureRecognizerStateBegan) {
}
if (longPressGesture.state == UIGestureRecognizerStateEnded) {
}
- (void)longPressClick:(UILongPressGestureRecognizer *)longPressGesture {
}
3、rotationGesture 旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationClick:)];
[imageView addGestureRecognizer:rotationGesture];
CGFloat rotation = rotationGesture.rotation * 180 * M_1_PI;
imageView.transform = CGAffineTransformMakeRotation(lastRotation + rotationGesture.rotation);
if (rotationGesture.state == UIGestureRecognizerStateEnded) {
lastRotation += rotationGesture.rotation;
}
- (void)rotationClick:(UIRotationGestureRecognizer *)rotationGesture {
}
4、pinchGesture 捏合手势
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchClick:)];
[imageView addGestureRecognizer:pinchGesture];
CGFloat scale = pinchGesture.scale;
imageView.transform = CGAffineTransformMakeScale(pinchGesture.scale, pinchGesture.scale);
imageView.bounds = CGRectMake(0, 0, imageView.bounds.size.width * pinchGesture.scale,
imageView.bounds.size.height * pinchGesture.scale);
[pinchGesture setScale:1];
- (void)pinchClick:(UIPinchGestureRecognizer *)pinchGesture {
}
5、panGesture 拖动手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];
[imageView addGestureRecognizer:panGesture];
CGPoint currentPoint = [panGesture locationInView:self.view];
imageView.center = currentPoint;
- (void)panClick:(UIPanGestureRecognizer *)panGesture {
}
6、swipeGesture 滑动手势
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipeGesture];
UISwipeGestureRecognizerDirection direction = swipeGesture.direction;
- (void)swipeClick:(UISwipeGestureRecognizer *)swipeGesture {
}
7、旋转+捏合+拖拽
UIRotationGestureRecognizer *rotaitonGest = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationView:)];
rotaitonGest.delegate =self;
[self.imgView addGestureRecognizer:rotaitonGest];
UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchView:)];
[self.imgView addGestureRecognizer:pinchGest];
UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panView:)];
[self.imgView addGestureRecognizer:panGest];
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (void)rotationView:(UIRotationGestureRecognizer *)rotationGest {
NSLog(@"旋转角度 %f",rotationGest.rotation);
self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, rotationGest.rotation);
rotationGest.rotation = 0;
}
- (void)pinchView:(UIPinchGestureRecognizer *)pinchGest {
self.imgView.transform = CGAffineTransformScale(self.imgView.transform, pinchGest.scale, pinchGest.scale);
pinchGest.scale = 1;
}
- (void)panView:(UIPanGestureRecognizer *)panGest {
CGPoint trans = [panGest translationInView:panGest.view];
NSLog(@"%@",NSStringFromCGPoint(trans));
CGPoint center = self.imgView.center;
center.x += trans.x;
center.y += trans.y;
self.imgView.center = center;
[panGest setTranslation:CGPointZero inView:panGest.view];
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)