一、拖拽
示例代碼:
復(fù)制代碼
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的左上角為坐標(biāo)原點(diǎn)
31 CGPoint point=[pan locationInView:pan.view];
32 NSLog(@"拖拽事件");
33 NSLog(@"獲取到的觸摸點(diǎn)的位置為:%@",NSStringFromCGPoint(point));
34
35 CGPoint point1=[pan translationInView:pan.view];
36 NSLog(@"拖拽事件1");
37 NSLog(@"獲取到的觸摸點(diǎn)的位置為:%@",NSStringFromCGPoint(point1));
38
39 //手指拖動(dòng),讓自定義的view也跟著手指移動(dòng)
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
復(fù)制代碼
注意點(diǎn):1.注意拖拽事件的位移疊加,
IOS開發(fā)UI篇—手勢識別器(拖拽+旋轉(zhuǎn)+縮放)
。注意數(shù)學(xué)遞增性,需要在每次調(diào)用完之后進(jìn)行清空處理。
2.注意獲取的點(diǎn)是以手指按下的點(diǎn)為原點(diǎn)的。CGPoint point1=[pan translationInView:pan.view];
//以控制器上的view的左上角為坐標(biāo)原點(diǎn) CGPoint point=[pan locationInView:pan.view];
二、旋轉(zhuǎn)
示例代碼:
復(fù)制代碼
1 //
2 // YYViewController.m
3 // 07-旋轉(zhuǎn)
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 //旋轉(zhuǎn)
23 //創(chuàng)建手勢識別器(旋轉(zhuǎn))
24 UIRotationGestureRecognizer *gesture=[[UIRotationGestureRecognizer alloc]init];
25 //添加手勢識別器
26 [self.iconView addGestureRecognizer:gesture];
27 //監(jiān)聽
28 [gesture addTarget:self action:@selector(gestureView:)];
29 }
30
31 -(void)gestureView:(UIRotationGestureRecognizer*)gesture
32 {
33
34 //旋轉(zhuǎn)的弧度:gesture.rotation
35 NSLog(@"旋轉(zhuǎn)事件,旋轉(zhuǎn)的弧度為:%1f",gesture.rotation);
36
37 //讓圖片跟隨手指一起旋轉(zhuǎn)
38 //每次從最初的位置開始
39 // self.iconView.transform=CGAffineTransformMakeRotation(gesture.rotation);
40
41 //在傳入的transform的基礎(chǔ)上旋轉(zhuǎn)
42 //在之前的基礎(chǔ)上,讓圖片跟隨一起旋轉(zhuǎn)(去掉自動(dòng)布局)
43 //注意問題:以風(fēng)火輪的速度旋轉(zhuǎn)
44 self.iconView.transform=CGAffineTransformRotate(self.iconView.transform, gesture.rotation);
45 //將旋轉(zhuǎn)的弧度清零
46 //(注意不是將圖片旋轉(zhuǎn)的弧度清零,而是將當(dāng)前手指旋轉(zhuǎn)的弧度清零)
47 gesture.rotation=0;
48 }
49 @end
復(fù)制代碼
注意點(diǎn):
1.imageview默認(rèn)為不可交互的,且不支持多點(diǎn)觸控,需要在storyboard中勾選這兩項(xiàng),
電腦資料
《IOS開發(fā)UI篇—手勢識別器(拖拽+旋轉(zhuǎn)+縮放)》(http://www.ishadingyu.com)。2.旋轉(zhuǎn)的度數(shù)疊加
3.旋轉(zhuǎn)
將旋轉(zhuǎn)弧度清零之后,每次調(diào)用又從零開始。