UIGestureRecognizer对图像进行缩放、移动、旋转操作
iOS提供了拍击、旋转、滑动、挤捏等丰富的手势,因此对图像的操作就显得非常友好、简单。下面代码展示了使用UIGestureRecognizer对图像进行缩放、移动、旋转操作使用方法。
在使用代码之前,首先建立一个UIView,并在该视图中增加一个UIImageView视图,用于展示图像。那么该UIView就类似图像画板一样,对图像的操作都基于此视图中进行。
[代码] 在视图中创建手势识别器UIGestureRecognizers
01 - (void)viewDidLoad {02 [super viewDidLoad];
03
04 UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease];
05 [pinchRecognizer setDelegate:self];
06 [self.view addGestureRecognizer:pinchRecognizer];
07
08 UIRotationGestureRecognizer *rotationRecognizer = [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)] autorelease];
09 [rotationRecognizer setDelegate:self];
10 [self.view addGestureRecognizer:rotationRecognizer];
11
12 UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)] autorelease];
13 [panRecognizer setMinimumNumberOfTouches:1];
14 [panRecognizer setMaximumNumberOfTouches:1];
15 [panRecognizer setDelegate:self];
16 [canvas addGestureRecognizer:panRecognizer];
17
18 UITapGestureRecognizer *tapProfileImageRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)] autorelease];
19 [tapProfileImageRecognizer setNumberOfTapsRequired:1];
20 [tapProfileImageRecognizer setDelegate:self];
21 [canvas addGestureRecognizer:tapProfileImageRecognizer];
22 }
[代码] 下面是响应各手势消息的方法:
01 // 缩放
02 -(void)scale:(id)sender {
03
04 if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
05 _lastScale = 1.0;
06 }
07
08 CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);
09
10 CGAffineTransform currentTransform = photoImage.transform;
11 CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
12
13 [photoImage setTransform:newTransform];
14
15 _lastScale = [(UIPinchGestureRecognizer*)sender scale];
16 [self showOverlayWithFrame:photoImage.frame];
17 }
18
19 // 旋转
20 -(void)rotate:(id)sender {
21
22 if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
23
24 _lastRotation = 0.0;
25 return;
26 }
27
28 CGFloat rotation = 0.0 - (_lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
29
30 CGAffineTransform currentTransform = photoImage.transform;
31 CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
32
33 [photoImage setTransform:newTransform];
34
35 _lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
36 [self showOverlayWithFrame:photoImage.frame];
37 }
38
39 // 移动
40 -(void)move:(id)sender {
41
42 CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:canvas];
43
44 if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
45 _firstX = [photoImage center].x;
46 _firstY = [photoImage center].y;
47 }
48
49 translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
50
51 [photoImage setCenter:translatedPoint];
52 [self showOverlayWithFrame:photoImage.frame];
53 }
欢迎光临小站
好岸园
http://www.hopean.com