服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS手势的实现方法

iOS手势的实现方法

2021-03-09 15:49makingitbest IOS

这篇文章主要为大家详细介绍了iOS手势的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了ios手势的具体实现代码,供大家参考,具体内容如下

效果

iOS手势的实现方法

细节

1.uitouch

 

?
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
#import "viewcontroller_0.h"
 
@interface viewcontroller_0 ()
 
@property (nonatomic, strong)uilabel *label;
 
@end
 
@implementation viewcontroller_0
 
- (void)viewdidload {
  
  [super viewdidload];
  
  self.label          = [[uilabel alloc] initwithframe:cgrectmake(100, 100, 100, 100)];
  self.label.backgroundcolor  = [uicolor yellowcolor];
  self.label.layer.borderwidth = 1;
  [self.view addsubview:self.label];
  
  uilabel *textlabel = [[uilabel alloc] initwithframe:cgrectmake(10, 80, 200, 45)];
  textlabel.text   = @"拖动方块";
  [textlabel sizetofit];
  textlabel.font   = [uifont systemfontofsize:12];
  [self.view addsubview:textlabel];
}
 
- (void)touchesbegan:(nsset<uitouch *> *)touches withevent:(nullable uievent *)event {
 
  //1.拿到手势
  uitouch *touch = [touches anyobject];
  
  //2.拿到touch 所在view的坐标
  cgpoint point = [touch locationinview:self.view];
  
  //3.让label拿到坐标
  self.label.center = point;
  
  nslog(@"1.手指接触到了屏幕");
}
 
- (void)touchesmoved:(nsset<uitouch *> *)touches withevent:(nullable uievent *)event {
 
  //1.拿到手势
  uitouch *touch = [touches anyobject];
  
  //2.拿到touch 所在view的坐标
  cgpoint point = [touch locationinview:self.view];
  
  //3.让label拿到坐标
  self.label.center = point;
  
  nslog(@"2.手指在屏幕上移动");
}
 
- (void)touchesended:(nsset<uitouch *> *)touches withevent:(nullable uievent *)event {
 
   nslog(@"3.手指刚离开屏幕");
}
 
- (void)touchescancelled:(nsset<uitouch *> *)touches withevent:(nullable uievent *)event {
 
  nslog(@"4.手势失效了");
}
 
@end

2.uitapgesturerecognizer

?
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
#import "viewcontroller_1.h"
 
@interface viewcontroller_1 ()
 
@property (nonatomic, strong) uilabel *label;
 
@end
 
@implementation viewcontroller_1
 
- (void)viewdidload {
  
  [super viewdidload];
 
  uilabel *textlabel = [[uilabel alloc] initwithframe:cgrectmake(10, 80, 200, 45)];
  textlabel.text   = @"电脑上操作tap手势 alt +shift 需要连续点击次数2次";
  [textlabel sizetofit];
  textlabel.font   = [uifont systemfontofsize:12];
  [self.view addsubview:textlabel];
  
  self.label            = [[uilabel alloc] initwithframe:cgrectmake(100, 150, 100, 100)];
  self.label.backgroundcolor    = [uicolor orangecolor];
  self.label.userinteractionenabled = yes;
  self.label.text          = @"0";
  self.label.textalignment     = nstextalignmentcenter;
  [self.view addsubview:self.label];
  
  // 1.创建tap手势
  uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(labeltap:)];
  tap.numberoftapsrequired  = 2; //需要轻轻点击的次数
  tap.numberoftouchesrequired = 2;//需要的手指数量 :2根手指alt +shift 需要匹配点击次数2次(其实直接用默认的就好)
  [self.label addgesturerecognizer:tap];
}
 
- (void)labeltap:(uitapgesturerecognizer *)tap {
 
  int num = [self.label.text intvalue];
  num++;
  self.label.text = [nsstring stringwithformat:@"%d",num ];
}
 
@end

3.uilongpressgesturerecognizer

?
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
#import "viewcontroller_2.h"
 
@interface viewcontroller_2 () <uigesturerecognizerdelegate>
 
@property (nonatomic, strong) uilabel *label;
 
@end
 
@implementation viewcontroller_2
 
