Permalink
Browse files

Initial implementation and import.

  • Loading branch information...
0 parents commit 5eec21972010b594051c3d336a9db74de9f8c9ae @kirbyt committed Apr 22, 2011
@@ -0,0 +1,40 @@
+//
+// KTOneFingerRotationGestureRecognizer.h
+//
+// Created by Kirby Turner on 4/22/11.
+// Copyright 2011 White Peak Software Inc. All rights reserved.
+//
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#import <UIKit/UIKit.h>
+
+
+@interface KTOneFingerRotationGestureRecognizer : UIGestureRecognizer
+{
+
+}
+
+/**
+ The rotation of the gesture in radians since its last change.
+ */
+@property (nonatomic, assign) CGFloat rotation;
+
+@end
@@ -0,0 +1,106 @@
+//
+// KTOneFingerRotationGestureRecognizer.m
+//
+// Created by Kirby Turner on 4/22/11.
+// Copyright 2011 White Peak Software Inc. All rights reserved.
+//
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#import "KTOneFingerRotationGestureRecognizer.h"
+#import <UIKit/UIGestureRecognizerSubclass.h>
+
+
+@implementation KTOneFingerRotationGestureRecognizer
+
+@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)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
+{
+ // We can look at any touch object since we know we
+ // have only 1. If there were more than 1 then
+ // touchesBegan:withEvent: would have failed the recognizer.
+ UITouch *touch = [touches anyObject];
+
+ // A tap can have slight movement, but we're not interested
+ // in a tap. We want more movement. So if a tap is detected
+ // fail the recognizer.
+ if ([self state] == UIGestureRecognizerStatePossible) {
+ [self setState:UIGestureRecognizerStateBegan];
+ } else {
+ [self setState:UIGestureRecognizerStateChanged];
+ }
+
+ // To rotate with one finger, we simulate a second finger.
+ // The second figure is on the opposite side of the virtual
+ // circle that represents the rotation gesture.
+
+ // Convert to screen coordinates. Gaves us better
+ // rotation control.
+ UIWindow *window = [[self view] window];
+ CGPoint center = [window center];
+ CGPoint currentTouchPoint = [touch locationInView:window];
+ CGPoint previousTouchPoint = [touch previousLocationInView:window];
+
+ // use the movement of the touch to decide
+ // how much to rotate the carousel
+ CGPoint line2Start = currentTouchPoint; //[touch locationInView:view];
+ CGPoint line1Start = previousTouchPoint; //[touch previousLocationInView:view];
+ CGPoint line2End = CGPointMake(center.x + (center.x - line2Start.x), center.y + (center.y - line2Start.y));
+ CGPoint line1End = CGPointMake(center.x + (center.x - line1Start.x), center.y + (center.y - line1Start.y));
+
+ //////
+ // Calculate the angle in radians.
+ // (From: http://iphonedevelopment.blogspot.com/2009/12/better-two-finger-rotate-gesture.html )
+ CGFloat a = line1End.x - line1Start.x;
+ CGFloat b = line1End.y - line1Start.y;
+ CGFloat c = line2End.x - line2Start.x;
+ CGFloat d = line2End.y - line2Start.y;
+
+ CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
+ CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);
+
+ CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
+
+ CGFloat angleInRadians = (line2Slope > line1Slope) ? degs : -degs;
+ //////
+
+ [self setRotation:angleInRadians];
+}
+
+- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
+{
+ [self setState:UIGestureRecognizerStateEnded];
+}
+
+- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
+{
+ [self setState:UIGestureRecognizerStateFailed];
+}
+
+@end
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2011 White Peak Software Inc
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
Oops, something went wrong.

0 comments on commit 5eec219

Please sign in to comment.