-
Notifications
You must be signed in to change notification settings - Fork 25
/
GesturesViewController.m
115 lines (98 loc) · 4.46 KB
/
GesturesViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* * Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#import "GesturesViewController.h"
@interface GesturesViewController ()
@property UIImage *image;
@property UIImageView *imageView;
@property UIPanGestureRecognizer *panRecognizer;
@property UIRotationGestureRecognizer *rotateRecognizer;
@property UIPinchGestureRecognizer *pinchRecognizer;
@end
static NSString* const IMAGE_NAME = @"solid";
static NSString* const IMAGE_TYPE = @"png";
/**
* A gesture controller using a image
*/
@implementation GesturesViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpView];
}
-(void)setUpView{
NSString *path = [[NSBundle mainBundle] pathForResource:IMAGE_NAME ofType:IMAGE_TYPE];
_image = [[UIImage alloc] initWithContentsOfFile:path];
_imageView = [[UIImageView alloc] initWithFrame:
[self frameFromCGPoint:CGPointZero AndCGSize:[self getLargeSquareSize]]];
[self centerView:_imageView];
_imageView.contentMode = UIViewContentModeScaleToFill;
_imageView.image = _image;
_imageView.userInteractionEnabled = YES;
_panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
_panRecognizer.delegate = self;
_pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
_pinchRecognizer.delegate = self;
_rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotateGesture:)];
_rotateRecognizer.delegate = self;
[_imageView addGestureRecognizer:_panRecognizer];
[_imageView addGestureRecognizer:_pinchRecognizer];
[_imageView addGestureRecognizer:_rotateRecognizer];
[self centerViewByWidth:_imageView];
[self.view addSubview:_imageView];
}
/**
* Action for the pan gesture. Moves the image based on touch.
*
* @param recognizer gesture recognizer
*/
-(IBAction)handlePanGesture:(UIPanGestureRecognizer*)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slide = magnitude / 200;
float slideFactor = 0.1 * slide;
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
[UIView animateWithDuration:slideFactor * 2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
}
}
/**
* Action for the pinch gesture. Scales the image based on touch.
*
* @param recognizer the gesture regonizer.
*/
-(IBAction)handlePinchGesture:(UIPinchGestureRecognizer*)recognizer {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
/**
* Action for the rotation gesture. Rotates the image based on touch
*
* @param recognizer the gesture recognizer
*/
-(IBAction)handleRotateGesture:(UIRotationGestureRecognizer*)recognizer {
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
@end