一、拖拽
示例代碼:
復制代碼
1 //
2 // YYViewController.m
3 // 06-拖拽事件
4 //
5 // Created by apple on 14-6-19.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10
11 @interface YYViewController ()
12 @property (strong, nonatomic) IBOutlet UIView *iconView;
13
14 @end
15
16 @implementation YYViewController
17
18 - (void)viewDidLoad
19 {
20 [super viewDidLoad];
21
22 //拖拽事件
23 UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]init];
24 [self.iconView addGestureRecognizer:pan];
25 [pan addTarget:self action:@selector(panView:)];
26 }
27
28 -(void)panView:(UIPanGestureRecognizer*)pan
29 {
30 //以控制器上的view的左上角為坐標原點
31 CGPoint point=[pan locationInView:pan.view];
32 NSLog(@"拖拽事件");
33 NSLog(@"獲取到的觸摸點的位置為:%@",NSStringFromCGPoint(point));
34
35 CGPoint point1=[pan translationInView:pan.view];
36 NSLog(@"拖拽事件1");
37 NSLog(@"獲取到的觸摸點的位置為:%@",NSStringFromCGPoint(point1));
38
39 //手指拖動,讓自定義的view也跟著手指移動
40 CGPoint temp=self.iconView.center;
41 temp.x+=point1.x;
42 temp.y+=point1.y;
43 self.iconView.center=temp;
44
45 //清空
46 [pan setTranslation:CGPointZero inView:pan.view];
47 }
48 @end
復制代碼
注意點:1.注意拖拽事件的位移疊加。
注意數學遞增性,需要在每次調用完之後進行清空處理。
2.注意獲取的點是以手指按下的點為原點的。CGPoint point1=[pan translationInView:pan.view];
//以控制器上的view的左上角為坐標原點 CGPoint point=[pan locationInView:pan.view];
二、旋轉
示例代碼:
復制代碼
1 //
2 // YYViewController.m
3 // 07-旋轉
4 //
5 // Created by apple on 14-6-19.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10
11 @interface YYViewController ()
12 @property (weak, nonatomic) IBOutlet UIImageView *iconView;
13
14 @end
15
16 @implementation YYViewController
17
18 - (void)viewDidLoad
19 {
20 [super viewDidLoad];
21
22 //旋轉
23 //創建手勢識別器(旋轉)
24 UIRotationGestureRecognizer *gesture=[[UIRotationGestureRecognizer alloc]init];
25 //添加手勢識別器
26 [self.iconView addGestureRecognizer:gesture];
27 //監聽
28 [gesture addTarget:self action:@selector(gestureView:)];
29 }
30
31 -(void)gestureView:(UIRotationGestureRecognizer*)gesture
32 {
33
34 //旋轉的弧度:gesture.rotation
35 NSLog(@"旋轉事件,旋轉的弧度為:%1f",gesture.rotation);
36
37 //讓圖片跟隨手指一起旋轉
38 //每次從最初的位置開始
39 // self.iconView.transform=CGAffineTransformMakeRotation(gesture.rotation);
40
41 //在傳入的transform的基礎上旋轉
42 //在之前的基礎上,讓圖片跟隨一起旋轉(去掉自動布局)
43 //注意問題:以風火輪的速度旋轉
44 self.iconView.transform=CGAffineTransformRotate(self.iconView.transform, gesture.rotation);
45 //將旋轉的弧度清零
46 //(注意不是將圖片旋轉的弧度清零,而是將當前手指旋轉的弧度清零)
47 gesture.rotation=0;
48 }
49 @end
復制代碼
注意點:
1.imageview默認為不可交互的,且不支持多點觸控,需要在storyboard中勾選這兩項。
2.旋轉的度數疊加
3.旋轉
將旋轉弧度清零之後,每次調用又從零開始。
三、縮放
復制代碼
1 //
2 // YYViewController.m
3 // 07-旋轉
4 //
5 // Created by apple on 14-6-19.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10
11 @interface YYViewController ()<UIGestureRecognizerDelegate>
12 @property (weak, nonatomic) IBOutlet UIImageView *iconView;
13
14 @end
15
16 @implementation YYViewController
17
18 - (void)viewDidLoad
19 {
20 [super viewDidLoad];
21 [self pinchTest];
22 [self gestureTest];
23 }
24
25 -(void)pinchTest
26 {
27 //縮放
28 UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]init];
29 [self.iconView addGestureRecognizer:pinch];
30 [pinch addTarget:self action:@selector(pinchView:)];
31
32 //設置代理
33 pinch.delegate=self;
34 }
35
36 -(void)pinchView:( UIPinchGestureRecognizer* )pinch
37 {
38 //縮放的比例 pinch.scale;
39 NSLog(@"縮放:%f",pinch.scale);
40 //對圖片進行縮放
41 // self.iconView.transform=CGAffineTransformMakeScale(pinch.scale,pinch.scale);
42 //在已有的基礎上對圖片進行縮放
43 self.iconView.transform=CGAffineTransformScale(self.iconView.transform, pinch.scale, pinch.scale);
44 //清零
45 pinch.scale=1.0;
46 }
47 -(void)gestureTest
48 {
49 //旋轉
50 //創建手勢識別器(旋轉)
51 UIRotationGestureRecognizer *gesture=[[UIRotationGestureRecognizer alloc]init];
52 //添加手勢識別器
53 [self.iconView addGestureRecognizer:gesture];
54 //監聽
55 [gesture addTarget:self action:@selector(gestureView:)];
56
57 //設置代理
58 gesture.delegate=self;
59 }
60 -(void)gestureView:(UIRotationGestureRecognizer*)gesture
61 {
62
63 //旋轉的弧度:gesture.rotation
64 NSLog(@"旋轉事件,旋轉的弧度為:%1f",gesture.rotation);
65
66 //讓圖片跟隨手指一起旋轉
67 //每次從最初的位置開始
68 // self.iconView.transform=CGAffineTransformMakeRotation(gesture.rotation);
69
70 //在傳入的transform的基礎上旋轉
71 //在之前的基礎上,讓圖片跟隨一起旋轉(去掉自動布局)
72 //注意問題:以風火輪的速度旋轉
73 self.iconView.transform=CGAffineTransformRotate(self.iconView.transform, gesture.rotation);
74 //將旋轉的弧度清零
75 //(注意不是將圖片旋轉的弧度清零,而是將當前手指旋轉的弧度清零)
76 gesture.rotation=0;
77 }
78
79 //實現代理方法
80 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
81 {
82 //默認為NO,這裡設置為YES
83 return YES;
84 }
85 @end