iOS 中各种手势的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#import "ViewController.h"
 
@interface ViewController ()
{
    UIView *view1;
}
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor brownColor];
    view1 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    view1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view1];
//添加轻点手势
    [self addGesture];
//    平移手势
    [self addPanGesAddSwipeGes];
//    长按手势
    [self longAction];
//    旋转手势
    [self rote];
//    捏合手势
    [self pinchGess];
     
}
#define mark手势轻点
- (void)addGesture{
    //初始化手势识别器
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    //设置点击次数
    tap.numberOfTapsRequired = 1;
    //设置触摸点数
    tap.numberOfTouchesRequired = 1;
    //视图添加手势
    [self.view addGestureRecognizer:tap];
    //添加target
    [tap addTarget:self action:@selector(tapAct:)];
     
    //手势的代理设置
    tap.delegate = self;
     
}
 
- (void)tapAct:(UIGestureRecognizer*)ges{
    NSLog(@"点击了");
}
 
//添加旋转手势
- (void)rote{
    UIRotationGestureRecognizer *rate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(RoteAct:)];
    [self.view addGestureRecognizer:rate];
}
#define mark旋转手势 点击手势的方法改变
- (void)RoteAct:(UIRotationGestureRecognizer*)ratation{
    if (ratation.state == UIGestureRecognizerStateChanged) {
     
        view1.transform = CGAffineTransformMakeRotation(ratation.rotation);
    
    }else if(ratation.state == UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:.35 animations:^{
            //取消形变,恢复原状
            view1.transform = CGAffineTransformIdentity;
        }];
        
    }
 
     
}
#define mark添加平移和清扫手势
- (void)addPanGesAddSwipeGes{
   //创建平移手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init];
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    // 设置最大识别数
    pan.maximumNumberOfTouches = 1;
//    pan.minimumNumberOfTouches = 1; //最小识别数
    //添加平移手势调用的方法
    [pan addTarget:self action:@selector(panAct:)];
     
     
    //添加清扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]init];
    swipe.delegate = self;
    [self.view addGestureRecognizer:swipe];
    [swipe addTarget:self action:@selector(swipAct:)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
#warning 优先级
#define mark  识别器优先级冲突解决方案,优先级低的调用requireGestureRecognizerToFail方法
    [pan requireGestureRecognizerToFail:swipe];
     
}
//平移手势调用的方法
- (void)panAct:(UIPanGestureRecognizer*)ges{
#define mark获取当前位置的偏移量
    CGPoint p = [ges translationInView:self.view];
    NSLog(@"%@",NSStringFromCGPoint(p));
}
//清扫手势调用的方法
- (void)swipAct:(UISwipeGestureRecognizer*)ges{
    NSLog(@"-------%lu",ges.direction);
}
 
#define mark长按手势
- (void)longAction{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]init];
    //设置长按时间
    longPress.minimumPressDuration = 2;
    //添加手势
    [self.view addGestureRecognizer:longPress];
    [longPress addTarget:self action:@selector(action:)];
}
//长按手势调用的方法
- (void)action:(UILongPressGestureRecognizer*)ges{
    if (ges.state == UIGestureRecognizerStateBegan) {
         
     
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"是否删除" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确认" otherButtonTitles:@"其它", nil];
    [actionSheet showInView:self.view];
    }
 
}
#pragma mark-捏合手势
- (void)pinchGess{
    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [self.view addGestureRecognizer:pin];
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pin{
    if (pin.state == UIGestureRecognizerStateChanged) {
     
        view1.transform = CGAffineTransformMakeScale(pin.scale, pin.scale);
     
    }else if(pin.state == UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:.35 animations:^{
            //取消一切形变
            view1.transform = CGAffineTransformIdentity;
        }];
    }
     
}
 
#define mark代理方法 识别器将要开始工作
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
     
    switch (gestureRecognizer.state) {
            case UIGestureRecognizerStatePossible:
            NSLog(@"等待");
            break;
        case UIGestureRecognizerStateBegan:
            NSLog(@"开始");
            break;
            case UIGestureRecognizerStateChanged:
            NSLog(@"改变");
            break;
            case UIGestureRecognizerStateEnded:
            NSLog(@"结束");
            break;
            case UIGestureRecognizerStateFailed:
            NSLog(@"失败");
        default:
            break;
    }
    return YES;
}
//代理方法,识别器将要识别
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    NSLog(@"////");
    return YES;
}

 

posted @   zhangxianhongx  阅读(390)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
点击右上角即可分享
微信分享提示