- (void)viewdidload {
  
  [super viewdidload];
  
  uilabel *textlabel = [[uilabel alloc] initwithframe:cgrectmake(10, 80, 200, 45)];
  textlabel.text   = @"long长按手势";
  [textlabel sizetofit];
  textlabel.font   = [uifont systemfontofsize:12];
  [self.view addsubview:textlabel];
  
  self.label            = [[uilabel alloc] initwithframe:cgrectmake(40, 150, 200, 150)];
  self.label.backgroundcolor    = [uicolor graycolor];
  self.label.userinteractionenabled = yes;
  self.label.text          = @"0";
  self.label.textalignment     = nstextalignmentcenter;
  [self.view addsubview:self.label];
  
  uilongpressgesturerecognizer *longpress = [[uilongpressgesturerecognizer alloc] initwithtarget:self action:@selector(longpressaction:)];
  longpress.numberoftapsrequired     = 1;
  longpress.numberoftouchesrequired    = 1;
  longpress.minimumpressduration     = 1.0;
  longpress.delegate  = self;
  [self.label addgesturerecognizer:longpress];
}
 
- (void)longpressaction:(uilongpressgesturerecognizer *)longpress {
 
  if(longpress.state == uigesturerecognizerstatebegan) {
    
    nslog(@"手势状态开始");
    
  } else if(longpress.state == uigesturerecognizerstateended) {
    
    nslog(@"手势状态结束");
  
  } else if(longpress.state == uigesturerecognizerstatechanged) {
    
    nslog(@"手势状态改变");
    
    nsinteger num = [self.label.text integervalue];
    num ++;
    self.label.text = [nsstring stringwithformat:@"%ld",(long)num];
  }
}
 
@end

4.uiswipegesturerecognizer

?
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
#import "viewcontroller_3.h"
 
@interface viewcontroller_3 ()
 
@property (nonatomic, strong) uilabel *label;
 
@end
 
@implementation viewcontroller_3
 
- (void)viewdidload {
  
  [super viewdidload];
 
  uilabel *textlabel   = [[uilabel alloc] initwithframe:cgrectmake(10, 80, 200, 45)];
  textlabel.text     = @"swipe手势 向右滑动➕1,你也可以设置左划上划下划";
  textlabel.numberoflines = 0;
  textlabel.font     = [uifont systemfontofsize:12];
  [self.view addsubview:textlabel];
  
  self.label            = [[uilabel alloc] initwithframe:cgrectmake(100, 150, 100, 100)];
  self.label.backgroundcolor    = [uicolor graycolor];
  self.label.userinteractionenabled = yes;
  self.label.text          = @"0";
  self.label.textalignment     = nstextalignmentcenter;
  [self.view addsubview:self.label];
 
  uiswipegesturerecognizer *swipegesture = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(swipeaction:)];
  swipegesture.direction         = uiswipegesturerecognizerdirectionright;//默认就是向右划
  [self.label addgesturerecognizer:swipegesture];
}
 
-(void)swipeaction:(uiswipegesturerecognizer *)swipe {
 
  if (swipe.direction == uiswipegesturerecognizerdirectionleft) {
    
    nslog(@"现在响应左划手势");
    
  } else if (swipe.direction == uiswipegesturerecognizerdirectionright) {
    
    nslog(@"现在响应右划手势");
    
    nsinteger num  = [self.label.text integervalue];
    num ++;
    self.label.text = [nsstring stringwithformat:@"%ld",(long)num];
    
  } else if (swipe.direction == uiswipegesturerecognizerdirectionup) {
    
    nslog(@"现在响应上划手势");
    
  } else if (swipe.direction == uiswipegesturerecognizerdirectiondown) {
    
    nslog(@"现在响应下划手势");
  }
}
 
@end

 5.uipangesturerecognizer

?
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
#import "viewcontroller_4.h"
 
@interface viewcontroller_4 ()
 
@property (nonatomic, strong) uilabel *label;
 
@end
 
@implementation viewcontroller_4
 
- (void)viewdidload {
  
  [super viewdidload];
  
  uilabel *textlabel = [[uilabel alloc] initwithframe:cgrectmake(10, 80, 200, 45)];
  textlabel.text   = @"pan手势,拖动方块";
  [textlabel sizetofit];
  textlabel.font   = [uifont systemfontofsize:12];
  [self.view addsubview:textlabel];
 
  self.label            = [[uilabel alloc] initwithframe:cgrectmake(100, 100, 100, 100)];
  self.label.backgroundcolor    = [uicolor graycolor];
  self.label.userinteractionenabled = yes;
  self.label.text          = @"0";
  self.label.textalignment     = nstextalignmentcenter;
  [self.view addsubview:self.label];
 
  uipangesturerecognizer *pan = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(panaction:)];
  [self.label addgesturerecognizer:pan];
}
 
