Permalink
Browse files

nice little demo of how to use UIGestureRecognizers via IB, image set…

… to be interactive and multitouch (otherwise it ignores gestures)
  • Loading branch information...
davekapp committed Feb 13, 2012
1 parent 129642e commit 17a25c778fe2bc7a2bc4ecf1c47d02c568d3098f
@@ -21,6 +21,7 @@
4EE62D1114E8CD7A000C4928 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4EE62CEF14E8CD7A000C4928 /* Foundation.framework */; };
4EE62D1914E8CD7A000C4928 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4EE62D1714E8CD7A000C4928 /* InfoPlist.strings */; };
4EE62D1C14E8CD7A000C4928 /* GestureTestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4EE62D1B14E8CD7A000C4928 /* GestureTestTests.m */; };
+ 4EE62D2614E8CDA7000C4928 /* halp.png in Resources */ = {isa = PBXBuildFile; fileRef = 4EE62D2514E8CDA7000C4928 /* halp.png */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -38,22 +39,23 @@
4EE62CED14E8CD7A000C4928 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
4EE62CEF14E8CD7A000C4928 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
4EE62CF114E8CD7A000C4928 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
- 4EE62CF514E8CD7A000C4928 /* GestureTest-Info.plist */ = {isa = PBXFileReference; path = "GestureTest-Info.plist"; sourceTree = "<group>"; };
+ 4EE62CF514E8CD7A000C4928 /* GestureTest-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GestureTest-Info.plist"; sourceTree = "<group>"; };
4EE62CF714E8CD7A000C4928 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
4EE62CF914E8CD7A000C4928 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
- 4EE62CFB14E8CD7A000C4928 /* GestureTest-Prefix.pch */ = {isa = PBXFileReference; path = "GestureTest-Prefix.pch"; sourceTree = "<group>"; };
- 4EE62CFC14E8CD7A000C4928 /* AppDelegate.h */ = {isa = PBXFileReference; path = AppDelegate.h; sourceTree = "<group>"; };
+ 4EE62CFB14E8CD7A000C4928 /* GestureTest-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "GestureTest-Prefix.pch"; sourceTree = "<group>"; };
+ 4EE62CFC14E8CD7A000C4928 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
4EE62CFD14E8CD7A000C4928 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
4EE62D0014E8CD7A000C4928 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/MainStoryboard_iPhone.storyboard; sourceTree = "<group>"; };
4EE62D0314E8CD7A000C4928 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/MainStoryboard_iPad.storyboard; sourceTree = "<group>"; };
- 4EE62D0514E8CD7A000C4928 /* ViewController.h */ = {isa = PBXFileReference; path = ViewController.h; sourceTree = "<group>"; };
+ 4EE62D0514E8CD7A000C4928 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
4EE62D0614E8CD7A000C4928 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
4EE62D0D14E8CD7A000C4928 /* GestureTestTests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GestureTestTests.octest; sourceTree = BUILT_PRODUCTS_DIR; };
4EE62D0E14E8CD7A000C4928 /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
- 4EE62D1614E8CD7A000C4928 /* GestureTestTests-Info.plist */ = {isa = PBXFileReference; path = "GestureTestTests-Info.plist"; sourceTree = "<group>"; };
+ 4EE62D1614E8CD7A000C4928 /* GestureTestTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GestureTestTests-Info.plist"; sourceTree = "<group>"; };
4EE62D1814E8CD7A000C4928 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
- 4EE62D1A14E8CD7A000C4928 /* GestureTestTests.h */ = {isa = PBXFileReference; path = GestureTestTests.h; sourceTree = "<group>"; };
+ 4EE62D1A14E8CD7A000C4928 /* GestureTestTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GestureTestTests.h; sourceTree = "<group>"; };
4EE62D1B14E8CD7A000C4928 /* GestureTestTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GestureTestTests.m; sourceTree = "<group>"; };
+ 4EE62D2514E8CDA7000C4928 /* halp.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = halp.png; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -119,6 +121,7 @@
4EE62D0214E8CD7A000C4928 /* MainStoryboard_iPad.storyboard */,
4EE62D0514E8CD7A000C4928 /* ViewController.h */,
4EE62D0614E8CD7A000C4928 /* ViewController.m */,
+ 4EE62D2514E8CDA7000C4928 /* halp.png */,
4EE62CF414E8CD7A000C4928 /* Supporting Files */,
);
path = GestureTest;
@@ -227,6 +230,7 @@
4EE62CF814E8CD7A000C4928 /* InfoPlist.strings in Resources */,
4EE62D0114E8CD7A000C4928 /* MainStoryboard_iPhone.storyboard in Resources */,
4EE62D0414E8CD7A000C4928 /* MainStoryboard_iPad.storyboard in Resources */,
+ 4EE62D2614E8CDA7000C4928 /* halp.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,94 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Scheme
+ version = "1.3">
+ <BuildAction
+ parallelizeBuildables = "YES"
+ buildImplicitDependencies = "YES">
+ <BuildActionEntries>
+ <BuildActionEntry
+ buildForTesting = "YES"
+ buildForRunning = "YES"
+ buildForProfiling = "YES"
+ buildForArchiving = "YES"
+ buildForAnalyzing = "YES">
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "4EE62CE814E8CD79000C4928"
+ BuildableName = "GestureTest.app"
+ BlueprintName = "GestureTest"
+ ReferencedContainer = "container:GestureTest.xcodeproj">
+ </BuildableReference>
+ </BuildActionEntry>
+ </BuildActionEntries>
+ </BuildAction>
+ <TestAction
+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.GDB"
+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.GDB"
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ buildConfiguration = "Debug">
+ <Testables>
+ <TestableReference
+ skipped = "NO">
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "4EE62D0C14E8CD7A000C4928"
+ BuildableName = "GestureTestTests.octest"
+ BlueprintName = "GestureTestTests"
+ ReferencedContainer = "container:GestureTest.xcodeproj">
+ </BuildableReference>
+ </TestableReference>
+ </Testables>
+ <MacroExpansion>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "4EE62CE814E8CD79000C4928"
+ BuildableName = "GestureTest.app"
+ BlueprintName = "GestureTest"
+ ReferencedContainer = "container:GestureTest.xcodeproj">
+ </BuildableReference>
+ </MacroExpansion>
+ </TestAction>
+ <LaunchAction
+ selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.GDB"
+ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.GDB"
+ launchStyle = "0"
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Debug"
+ debugDocumentVersioning = "YES"
+ allowLocationSimulation = "YES">
+ <BuildableProductRunnable>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "4EE62CE814E8CD79000C4928"
+ BuildableName = "GestureTest.app"
+ BlueprintName = "GestureTest"
+ ReferencedContainer = "container:GestureTest.xcodeproj">
+ </BuildableReference>
+ </BuildableProductRunnable>
+ <AdditionalOptions>
+ </AdditionalOptions>
+ </LaunchAction>
+ <ProfileAction
+ shouldUseLaunchSchemeArgsEnv = "YES"
+ savedToolIdentifier = ""
+ useCustomWorkingDirectory = "NO"
+ buildConfiguration = "Release"
+ debugDocumentVersioning = "YES">
+ <BuildableProductRunnable>
+ <BuildableReference
+ BuildableIdentifier = "primary"
+ BlueprintIdentifier = "4EE62CE814E8CD79000C4928"
+ BuildableName = "GestureTest.app"
+ BlueprintName = "GestureTest"
+ ReferencedContainer = "container:GestureTest.xcodeproj">
+ </BuildableReference>
+ </BuildableProductRunnable>
+ </ProfileAction>
+ <AnalyzeAction
+ buildConfiguration = "Debug">
+ </AnalyzeAction>
+ <ArchiveAction
+ buildConfiguration = "Release"
+ revealArchiveInOrganizer = "YES">
+ </ArchiveAction>
+</Scheme>
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>SchemeUserState</key>
+ <dict>
+ <key>GestureTest.xcscheme</key>
+ <dict>
+ <key>orderHint</key>
+ <integer>0</integer>
+ </dict>
+ </dict>
+ <key>SuppressBuildableAutocreation</key>
+ <dict>
+ <key>4EE62CE814E8CD79000C4928</key>
+ <dict>
+ <key>primary</key>
+ <true/>
+ </dict>
+ <key>4EE62D0C14E8CD7A000C4928</key>
+ <dict>
+ <key>primary</key>
+ <true/>
+ </dict>
+ </dict>
+</dict>
+</plist>
@@ -9,5 +9,14 @@
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
+@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
+@property (weak, nonatomic) IBOutlet UIImageView *imageView;
+- (IBAction)wasTapped:(id)sender;
+- (IBAction)wasSwiped:(id)sender;
+- (IBAction)wasPinched:(id)sender;
+- (IBAction)wasRotated:(id)sender;
+
+- (BOOL)canBecomeFirstResponder;
+- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
@end
@@ -8,7 +8,14 @@
#import "ViewController.h"
+#define kImageOriginX 94.0
+#define kImageOriginY 330.0
+#define kImageWidth 132.0
+#define kImageHeight 117.0
+
@implementation ViewController
+@synthesize outputLabel;
+@synthesize imageView;
- (void)didReceiveMemoryWarning
{
@@ -26,6 +33,8 @@ - (void)viewDidLoad
- (void)viewDidUnload
{
+ [self setOutputLabel:nil];
+ [self setImageView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
@@ -39,6 +48,7 @@ - (void)viewWillAppear:(BOOL)animated
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
+ [self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated
@@ -61,4 +71,68 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
}
}
+- (IBAction)wasTapped:(id)sender {
+ UITapGestureRecognizer *recognizer = (UITapGestureRecognizer*)sender;
+ CGPoint location = [recognizer locationInView:[recognizer view]];
+ self.outputLabel.text = [NSString stringWithFormat:@"Tapped @ [%f, %f]!", location.x, location.y];
+}
+
+- (IBAction)wasSwiped:(id)sender {
+ self.outputLabel.text = @"Swiped!";
+}
+
+- (IBAction)wasPinched:(id)sender {
+ UIPinchGestureRecognizer *recognizer = (UIPinchGestureRecognizer*)sender;
+ NSString *output;
+ float scale = recognizer.scale;
+ self.imageView.transform = CGAffineTransformMakeRotation(0.0); // resetting rotation on zoom
+
+ output = [NSString stringWithFormat:@"Pinched w/scale:%1.2f velocity:%1.2f", scale, recognizer.velocity];
+ self.outputLabel.text = output;
+
+ // now set the frame w/the scale to change the image size
+ self.imageView.frame = CGRectMake(kImageOriginX, kImageOriginY, kImageWidth * scale, kImageHeight * scale);
+}
+
+- (IBAction)wasRotated:(id)sender {
+ UIRotationGestureRecognizer *recognizer = (UIRotationGestureRecognizer*)sender;
+ float rotation = recognizer.rotation;
+
+ NSString *output = [NSString stringWithFormat:@"Rotated w/radians:%1.2f, velocity:%1.2f", rotation, recognizer.velocity];
+ self.outputLabel.text = output;
+
+ // now do the actual rotation by setting a transform (which takes radians, so no math needed on our part
+ self.imageView.transform = CGAffineTransformMakeRotation(rotation);
+}
+
+- (BOOL)canBecomeFirstResponder {
+ return YES;
+}
+
+- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
+ if (motion == UIEventSubtypeMotionShake) {
+ self.outputLabel.text = @"Things are shakin' here!";
+ self.imageView.transform = CGAffineTransformMakeRotation(0.0);
+ self.imageView.frame = CGRectMake(kImageOriginX, kImageOriginY, kImageWidth, kImageHeight);
+ }
+}
@end
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Oops, something went wrong.

0 comments on commit 17a25c7

Please sign in to comment.