UI基础 - UIRecognizer
▶ UIRecognizer
1 - ⼿势识别器是对触摸事件做了封装,无需⾃己去判断某个手势是否触发
2 - ⼿势识别器有 7 个子类
UITapGestureRecognizer :轻拍手势识别器,能识别轻拍操作
UILongPressGestureRecognizer:长按手势识别器,能识别长按操作
UIRotationGestureRecognizer :旋转手势识别器,能识别旋转操作
UIPinchGestureRecognizer :捏合手势识别器,能识别捏合操作
UIPanGestureRecognizer :平移手势识别器,能识别拖拽操作
UISwipeGestureRecognizer :轻扫手势识别器,能识别拖拽操作;
UIScreenEdgePanGestureRecognizer:屏幕边缘轻扫识别器,是 iOS7 中新增的手势
3 - 代码示例:如何使用手势
1 #import "ViewController.h" 2 @interface ViewController() 3 4 @property(nonatomic,strong)UIImageView *imageView; 5 6 @end 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 // 图片 13 self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1n.jpeg"]]; 14 self.imageView.frame = CGRectMake(0, 200, self.view.frame.size.width - 100, 180) ; 15 self.imageView.userInteractionEnabled = YES; 16 [self.view addSubview:self.imageView]; 17 18 // 轻拍 19 //[self addTapGesture]; 20 21 // 长按 22 //[self addLongPressGesture]; 23 24 // 轻扫 25 //[self addSwipeGesture]; 26 27 // 捏合 28 //[self addPinchGesture]; 29 30 // 旋转 31 //[self addRotationGesture]; 32 33 // 拖动手势 34 //[self addPanGesture]; 35 36 // 边缘手势 37 //[self addScreenEdgePanGesture]; 38 39 } 40 41 // 轻拍手势 42 -(void)addTapGesture{ 43 44 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; 45 tapGesture.numberOfTapsRequired = 2;// 需连续点2次 46 tapGesture.numberOfTouchesRequired = 1;// 需1个手指同时点击 47 [self.imageView addGestureRecognizer:tapGesture]; 48 49 } 50 - (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture{ 51 52 UIImageView *imageView = (UIImageView*)tapGesture.view; 53 imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%dn.jpeg",arc4random()%3+1]]; 54 } 55 56 57 // 长按手势 58 -(void)addLongPressGesture{ 59 60 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; 61 longPress.minimumPressDuration = 1.0;// 长按时间 62 [self.imageView addGestureRecognizer:longPress]; 63 } 64 -(void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture{ 65 66 UIImageView *imageView = (UIImageView*)longPressGesture.view; 67 68 // 开始长按 69 if (longPressGesture.state == UIGestureRecognizerStateBegan) { 70 imageView.image = [UIImage imageNamed:@"2n.jpeg"]; 71 } 72 // 结束长按 73 else if (longPressGesture.state == UIGestureRecognizerStateEnded){ 74 imageView.image = [UIImage imageNamed:@"3n.jpeg"]; 75 } 76 } 77 78 79 // 轻扫手势 80 -(void)addSwipeGesture{ 81 82 UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; 83 swipeGesture.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;// 轻扫方向 84 [self.imageView addGestureRecognizer:swipeGesture]; 85 86 } 87 - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture{ 88 89 UIImageView *imageView = (UIImageView*)swipeGesture.view; 90 imageView.image = [UIImage imageNamed:@"3n.jpeg"]; 91 } 92 93 // 捏合手势 94 -(void)addPinchGesture{ 95 96 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)]; 97 [self.imageView addGestureRecognizer:pinchGesture]; 98 99 } 100 - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture{ 101 102 // // 方式一 103 // CGRect newBounds = pinchGesture.view.bounds; 104 // newBounds.size.width *= pinchGesture.scale; 105 // newBounds.size.height *= pinchGesture.scale; 106 // pinchGesture.view.bounds = newBounds; 107 // // 缩放系数:每次计算完以后 必须让 scale 归 1.0 108 // pinchGesture.scale = 1.0; 109 110 // 方式二:CGAffineTransformScale 111 pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale); 112 pinchGesture.scale = 1.0; 113 } 114 115 // 旋转手势 116 - (void)addRotationGesture{ 117 118 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)]; 119 [self.imageView addGestureRecognizer:rotationGesture]; 120 } 121 - (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture{ 122 123 rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation); 124 rotationGesture.rotation = 0.0; 125 } 126 127 // 拖动手势 128 -(void)addPanGesture{ 129 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; 130 [self.imageView addGestureRecognizer:panGesture]; 131 } 132 - (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture{ 133 134 // // 方式一 135 // CGPoint offSet = [panGesture translationInView:panGesture.view]; 136 // NSLog(@"%@",NSStringFromCGPoint(offSet)); 137 // CGPoint newCenter = panGesture.view.center; 138 // newCenter.x += offSet.x; 139 // newCenter.y += offSet.y; 140 // panGesture.view.center = newCenter; 141 // [panGesture setTranslation:CGPointZero inView:panGesture.view];// 偏移量是累加的,每次都须清零 142 143 // 方式二 144 CGPoint offSet = [panGesture translationInView:panGesture.view]; 145 panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, offSet.x, offSet.y); 146 [panGesture setTranslation:CGPointZero inView:panGesture.view];// 偏移量清零 147 } 148 149 150 // 边缘手势 151 -(void)addScreenEdgePanGesture{ 152 153 UIScreenEdgePanGestureRecognizer *screenEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleScreenEdgePanGesture:)]; 154 screenEdgeGesture.edges = UIRectEdgeLeft; 155 [self.view addGestureRecognizer:screenEdgeGesture]; 156 } 157 - (void)handleScreenEdgePanGesture:(UIScreenEdgePanGestureRecognizer *)screenEdgeGesture{ 158 159 self.imageView.image = [UIImage imageNamed:@"2n.jpeg"]; 160 } 161 162 @end
分类:
UI章节
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律