- (void)panaction:(uipangesturerecognizer *)pan {
  
  cgpoint offset  = [pan locationinview:self.view];
  self.label.center = offset;
}
 
@end

6.uirotationgesturerecognizer

?
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
#import "viewcontroller_5.h"
 
@interface viewcontroller_5 ()
 
@property (nonatomic, strong) uilabel *label;
 
@end
 
@implementation viewcontroller_5
 
- (void)viewdidload {
  
  [super viewdidload];
  
  uilabel *textlabel = [[uilabel alloc] initwithframe:cgrectmake(10, 80, 200, 45)];
  textlabel.text   = @"模拟器测试旋转手势时,按住 option键,再用触摸板或鼠标操作";
  textlabel.font   = [uifont systemfontofsize:12];
  textlabel.numberoflines = 0;
  [self.view addsubview:textlabel];
 
  self.label            = [[uilabel alloc] initwithframe:cgrectmake(40, 200, 200, 50)];
  self.label.backgroundcolor    = [uicolor graycolor];
  self.label.userinteractionenabled = yes;
  self.label.text          = @"we are friends";
  self.label.textalignment     = nstextalignmentcenter;
  [self.view addsubview:self.label];
  
  //(模拟器测试捏合和旋转手势时,按住 option 键,再用触摸板或鼠标操作)
  uirotationgesturerecognizer *rotation = [[uirotationgesturerecognizer alloc] initwithtarget:self action:@selector(rotationaction:)];
  [self.view addgesturerecognizer:rotation];
 
}
 
- (void)rotationaction:(uirotationgesturerecognizer *)rotation {
  
  self.label.transform = cgaffinetransformrotate(self.label.transform, rotation.rotation);
  rotation.rotation = 0; // 这个很重要!!!!!
 
//  static float orginstate;
// 
//  self.label.transform = cgaffinetransformmakerotation(rotation.rotation + orginstate);
// 
//  if (rotation.state == uigesturerecognizerstateended) {
//   
//    orginstate = orginstate + rotation.state;
//    self.label.transform = cgaffinetransformmakerotation(rotation.rotation);
//  }
}
 
@end

7.uipinchgesturerecognizer

?
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
#import "viewcontroller_6.h"
 
@interface viewcontroller_6 ()
 
@property (nonatomic, strong) uilabel *label;
 
@end
 
@implementation viewcontroller_6
 
- (void)viewdidload {
  
  [super viewdidload];
 
  uilabel *textlabel = [[uilabel alloc] initwithframe:cgrectmake(10, 80, 200, 45)];
  textlabel.text   = @"模拟器测试捏合手势时,按住 option键,再用触摸板或鼠标操作";
  textlabel.font   = [uifont systemfontofsize:12];
  textlabel.numberoflines = 0;
  [self.view addsubview:textlabel];
  
  self.label            = [[uilabel alloc] initwithframe:cgrectmake(100, 250, 80, 80)];
  self.label.backgroundcolor    = [uicolor graycolor];
  self.label.userinteractionenabled = yes;
  self.label.text          = @"0";
  self.label.textalignment     = nstextalignmentcenter;
  [self.view addsubview:self.label];
  
//  (模拟器测试捏合和旋转手势时,按住 option 键,再用触摸板或鼠标操作)
  uipinchgesturerecognizer * pinch = [[uipinchgesturerecognizer alloc]initwithtarget:self action:@selector(pichgesture:)];
  [self.view addgesturerecognizer:pinch];
}
 
- (void)pichgesture:(uipinchgesturerecognizer *)pinch {
  
  static float originscale = 1;
  
  //手势缩放返回的scale 是相对于上一次的
  self.label.transform = cgaffinetransformmakescale(pinch.scale * originscale , pinch.scale*originscale);
  
  if (pinch.state == uigesturerecognizerstateended) {
    
    originscale = originscale * pinch.scale;
  }
}
 
@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22