UIGesture 缩放与旋转手势

Posted vector11248

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIGesture 缩放与旋转手势相关的知识,希望对你有一定的参考价值。

 1 //
 2 //  ViewController.m
 3 //  UIWindowsApp
 4 //
 7 //
 8 
 9 #import "ViewController.h"
10 #import "ViewController2.h"
11 
12 @interface ViewController ()
13 
14 @end
15 
16 @implementation ViewController
17 
18 
19 
20 
21 - (void)viewDidLoad {
22     [super viewDidLoad];
23     
24     UIImage* image = [UIImage imageNamed:@"1.jpg"];
25     
26     UIImageView* _imageView= [[UIImageView alloc]init];
27     
28     _imageView.image = image;
29     
30     _imageView.frame = CGRectMake(50, 100, 220, 300);
31     
32     //_imageView.tag = 101;
33     
34     //是否开启交互事件响应开关,默认值为NO
35     _imageView.userInteractionEnabled = YES;
36     
37     [self.view addSubview:_imageView];
38 
39     _pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAct:)];
40    
41 //    NSLog(@"fff.");
42     [_imageView addGestureRecognizer:_pinchGes];
43     
44     _rotGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotAct:)];
45     
46     [_imageView addGestureRecognizer:_rotGes];
47     
48     _pinchGes.delegate = self;
49     _rotGes.delegate = self;
50     
51     
52 }
53 
54 //是否可以同时响应两个手势,yes 可以,no 不可以
55 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
56 {
57     return YES;
58 }
59 
60 -(void) rotAct:(UIRotationGestureRecognizer*) rot
61 {
62     NSLog(@"ddddd");
63     UIImageView * _imageView = (UIImageView*) rot.view;
64     
65     _imageView.transform = CGAffineTransformRotate(_imageView.transform, rot.rotation);
66     
67     rot.rotation = 0;
68 }
69 
70 
71 
72 
73 
74 -(void) pinchAct:(UIPinchGestureRecognizer*) pinch
75 {
76     
77     //获得监控图像的视图
78     UIImageView * _imageView = (UIImageView*) pinch.view;
79     
80     //p1:原来的矩阵
81     //p2:x方向的缩放
82     //p3:y方向的缩放
83     //返回值是新的缩放后的矩阵变换
84     _imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
85 
86     //scale>1 放大
87     //scale<1 缩小
88     pinch.scale = 1;
89 }
90 - (void)didReceiveMemoryWarning {
91     [super didReceiveMemoryWarning];
92     // Dispose of any resources that can be recreated.
93 }
94 
95 
96 @end
 1 //
 2 //  ViewController.h
 3 //  UIWindowsApp
 4 
 5 
 6 #import <UIKit/UIKit.h>
 7 
 8 @interface ViewController : UIViewController<UIGestureRecognizerDelegate>
 9 {
10    
11     //定义缩放,旋转手势
12     UIPinchGestureRecognizer* _pinchGes;
13     
14     UIRotationGestureRecognizer* _rotGes;
15     
16     
17 }
18 
19 //属性定义
20 //@property(retain,nonatomic) UITextField* textField;
21 
22 
23 
24 
25 @end 

 

以上是关于UIGesture 缩放与旋转手势的主要内容,如果未能解决你的问题,请参考以下文章

为啥我们必须将手势识别器的旋转/缩放设置回 0/1?

iOS transform解决连续多次旋转缩放,实现图片旋转缩放效果

touch.js 拖动缩放旋转 (鼠标手势)

touch.js 拖动缩放旋转 (鼠标手势)

ios手势识别之旋转+缩放

给图片添加缩放旋转放大手势