·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> iOS阶段学习第35天笔记(Touch手势介绍)

iOS阶段学习第35天笔记(Touch手势介绍)

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

一、Touch手势

1、利用手势实现UIButton移动效果  实例代码     


1) 创建一个继承自UIButton的类 MyButton.h  代码实现 

1 #import <UIKit/UIKit.h>
2 @interface MyButton : UIButton
3 @end

2)MyButton.m  的代码实现

 1 #import "MyButton.h"
 2 @implementation MyButton
 3 {
 4     CGPoint _lastPoint;
 5 }
 6 
 7 //手势开始
 8 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 9 {
10     UITouch *touch = [touches anyObject];
11     CGPoint point = [touch locationInView:self];
12     NSLog(@"began:%@",NSStringFromCGPoint(point));
13     _lastPoint = point;
14 }
15 
16 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
17 {
18     UITouch *touch = [touches anyObject];
19     CGPoint point = [touch locationInView:self];  
20     CGFloat offsetx = point.x - _lastPoint.x;
21     CGFloat offsety = point.y - _lastPoint.y; 
22     self.center = CGPointMake(self.center.x + offsetx, self.center.y + offsety);   
23     NSLog(@"moved:%@",NSStringFromCGPoint(point));
24 }
25 
26 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
27 {
28     UITouch *touch = [touches anyObject];
29     CGPoint point = [touch locationInView:self];
30     NSLog(@"end:%@",NSStringFromCGPoint(point));
31 }
32 @end 

3)父视图中的代码实现

 1 #import "ViewController.h"
 2 #import "MyButton.h"
 3 @interface ViewController ()
 4 {
 5     MyButton *_v;
 6     CGPoint _lastPoint;
 7 }
 8 @end
 9 
10 @implementation ViewController
11 
12 - (void)viewDidLoad {
13     [super viewDidLoad];
14     _v = [[MyButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
15  _v.backgroundColor = [UIColor redColor];
16     [self.view addSubview:_v];
17 }
18 
19 //手势开始
20 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
21 {
22     UITouch *touch = [touches anyObject];
23     CGPoint point = [touch locationInView:self.view];
24     NSLog(@"began:%@",NSStringFromCGPoint(point));
25     _lastPoint = point;
26 }
27 
28 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
29 {
30     UITouch *touch = [touches anyObject];
31     CGPoint  point = [touch locationInView:self.view];
32     CGFloat  offsetx = point.x - _lastPoint.x;
33     CGFloat  offsety = point.y - _lastPoint.y; 
34     _v.center = CGPointMake(_v.center.x + offsetx, _v.center.y + offsety);     
35     _lastPoint = point;     
36     NSLog(@"moved:%@",NSStringFromCGPoint(point));
37 }
38 
39 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
40 {
41     UITouch *touch = [touches anyObject];
42     CGPoint  point = [touch locationInView:self.view];
43     NSLog(@"end:%@",NSStringFromCGPoint(point));
44 }
45 
46 -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
47 {
48     
49 }
50 @end

2、利用Touch手势实现控件的缩放与旋转效果 实例代码

 1 #import "ViewController.h"
 2 //遵守旋转与缩放的代理协议
 3 @interface ViewController ()<UIGestureRecognizerDelegate>
 4 @end
 5 
 6 @implementation ViewController
 7 
 8 - (void)viewDidLoad {
 9     [super viewDidLoad];
10     
11     UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
12     imgv.center = self.view.center;
13     [self.view addSubview:imgv];
14     imgv.image = [UIImage imageNamed:@"3"];
15     imgv.userInteractionEnabled = YES;
16     
17     //点击手势
18     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)];
19     
20     //设置该手势需要的手指数
21     tap.numberOfTouchesRequired = 2;
22     
23     //设置该手势的点击次数
24     tap.numberOfTapsRequired = 4;
25     [imgv addGestureRecognizer:tap];
26     
27     //平移手势,拖拽手势
28     UipanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
29     [imgv addGestureRecognizer:pan];
30     
31     //缩放手势
32     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGes:)];
33     [imgv addGestureRecognizer:pinch];
34     pinch.delegate = self;
35     
36     //旋转
37     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
38     [imgv addGestureRecognizer:rotation];
39     rotation.delegate = self;
40 }
41 
42 //返回值表示能否同时识别其他(相对于已经设置了代理的手势)手势
43 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:
(UIGestureRecognizer *)otherGestureRecognizer 44 { 45 return YES; 46 } 47 -(void)rotationGes:(UIRotationGestureRecognizer *)rotation 48 { 49 rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation); 50 rotation.rotation = 0.0; 51 } 52 -(void)pinchGes:(UIPinchGestureRecognizer *)pinch 53 { 54 //transform:仿射变换 55 //pinch.scale,是缩放手势的捏合倍率 56 pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale); 57 58 //倍率还原 59 pinch.scale = 1.0; 60 } 61 -(void)panGes:(UIPanGestureRecognizer *)pan 62 { 63 //返回当前的手势的偏移量 64 CGPoint offset = [pan translationInView:pan.view]; 65 //pan.view就是pan手势所加到的视图 66 pan.view.center = CGPointMake(pan.view.center.x + offset.x, pan.view.center.y + offset.y); 67 //移动以后,把偏移量归0 68 [pan setTranslation:CGPointZero inView:pan.view]; 69 } 70 71 -(void)tapGes:(UIGestureRecognizer *)tap 72 { 73 NSLog(@"=========="); 74 } 75 @end