/
SpinGestureRecognizer.m
61 lines (48 loc) · 1.89 KB
/
SpinGestureRecognizer.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
//
// SpinGestureRecognizer.m
// PhotoWheelPrototype
//
// Created by Kirby Turner on 9/24/11.
// Copyright (c) 2011 White Peak Software Inc. All rights reserved.
//
#import "SpinGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation SpinGestureRecognizer
@synthesize rotation = _rotation;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Fail when more than 1 finger detected.
if ([[event touchesForGestureRecognizer:self] count] > 1) {
[self setState:UIGestureRecognizerStateFailed];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self setState:UIGestureRecognizerStateEnded];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self setState:UIGestureRecognizerStateFailed];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([self state] == UIGestureRecognizerStatePossible) {
[self setState:UIGestureRecognizerStateBegan];
} else {
[self setState:UIGestureRecognizerStateChanged];
}
// We can look at any touch object since we know we
// have only 1. If there were more than 1,
// touchesBegan:withEvent: would have failed the recognizer.
UITouch *touch = [touches anyObject];
// To rotate with one finger, we simulate a second finger.
// The second finger is on the opposite side of the virtual
// circle that represents the rotation gesture.
UIView *view = [self view];
CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds]));
CGPoint currentTouchPoint = [touch locationInView:view];
CGPoint previousTouchPoint = [touch previousLocationInView:view];
CGFloat angleInRadians = atan2f(currentTouchPoint.y - center.y, currentTouchPoint.x - center.x) - atan2f(previousTouchPoint.y - center.y, previousTouchPoint.x - center.x);
[self setRotation:angleInRadians];
}
@end