iOS手势的实现方法

时间:2021-08-03 08:57:23

本文实例为大家分享了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

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