7.3. Detecting Panning and Dragging Gestures
Problem
You want the users of your application to be able to move GUI elements around using their fingers.
Note
Pan gestures are continuous movements of fingers on the screen; recall that swipe gestures were discrete gestures. This means the method set as the target method of a pan gesture recognizer gets called repeatedly from the beginning to the end of the recognition process.
Solution
Use the UIPanGestureRecognizer
class:
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; /* Let's first create a label */ CGRect labelFrame = CGRectMake(0.0f, /* X */ 0.0f, /* Y */ 150.0f, /* Width */ 100.0f); /* Height */ self.helloWorldLabel = [[UILabel alloc] initWithFrame:labelFrame]; self.helloWorldLabel.text = @"Hello World"; self.helloWorldLabel.backgroundColor = [UIColor blackColor]; self.helloWorldLabel.textColor = [UIColor whiteColor]; self.helloWorldLabel.textAlignment = UITextAlignmentCenter; /* Make sure to enable user interaction; otherwise, tap events won't be caught on this label */ self.helloWorldLabel.userInteractionEnabled = YES; /* And now make sure this label gets displayed on the view */ [self.view addSubview:self.helloWorldLabel]; /* Create the Pan Gesture Recognizer */ self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestures:)]; /* At least and at most we need only one finger to activate the pan gesture recognizer */ self.panGestureRecognizer.minimumNumberOfTouches ...
Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.